1
votes

I created a game called JumpyMan. I created game mechanics like moving right and left, jumping and falling down. Because the game was not that good looking I added some character animation(sprites), when you press Key D , the animation starts and character is moving in right direction.

But, when i press Key A, the character moves in left direction but the animation is not starting. When i press both keys in the same time, the left animation starts but my character stands still.

What i could do to fix this bug?

Here's my code: https://github.com/zewutz/jumpyman ( All the images needed are in github repo )

import pygame
import os
from pygame import Surface
from pygame.transform import scale


pygame.init()
#Screen, title/icon and clock
screen = pygame.display.set_mode((1366,650))
pygame.display.set_caption("JumpyMan")
iconImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
iconImg = pygame.transform.scale(iconImg,(100,128))
pygame.display.set_icon(iconImg)
clock = pygame.time.Clock()

#backgroung
backgroundImage = pygame.image.load('resources\images\Imgbackground\jumpymanbackground.jpg')
backgroundCoord = (0,-350)

#Player sprites 
left = [None]*10 
for picIndex in range(1,10):
    left[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgleft', "L" + str(picIndex) + ".png"))
    picIndex += 1

right = [None]*10
for picIndex in range(1,10):
    right[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgright', "Run__00" + str(picIndex) + ".png"))
    picIndex += 1

# Player
playerImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
smallerImage = pygame.transform.scale(playerImg, (50,100))
x = 10
y = 460
vel_x = 5
vel_y = 15
jump = False
move_left = False
move_right = False
stepIndex = 0

def characterMovementX():
    global stepIndex

    if stepIndex >= 9:
        stepIndex = 0

    if move_left:
        screen.blit(left[stepIndex], (x,y))
        stepIndex += 1
    elif move_right:
        screen.blit(right[stepIndex], (x,y))
        stepIndex += 1
    else:
        screen.blit(smallerImage,(x,y))


#Game loop
game = True
while game:
    screen.blit(backgroundImage,backgroundCoord)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game = False
    

    #Move left, right
    userInput = pygame.key.get_pressed()
    if userInput[pygame.K_a] and x > 5 :
        move_left = True
        x -= vel_x
    if userInput[pygame.K_d] and x < 1280:
        move_right = True
        x += vel_x

    else:
        move_left = False
        move_right = False
        stepIndex = 0
    

    #Jump
    if jump is False and userInput[pygame.K_SPACE]:
        jump = True
    if jump:
        y -= vel_y
        vel_y -= 1
        if vel_y < -15:
            jump = False
            vel_y = 15


    characterMovementX()
    clock.tick(30)
    pygame.display.update()

pygame.quit()
1

1 Answers

1
votes

The bug is caused by the if - else statement for the movement to the right:

if userInput[pygame.K_a] and x > 5 :
   move_left = True
   x -= vel_x
if userInput[pygame.K_d] and x < 1280:
   move_right = True
   x += vel_x
else:
   move_left = False
   move_right = False
   stepIndex = 0

If the player doesn't move to the right, the else case is executed and move_left is set to Flase, even when it was set True immediately before.

You have to a if - elif - else statement:

if userInput[pygame.K_a] and x > 5 :
    move_left = True
    move_right = False
    x -= vel_x

elif userInput[pygame.K_d] and x < 1280:
    move_left = False
    move_right = True
    x += vel_x

else:
    move_left = False
    move_right = False
    stepIndex = 0

I recommend to set move_left = False and move_right = False before the selection:

move_left = False
move_right = False

if userInput[pygame.K_a] and x > 5 :
    move_left = True
    x -= vel_x
elif userInput[pygame.K_d] and x < 1280:
    move_right = True
    x += vel_x
else:
    stepIndex = 0