1
votes

I'm learning how to code games in pygame and I wrote a simple pygame code that loads a background and draws a player sprite. I drew the background image, only to draw the player afterwards, so the image doesn't overlap with the player image, and then called pygame.display.flip() to flip the screen. It still doesn't work, why? I pasted the images used below

enter image description here enter image description here

import pygame

pygame.init()

black = (0, 0, 0)
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
FPS = 60

background_img = pygame.image.load("environment_forest_alt1.png")
backgroundimg_rect = background_img.get_rect()
player_img_idle = pygame.image.load("adventurer-idle-00.png")
player_img_run = pygame.image.load("adventurer-run-00.png")
player_img_attack = pygame.image.load("adventurer-attack1-01.png")
player_img_attack2 = pygame.image.load("adventurer-attack1-02.png")
player_img_attack3 = pygame.image.load("adventurer-attack1-03.png")
player_img_attack4 = pygame.image.load("adventurer-attack1-04.png")
player_img_attacks = [player_img_attack, player_img_attack2, player_img_attack3, player_img_attack4]

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(player_img_idle, (200, 100))
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()
        self.rect.x = 10
        self.rect.y = height - 10

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

exitGame = False
while not exitGame:
    clock.tick(FPS)
    screen.blit(background_img, backgroundimg_rect)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitGame = True

    all_sprites.draw(screen)
    pygame.display.flip()
pygame.quit()
quit()
1
I tried to reproduce the problem, by using two images I made myself, in Paint, however, the "player" is still visible. If you post the two images used, player_img_idle, and background_img, I will try reproduce the problem, again. However, you could try restructuring your while loop, to look more like regular pygame code: do all event handling first (for event in pygame.ev...), then render the background image and sprite, flip the screen, and then call clock.tick(FPS). - Micheal O'Dwyer
@MichealO'Dwyer Posted the images! - Lol Lol
I believe you have to blit sprites, not draw them? - user10030086

1 Answers

4
votes

You just draw the player sprite mostly outside the screen. You would see it if the player image had not so much empty space at the top.

Just change the line

self.rect.y = height - 10

to

 self.rect.y = height - 100

or even

self.rect.y = height - self.rect.height