Nico Zinnendorf
Nico Zinnendorf

Reputation: 3

Pygame - Background doesnt work

im having a hard time making my first steps in pygame. I just wanna move a Picture around in front of a jpg-background.

I googled several times for a solution and tried different things .. and this is the "nearest" i can come up with:

(but it still doenst work ..)

Any ideas?

Thanks in advance!

Nico

import pygame

pygame.init()

display_width = 1600
display_height = 900

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

Player_width = 73

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
background = pygame.surface(screen.get_size())

PlayerImg = pygame.image.load("C:\python\warrior1.png")
background_image = pygame.image.load("depositphotos_159346790-stock-photo-
forest-and-stones-2d-game.jpg")

background.blit(background_image)
screen.blit(background (0,0))

def Player(x,y):
gameDisplay.blit(PlayerImg,(x,y))



def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)

x_change = 0
y_change = 0

gameExit = False

while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

        if event.type == pygame.KEYDOWN:
            [...]

    x += x_change
    y += y_change

    Player(x,y)

    if x > display_width - Player_width or x < 0:
        gameExit = True

    pygame.display.update()
    clock.tick(60)

game_loop()
pygame.quit()
quit()

Upvotes: 0

Views: 165

Answers (1)

moppi
moppi

Reputation: 11

Answer:

  • delete background = pygame.surface(screen.get_size())

  • in player replace gameDisplay with screen

  • replace background.blit(background_image) screen.blit(background (0,0)) with screen.blit(background_image, (0,0))

  • add screen.blit(background_image, (0,0)) also in the while Loop


Helpful Tips for Pygame:

Look at error messages and post them too.

Use *.png files in pygame. They can be transparent. You see no edges when the pictures overlap.

Watch Sentdex's Pygame Tutorial again.

Upvotes: 1

Related Questions