-3

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()
360yogi
  • 3
  • 2
  • 1
    Well, you're creating a brand-new `player()` object, at the specific coordinates (500,500), every iteration of your mainloop. Also, `.move()` is only changing *local variables* `right` and `left` - entirely unrelated to the global variables of the same name that are being used elsewhere. – jasonharper Jun 27 '20 at 18:07

2 Answers2

0

As recommended by @jasonharper, you need to create your player only once at the start and more the right and left variables inside your class.

class player:    
    def __init__(self, x, y, vel = 5, walkCount = 0):
        self.x = x
        self.y = y
        self.vel = vel
        self.walkCount = walkCount
        self.right = False
        self.left = False
    
    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
            self.right = True
            self.left = False
        elif keys[pygame.K_s]:
            self.x -= self.vel
            self.left = True
            self.right = False
        else:
            self.right = False
            self.left = False
            self.walkCount = 0

def redrawGameWindow():
    screen.blit(bg, (0, 0))
    screen.blit(car, (800,500))

run = True
b = player(500,500)
while run:
    clock.tick(60)    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redrawGameWindow()
    b.move()
    b.update()
rob_m
  • 321
  • 1
  • 9
0

Firstly, to check what is wrong you should post most of your code, so we can try testing it, and debugging it. Secondly, classes in programming is really useful but you shouldn't do anything that doesn't make sens in your code. Classes are there to help you make a lot of objects but also to maintain the stability of your code.

So all your code should make sens. For example in you code you let the player update the display. The display.update method would be better in your while loop since the Player shouldn't control whenever you update your screen. Imagin later on you add content, but your code doesn't work because the display isn't updating...

Anyways, lets come back to the code. You can see here that you create your object in the while loop. So everytime you do the loop, you create a new object with the default values :

b = player(500,500)

Thus, every while cycle this function executes :

def __init__(self, x, y, vel = 5, walkCount = 0):
    self.x = x
    self.y = y
    self.vel = vel
    self.walkCount = walkCount

And so x and y stay at 500.

vlizana
  • 2,962
  • 1
  • 16
  • 26