有3个文件。rocket_game.py,rocket.py,settings.py。rocket.py含量。键“向上”箭头什么都不做。按“向下”箭头将飞船向上移动。如果我将最后一行改为"self.y += self.settings.ship_speed",向下箭头将船向下移动。但是为什么这场游戏不能勾上箭头呢?
def update(self):
"""Update the ship's position based on the moving flag."""
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
elif self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
if self.moving_up and self.rect.top < self.screen_rect.top:
self.y += self.settings.ship_speed
elif self.moving_down and self.rect.bottom > 0:
self.y -= self.settings.ship_speed键“向上”箭头什么都不做。按“向下”箭头将飞船向上移动。如果我改变了自己.y += self.settings.ship_speed,向下箭头将船向上移动,我理解为什么。但是为什么向上箭头不呢?
如果我打印事件,它会看到我按下向上箭头,但火箭不会移动。
另一个问题是,顶部和底部的界限似乎并不存在。
屏幕的左下角分别描述为坐标0、0。我确实尝试将底部的边界设置为0,但船仍然在屏幕下面飞行。左右边界很好。对于python编程来说,我是新手,我试着为rect阅读pygame,但它似乎没有我所需要的引用。它确实列出了上面和下面,而不是上下。我在rocket_game.py和rocket.py中都尝试过多种方法。
没有错误消息,最终目标是添加在screen_rect边界内向上和向下移动、左移动和右移动的功能。
发布于 2021-10-15 15:43:53
在吡咯坐标系中,顶部左坐标为(0,0),底部右侧为(宽度、高度)。
更改移动方向和阻止对象移出屏幕的条件:
if self.moving_up and self.rect.top > 0:
self.y -= self.settings.ship_speed
elif self.moving_down and self.rect.botom < self.screen_rect.bottom:
self.y += self.settings.ship_speedhttps://stackoverflow.com/questions/69587019
复制相似问题