1

I tried to show a character in my window at a certain point. What am I doing wrong?

This is for my first game and I am new to pygame. I tried the code, that google gave me.

screenwidth = 800
screenheight = 600

win = pygame.display.set_mode((screenwidth, screenheight))

bg = pygame.image.load("bg.png")
char = pygame.image.load("char.png")

x = 400
y = 300
vel = 5

isJumping = False
running = True

clock = pygame.time.Clock()


def redrawBackground():
    win.blit(bg, (0, 0))    
    pygame.display.update()

def player(x, y):
    win.blit(char, (x, y))

while running:
    clock.tick(60)
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        player_one_x -= vel

    if keys[pygame.K_RIGHT]:
        player_one_x += vel

    if not(isJumping):
        if keys[pygame.K_SPACE]:  
            for i in range(10):
                player_one_y -= vel

        isJumping = True
    player(x, y)
    redrawBackground()

I expect the result of this code to show the file named "char.png", but it didn't show up.

MrR-Py
  • 13
  • 2
  • If you are getting an error, please add that error. Also, the link from google might be some help. –  Sep 11 '19 at 16:03
  • Are the image files in the working directory? Note, the working directory is not the directory of the python source file. See [Cannot open image in pygame](https://stackoverflow.com/questions/57766969/cannot-open-image-in-pygame-on-android/57767162#57767162) – Rabbid76 Sep 11 '19 at 16:23

1 Answers1

0

Your code is drawing the player, then over-writing it with the background:

while running:
    #...
    player(x, y)
    redrawBackground()

This might be better as:

def redrawBackground():
    win.blit(bg, (0, 0))    
    # removed the call to pygame.display.update()

and

while running:
    #...
    redrawBackground()
    player(x, y)
    pygame.display.update()
Kingsley
  • 14,398
  • 5
  • 31
  • 53