Zelix
Zelix

Reputation: 45

Pygame sprites behaving incorrectly when moving left

Alright so following up to my previous question I've made some progress, game has the addition of left and right movement and a simple jump script, evolution woo :D

My latest addition was an Idle animation and now the dude can't move left :D I hope its not too silly I checked everything but can't point out the issue >.> ..

regardless here's the code, Thanks in advance, truly appreciative! (origination is awful srry >.<):

import pygame

pygame.init()

path = "C:/Users/user/Documents/Le game/"
win = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Potato ultra")
bg = pygame.image.load(path + "BG.jpg")
walk_right = [pygame.image.load("C:/Users/user/Documents/Le game/R2.png"), pygame.image.load("C:/Users/user/Documents/Le game/R3.png"), pygame.image.load("C:/Users/user/Documents/Le game/R4.png"), pygame.image.load("C:/Users/user/Documents/Le game/R5.png"), pygame.image.load("C:/Users/user/Documents/Le game/R6.png"), pygame.image.load("C:/Users/user/Documents/Le game/R7.png"), pygame.image.load("C:/Users/user/Documents/Le game/R8.png"), pygame.image.load("C:/Users/user/Documents/Le game/R9.png")]
walk_left = [pygame.image.load("C:/Users/user/Documents/Le game/L2.png"), pygame.image.load("C:/Users/user/Documents/Le game/L3.png"), pygame.image.load("C:/Users/user/Documents/Le game/L4.png"), pygame.image.load("C:/Users/user/Documents/Le game/L5.png"), pygame.image.load("C:/Users/user/Documents/Le game/L6.png"), pygame.image.load("C:/Users/user/Documents/Le game/L7.png"), pygame.image.load("C:/Users/user/Documents/Le game/L8.png"), pygame.image.load("C:/Users/user/Documents/Le game/L9.png")]
Static = pygame.image.load("C:/Users/user/Documents/Le game/Idle.png")

SW = 800
SH = 700
x = 0
y = 480
width = 64
height = 64
vel = 20
isJump = False
MoveLeft = False
MoveRight = False
Idle = False
JumpCount = 10
walkCount = 0

def redrawGameWindow():

    win.blit(bg, (0,0))
    global walkCount
    if not Idle:
        if MoveRight:
            if walkCount <= 7:
                win.blit(walk_right[walkCount], (x, y))
            elif walkCount > 7:
                walkCount = 0
                win.blit(walk_right[walkCount], (x, y))
        if MoveLeft:
            if walkCount <= 7:
                win.blit(walk_left[walkCount], (x, y))
            elif walkCount > 7:
                walkCount = 0
                win.blit(walk_left[walkCount], (x, y))
    else:
        win.blit(Static, (x, y))

    pygame.display.update()


run = True
while run:
    pygame.time.delay(50)

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

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 0:
        Idle = False
        MoveRight = False
        MoveLeft = True
        x -= vel
        walkCount += 1
    if keys[pygame.K_RIGHT] and x < SW - width:
        Idle = False
        MoveRight = True
        MoveLeft = False
        x += vel
        walkCount += 1
    else:
        Idle = True

    if not isJump:
        if keys[pygame.K_UP] and y > 0:
            y -= vel
            if y < 0:
                y = 0
        if keys[pygame.K_DOWN] and y < SH - height:
            y += vel
            if y > 636:
                y = 636
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if JumpCount >= -10:
            y -= (JumpCount * 4)
            if y < 0:
                y = 0
            JumpCount -= 2
        else:
            isJump = False
            JumpCount = 10

    redrawGameWindow()

pygame.quit()

Upvotes: 3

Views: 43

Answers (1)

paxdiablo
paxdiablo

Reputation: 881523

Well, the first thing I would do would be to add the following code (before the final line below, which you already have):

print("DEBUG1 keyl =", keys[pygame.K_LEFT], "x =", x)
print("DEBUG2 keyr =", keys[pygame.K_RIGHT], "SW =", Sw, "wid =", width)
print("DEBUG3 mover =", MoveRight, "movel =", MoveLeft)
print("DEBUG4 vel =", vel, "walkc =", walkCount)
print("DEBUG5 ==========")
if keys[pygame.K_LEFT] and x > 0:

That way, you'll see all the variables that take part in deciding left and right moves, and it will hopefully become obvious what is preventing the left move from functioning.


Based on a slightly deeper look at your code, it appears to be this bit:

if keys[pygame.K_LEFT] and x > 0:
    Idle = False
    MoveRight = False
    MoveLeft = True
    x -= vel
    walkCount += 1
if keys[pygame.K_RIGHT] and x < SW - width:
    Idle = False
    MoveRight = True
    MoveLeft = False
    x += vel
    walkCount += 1
else:
    Idle = True

Since that else belongs only to the second if statement, it will fire whenever the right key is not being pressed, regardless of whether you're pressing the left key.

I suspect you can fix this simply by changing it from an if, if, else sequence to an if, elif, else sequence, so that the else fires only if neither of the keys are pressed.


A couple of possible improvements to your code:

  • you can get rid of all that walkCount checking and adjusting in the redraw function by simply using walkCount = (walkCount + 1) % 8 in the event loop - this will ensure it wraps from seven to zero without further effort.

  • you don't appear to have limiters on the x value. For example, if x == 5 and vel == 10, it's possible that a left move will set x = -5 which may not be desirable. You have more knowledge of the game play than me so I could be wrong, but it's worth checking.

  • you may not need both MoveLeft and MoveRight. The Idle flag decides whether you're moving or not so, if you are moving, it's either left or right. So a Idle/MoveRight combo should be enough for the code to figure out what to do. Again, this is a gameplay issue, something to look at but I may be incorrect.

  • not sure how your sprites look when they jump but you may be better off using constant acceleration formulae for calculating y position. I've answered similar questions before so you can have a look at this answer for guidance.

Upvotes: 3

Related Questions