Pygame简单问题,对象移动和显示

时间:2016-02-09 21:05:35

标签: python pygame

    player_1 = pygame.image.load(player1)
    #

    def player1(x,y):
        window.blit(player_1, (x,y))

    x =  (110)
    y = (150)
    x_change = 0
    y_change = 0
    player1_speed = 0

    while not gameover:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               gameover = True

            if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_LEFT:
                x_change = -5
               elif event.key == pygame.K_RIGHT:
                  x_change = 5

               elif event.key == pygame.K_UP:
                  y_change = -5

              elif event.key == pygame.K_DOWN:
                 y_change = 5

        if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            x_change = 0


        x += x_change
        y += y_change        

        player1(x,y)
        pygame.display.update()
        fpsClock.tick(60)
        screen.blit(background_surface, (0,0))

# update display
pygame.display.flip()

嗨,我有一个程序,在移动物体时不幸的是,它向左移动&完全正确,但上下似乎并没有停止。还存在屏幕闪烁的问题。很抱歉这个问题很长,但任何帮助都将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

1。据我所知,你只是blitting player1。玩家2在哪里?

2。您的代码中缺少一些缩进。我想正确的形式是:

 if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
        x_change = 0

3. 然后你只是将x_change设置为0.我想你也想把y_change设置为0 ......即使你必须通过分组垂直和水平键。

像:

if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
    x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
    y_change = 0

4. 更新屏幕后调用blit方法。在screen.blit(..)之后移动pygame.display.update()会解决一些问题......

5. 翻转方法不在你的循环中。只有在游戏循环中断时才会调用它!

小提示:复制粘贴是一种非常糟糕的做法。试着自己做吧!