Joseph
Joseph

Reputation: 35

How do I get pygame to display my sprites?

I am unsure where the problem is or if more code is needed to see what's wrong

This is how I am calling the sprites:

walkRight = [pygame.image.load('sprites/R1.png'), pygame.image.load('sprites/R2.png'), pygame.image.load('sprites/R3.png'), pygame.image.load('sprites/R4.png'), pygame.image.load('sprites/R5.png'), pygame.image.load('sprites/R6.png'), pygame.image.load('sprites/R7.png'), pygame.image.load('sprites/R8.png'), ]
walkLeft = [pygame.image.load('sprites/L1.png'), pygame.image.load('sprites/L2.png'), pygame.image.load('sprites/L3.png'), pygame.image.load('sprites/L4.png'), pygame.image.load('sprites/L5.png'), pygame.image.load('sprites/L6.png'), pygame.image.load('sprites/L7.png'), pygame.image.load('sprites/L8.png')]
bg = pygame.image.load('sprites/bg.jpg')
char = pygame.image.load('sprites/standing.png')

All the files are in folder Gmaepy spelling was intentional in that folder contains the main.py and there is a sprites folder containing all the sprites. I am thinking it is maybe in my redrawGameWindow() function but I am unfamiliar with all this still

def redrawGameWindow():
    # We have 8 images for our walking animation, I want to show the same image for 3 frames
    # so I use the number 16 as an upper bound for walkCount because 24 / 3 = 8. 8 images shown
    # 3 times each animation.
    global walkCount

    win.blit(bg, (0, 0)) # Draws the background image at (0,0)
    if walkCount + 1 >= 24:
        walkCount = 0

        if left:
            win.blit(walkLeft[walkCount//3], (x, y))
            walkCount += 1
        elif right:
            win.blit(walkRight[walkCount//3], (x, y))
            walkCount += 1
        else:
            win.blit(char, (x, y))
            walkCount = 0

    pygame.display.update()

When I run the program in powershell the background gets displayed but the character sprite is nowhere to be seen. It was working fine when I was displaying a rectangle but I am having trouble with the sprites

Upvotes: 2

Views: 71

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

It is a matter of Indentation. You must blit the sprites in any case:

def redrawGameWindow():
    # We have 8 images for our walking animation, I want to show the same image for 3 frames
    # so I use the number 16 as an upper bound for walkCount because 24 / 3 = 8. 8 images shown
    # 3 times each animation.
    global walkCount

    win.blit(bg, (0, 0)) # Draws the background image at (0,0)
    if walkCount + 1 >= 24:
        walkCount = 0

    # INDENTAITON
    #<--|

    if left:
        win.blit(walkLeft[walkCount//3], (x, y))
        walkCount += 1
    elif right:
        win.blit(walkRight[walkCount//3], (x, y))
        walkCount += 1
    else:
        win.blit(char, (x, y))
        walkCount = 0

    pygame.d

Upvotes: 1

Related Questions