So there you go, I wanted to experiment a little pygame but I find myself stuck.
Context
I created a small sprite (with Piskelapp) that represents the player and looks like:
player.png
then add my background in jpg format. However when launching the game, my sprite is cut by the background as follows:
The ship is not placed in front of the background and more I go up it, more it disappears behind the background…
Here is my code:
JavaScript
x
49
49
1
import pygame
2
pygame.init()
3
4
# class user
5
6
class Player(pygame.sprite.Sprite):
7
def __init__(self):
8
super().__init__()
9
self.pv = 100
10
self.__max_health = 100
11
self.attack = 2
12
self.velocity = 5
13
self.image = pygame.image.load('assets/player.png')
14
self.rect = self.image.get_rect()
15
self.rect.x = 400
16
self.rect.y = 500
17
18
# open game window
19
pygame.display.set_caption("Rocket 'n' Rock")
20
screen = pygame.display.set_mode((1080, 720))
21
22
# background import
23
bg = pygame.image.load('assets/bg.jpg')
24
25
# load player
26
27
player = Player()
28
29
running = True
30
31
# game mainloop
32
while running:
33
34
# bg apply
35
screen.blit(bg, (0,-400))
36
# screen update
37
pygame.display.flip()
38
39
# player image apply
40
screen.blit(player.image, player.rect)
41
42
# if player close the window
43
for event in pygame.event.get():
44
# *close event
45
if event.type == pygame.QUIT:
46
running = False
47
pygame.quit()
48
print("close game")
49
anyone have a tip? I’m wondering if it’s not a file format problem? thank you for your time
Advertisement
Answer
In you game mainloop, you should put:
JavaScript
1
2
1
screen.blit(player.image, player.rect)
2
before:
JavaScript
1
2
1
pygame.display.flip()
2