Python Pygame精灵穿越s屏

Python Pygame精灵穿越s屏,python,pygame,Python,Pygame,问题是当我按下左键或右键时,精灵会跨越屏幕的左边界和右边界。但当我轻触它时,它不会只有在我持续按住键时才会交叉 这是人类飞船的等级 class Human: y = display_height * 0.8 x = display_width * 0.45 width = 120 image = pygame.image.load('yasin/alien1.png') def run(self): gameDisplay.blit(Human.image, (Human.x, Hu

问题是当我按下左键或右键时,精灵会跨越屏幕的左边界和右边界。但当我轻触它时,它不会只有在我持续按住键时才会交叉

这是人类飞船的等级

class Human:

y = display_height * 0.8
x = display_width * 0.45
width = 120
image = pygame.image.load('yasin/alien1.png')

def run(self):
    gameDisplay.blit(Human.image, (Human.x, Human.y))
这是在整个游戏中迭代的主循环

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        gameExit = True
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            if human.x > 0:
                x_change = -8
            else:
                x_change = 0
        elif event.key == pygame.K_RIGHT:
            if human.x < display_width - human.width:
                x_change = 8
            else:
                x_change = 0
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            x_change = 0
human.x += x_change
human.run()
pygame.event.get()中事件的
:
如果event.type==pygame.QUIT:
gameExit=True
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
如果human.x>0:
x_变化=-8
其他:
x_变化=0
elif event.key==pygame.K_RIGHT:
如果human.x<显示宽度-human.width:
x_变化=8
其他:
x_变化=0
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
人类x+=x_变化
human.run()

if human.x>0:
if human.x
移出事件循环,因为它们在事件队列中每个事件只执行一次。而是检查主while循环中的玩家是否仍在游戏区域内,否则停止它

我还做了一些修改:属性应该在
\uuuu init\uuuu
方法中定义,以使它们成为实例属性而不是类属性。在课堂上使用
self.x
而不是
Human.x
x_change
y_change
变量属于人类对象,因此它们也应该是属性。然后,您可以将
update
方法添加到
Human
中,在该方法中,您可以执行边界检查和移动

import pygame


display_width, display_height = 640, 480

class Human:

    def __init__(self):
        self.image = pygame.image.load('yasin/alien1.png')
        self.y = display_height * 0.8
        self.x = display_width * 0.45
        self.x_change = 0
        self.y_change = 0
        self.width = 120

    def run(self, gameDisplay):
        gameDisplay.blit(self.image, (self.x, self.y))

    def update(self):
        self.x += self.x_change
        # Check if the human is outside of the game area.
        if self.x < 0:
            self.x_change = 0  # Stop it.
            self.x = 0  # Reset the position, so that we can move again.
        elif self.x > display_width - self.width:
            self.x_change = 0
            self.x = display_width - self.width


def main():
    pygame.init()
    gameDisplay = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    human = Human()

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    human.x_change = -8
                elif event.key == pygame.K_RIGHT:
                    human.x_change = 8
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    human.x_change = 0

        human.update()

        gameDisplay.fill((30, 30, 30))
        human.run(gameDisplay)
        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pygame.quit()
导入pygame
显示宽度,显示高度=640480
类人:
定义初始化(自):
self.image=pygame.image.load('yasin/alien1.png')
self.y=显示高度*0.8
self.x=显示宽度*0.45
self.x_change=0
self.y_change=0
自宽=120
def运行(自我,游戏显示):
blit(self.image,(self.x,self.y))
def更新(自我):
self.x+=self.x_变化
#检查人员是否在游戏区域之外。
如果self.x<0:
self.x_change=0#停止它。
self.x=0#重置位置,以便我们可以再次移动。
elif self.x>显示宽度-self.width:
self.x_change=0
self.x=显示宽度-self.width
def main():
pygame.init()
gameDisplay=pygame.display.set_模式((640480))
clock=pygame.time.clock()
人
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
gameExit=True
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
人类x_变化=-8
elif event.key==pygame.K_RIGHT:
人类x_变化=8
elif event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
人的x_变化=0
human.update()
gameDisplay.fill((30,30,30))
human.run(游戏显示)
pygame.display.flip()
时钟滴答(30)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
pygame.quit()

您是否设置了pygame.key.set\u repeat()?启用键盘重复时,按下的键将生成多个pygame.KEYDOWN事件。当pygame被初始化时,按键重复被禁用。我尝试将其添加到初始化、游戏循环和for事件循环中(因为我不知道它要去哪里,我把它放在所有地方),不幸的是没有改变任何东西我尝试了你放的东西,但是现在精灵似乎一动不动,如果你不给我看代码(a),我帮不了你。您可以发布新问题或编辑此问题。问题很大,因此如果我通过电子邮件发送给您,可以吗?请尝试减少上解释的代码,然后发布新问题或将代码添加到此问题。另外,检查是否在主while循环中调用精灵的
update
方法。你必须每帧调用
human.update()
,否则精灵无法移动。