我正在用pygame製作自上而下的益智遊戲,而且我的運動一直很奇怪。會發生什麼情況是,當我按下一個水平移動的按鍵並且一個按鍵立即垂直移動時,只有當它接觸水平表面時,角色纔會移動。這裏是我的運動和碰撞代碼:Pygame - 運動不一致
def move(self, dx, dy):
self.dx = dx
self.dy = dy
if self.dx != 0:
self.move_single_axis(self.dx, 0)
if self.dy != 0:
self.move_single_axis(0, self.dy)
def move_single_axis(self, dx, dy):
self.dx = dx
self.dy = dy
self.rect.x += self.dx
self.rect.y += self.dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if self.dx > 0:
self.rect.right = wall.rect.left
if self.dx < 0:
self.rect.left = wall.rect.right
if self.dy > 0:
self.rect.bottom = wall.rect.top
if self.dy < 0:
self.rect.top = wall.rect.bottom
def checkMovement(self):
if self.paused == False:
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
self.move(5, 0)
elif keys[pygame.K_a]:
self.move(-5, 0)
elif keys[pygame.K_w]:
self.move(0, -5)
elif keys[pygame.K_s]:
self.move(0, 5)
謝謝!
請發表[仍可產生的誤差最小的例子,我們可以直接複製粘貼和運行( http://stackoverflow.com/help/mcve)。 – skrx
問題是,我的程序非常非常大。 – Will6316
如果你不提供mcve,你不可能得到幫助。沒有一個好例子,找到錯誤可能很困難,甚至是不可能的。從頭開始,只需添加一個簡單的while +事件循環,牆壁和玩家類以及移動代碼。 – skrx