在pygame中使用y坐标将项目符号向上移动到屏幕上

时间:2019-11-27 06:40:58

标签: python pygame

我正在使用名为“ Python崩溃课程”的书来学习python。 这本书的一部分是使用Pygame制作游戏的,而我则负责为船上的子弹编程子弹模块。 这是我有些困惑的代码。

def __init__(self, ai_game):
    --snip--
    # Create a bullet rect at (0, 0) and then set correct position.
    self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
        self.settings.bullet_height)
    self.rect.midtop = ai_game.ship.rect.midtop
    --snip--
def update(self):
    """Move the bullet up the screen."""
    # Update the decimal position of the bullet.
    self.y -= self.settings.bullet_speed

,最后一行“ self.y-= self.settings.bullet_speed”, 为什么减?子弹的起始位置在底部,并且应该沿y轴上升。我想念什么?

1 个答案:

答案 0 :(得分:3)

我们习惯于位于网格左下角的坐标系(0,0)中的原点。在Pygame和许多图形框架中,原点位于左上角。因此,在1200x800游戏窗口上,左上角为(0,0),左下角为(0,800)。如果要从屏幕上移,请稳定减小y值。

原点位于顶部,因为窗口的顶部通常在屏幕上是静态的。人们调整窗口大小时,通常会拖动屏幕底部,并且所有溢出都会发生在底部。

如果您想查看任何游戏对象的实际坐标,可以在_update_screen()方法中添加如下一行:

print(f"Ship coordinates: {ship.rect.x} {ship.rect.y}")

您会发现有关此here

的有趣历史事实