Im learning python i created code for player movement, it works when it isnt in class, but i need it in class. when i press W or S the player moves only once by vel = 5 and then it comes back to its original coordinates. How to fix it ?
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()