I am doing a remake of doodle jump. The problem i have encountered is that the direction at which the object is facing gets iterated over. What I mean by this, is something like this: If the direction is equal to 'right', but, you decided to change the direction further through the loop, so the direction should become 'left', but since the loop starts from the first line, it just almost immediately changes back to direction 'right'.
like so: loop: right if something happens: left
The problem is that the loop starts from line one, so it immediately makes it back to right
Here is the code:
class Player():
def __init__(self, x, y):
self.direction = -1
player_img = pygame.image.load('player1.png')
if self.direction == 1:
self.player = pygame.transform.scale(player_img, (100, 100))
if self.direction == -1:
player = pygame.transform.scale(player_img, (100, 100))
self.player = pygame.transform.flip(player, True, False)
self.rect = self.player.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_y = 0
self.jumped = False
def update(self):
dx = 0
dy = 0
# Key windings
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
dx -= 5
self.direction = -1
if key[pygame.K_RIGHT]:
dx += 5
self.direction = 1