Reputation: 153
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
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()
Upvotes: 1
Views: 284
Reputation: 101042
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
Upvotes: 4