我正在学习python,我为玩家运动创建了代码,它不在课堂上时工作,但我在课堂上需要它。当我按 W 或 S 时,玩家只移动一次 vel = 5,然后它回到原来的坐标。如何解决?
right = False
left = False
class player:
def __init__(self, x, y, vel = 5, walkCount = 0):
self.x = x
self.y = y
self.vel = vel
self.walkCount = walkCount
def update(self):
if self.walkCount + 1 >= 40:
self.walkCount = 0
if right:
screen.blit(charRun[walkCount//5],(self.x, self.y))
self.walkCount += 1
elif left:
screen.blit(charBack[walkCount//5],(self.x, self.y))
self.walkCount += 1
else:
screen.blit(char,(self.x, self.y))
pygame.display.update()
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.x += self.vel
right = True
left = False
elif keys[pygame.K_s]:
self.x -= self.vel
left = True
right = False
else:
right = False
left = False
self.walkCount = 0
def redrawGameWindow():
screen.blit(bg, (0, 0))
screen.blit(car, (800,500))
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redrawGameWindow()
b = player(500,500)
b.move()
b.update()