目前,我正在使用所有四个键来控制蛇左、右、上、下。我想知道如何只使用左右键来移动蛇。
if event.key == pygame.K_LEFT:
snake.direction = 2
elif event.key == pygame.K_RIGHT:
snake.direction = 3
elif event.key == pygame.K_UP:
snake.direction = 0
elif event.key == pygame.K_DOWN:
snake.direction = 1
def move(self):
if self.direction is 0:
self.dy = -self.block
self.dx = 0
if self.direction is 1:
self.dy = self.block
self.dx = 0
if self.direction is 2:
self.dy = 0
self.dx = -self.block
if self.direction is 3:
self.dy = 0
self.dx = self.block
self.x += self.dx
self.y += self.dy
谁能指导我该怎么做?
if event.key == pygame.K_LEFT:
if snake.direction == 0
snake.direction = 2
elif snake.direction == 2
snake.direction = 1
elif snake.direction == 1
snake.direction = 3
elif snake.direction == 3
snake.direction = 0
elif event.key == pygame.K_RIGHT:
if snake.direction == 0
snake.direction = 3
elif snake.direction == 3
snake.direction = 1
elif snake.direction == 1
snake.direction = 2
elif snake.direction == 2
snake.direction = 0
def move(self):
if self.direction is 0:
self.dy = -self.block
self.dx = 0
if self.direction is 1:
self.dy = self.block
self.dx = 0
if self.direction is 2:
self.dy = 0
self.dx = -self.block
if self.direction is 3:
self.dy = 0
self.dx = self.block
self.x += self.dx
self.y += self.dy
这应该会根据蛇之前行进的方向来旋转蛇。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)