【问题标题】:pygame object moves for a secondpygame 对象移动一秒钟
【发布时间】:2015-09-22 20:11:26
【问题描述】:

代码中的“PlayerRect”出现在屏幕上,我可以移动它,但它只移动了几分之一秒,然后返回到它开始的位置。我使用了不同的代码来让它移动,但它们都有相同的结果。有什么建议吗?

import pygame,sys,os
from pygame.locals import *
pygame.init


MOVERATE = 10
WINDOWWIDTH = 500
WINDOWHEIGHT = 500
def terminate():
     pygame.quit()
     sys.exit()

playerImage = pygame.image.load('Test_Block.png')
playerRect = playerImage.get_rect()

WHITE = (255,255,255,0)

WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.update()


WindowSurface.fill(WHITE)
mainClock = pygame.time.Clock()



while True:



     moveLeft = moveRight = moveUp = moveDown = False
     playerRect.topleft = (WINDOWHEIGHT / 2),(WINDOWWIDTH / 2)


     for event in pygame.event.get():
          if event.type == QUIT:
               terminate()


          if event.type == KEYDOWN:
                     if event.key == ord('a'):
                         moveRight = False
                         moveLeft = True
                     if event.key == ord('d'):
                         moveLeft = False
                         moveRight = True
                     if event.key == ord('w'):
                         moveDown = False
                         moveUp = True
                     if event.key == ord('s'):
                         moveUp = False
                         moveDown = True

                     if event.type == KEYUP:
                         if event.type == K_ESCAPE:
                             terminate()
                         if event.key == ord('a'):
                              moveLeft = False
                         if event.type == ord('d'):
                              moveRight = False
                         if event.key == ord('w'):
                              moveUp = False
                         if event.key == ord('s'):
                              moveDown = False


     if moveLeft and playerRect.left > 0:
          playerRect.move_ip(-1 * MOVERATE,0)
     if moveRight and playerRect.right < WINDOWWIDTH:
          playerRect.move_ip(MOVERATE,0)
     if moveUp and playerRect.top >0:
          playerRect.move_ip(0,-1 * MOVERATE)
     if moveDown and playerRect.bottom < WINDOWHEIGHT:
          playerRect.move_ip(0,MOVERATE)

     WindowSurface.blit(playerImage,playerRect)
     pygame.display.update()
     mainClock.tick(30)

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    我怀疑问题在于您每次通过循环都重置 playerRect 的位置。将 move_* = false 行和 playerRect.topleft 行移到 while 循环之前:

    moveLeft = moveRight = moveUp = moveDown = False
    playerRect.topleft = (WINDOWHEIGHT / 2),(WINDOWWIDTH / 2)
    
    while True:
    
         for event in pygame.event.get():
              if event.type == QUIT:
                   terminate()
    

    如果这不能解决问题,请追踪位置。在每次循环迭代中,将 playerRect 的坐标打印到日志文件中。还要跟踪关键事件。这将区分显示失败和定位失败。

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多