Reputation: 73
I have this code that runs fine but the only problem is that the player's image is behind the other images so you can't see where the player is. How do I make it so the player's image is in front of that image like a background layer. And how do I also make other images appear behind the other background images. Like for example I have this background tree:
class Tree(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('tree.png')
self.rect = self.image.get_rect()
And I have the player's class:
class Player(pygame.sprite.Sprite):
change_x = 0
change_y = 0
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('mapMover.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def changespeed_x(self,x):
self.change_x = x
def changespeed_y(self,y):
self.change_y = y
def update(self, barrier_list, Upcar_list, Downcar_list):
self.rect.x += self.change_x
barrier_hit_list = pygame.sprite.spritecollide(self, barrier_list, False)
Upcar_hit_list = pygame.sprite.spritecollide(self, Upcar_list, False)
Downcar_hit_list = pygame.sprite.spritecollide(self, Downcar_list, False)
for barrier in barrier_hit_list:
if self.change_x > 0:
self.rect.right = barrier.rect.left
else:
self.rect.left = barrier.rect.right
for Upcar in Upcar_hit_list:
if self.change_x > 0:
self.rect.right = Upcar.rect.left
Upcar.rect.x += 3
else:
self.rect.left = Upcar.rect.right
Upcar.rect.x -= 3
return
for Downcar in Downcar_hit_list:
if self.change_x > 0:
self.rect.right = Downcar.rect.left
Downcar.rect.x += 3
else:
self.rect.left = Downcar.rect.right
Downcar.rect.x -= 3
return
self.rect.y += self.change_y
barrier_hit_list = pygame.sprite.spritecollide(self, barrier_list, False)
Upcar_hit_list = pygame.sprite.spritecollide(self, Upcar_list, False)
Downcar_hit_list = pygame.sprite.spritecollide(self, Downcar_list, False)
for barrier in barrier_hit_list:
if self.change_y > 0:
self.rect.bottom = barrier.rect.top
else:
self.rect.top = barrier.rect.bottom
for Upcar in Upcar_hit_list:
if self.change_y > 0:
self.rect.top = Upcar.rect.bottom
Upcar.rect.y += 3
else:
self.rect.bottom = Upcar.rect.top
Upcar.rect.x -= 3
return
for Downcar in Downcar_hit_list:
if self.change_y > 0:
self.rect.top = Downcar.rect.bottom
Downcar.rect.y += 3
else:
self.rect.bottom = Downcar.rect.top
Downcar.rect.x -= 3
return
Python 2.6, Pygame Sprites, Windows 7
Upvotes: 0
Views: 8848
Reputation: 101142
You didn't show the relevant code, but the issue is clear:
The Surface
you blittet last to the screen is "on top". If you first draw your player sprite, and then your background image on top of it, you'll only the background image.
So you either have to draw your sprites in the right order, or, since you use the Sprite
class, but your sprites into a LayeredUpdates
group and give them a _layer
attribute.
Upvotes: 4
Reputation: 7855
As said above, you're drawing/blitting the player first, and then the background, in the mainloop.
Wrong:
while not done: # The main loop, it may be different in your code.
player.draw()
background.draw() # What you're probably doing.
Right:
while not done: # The main loop, it may be different in your code.
background.draw() # What you should do.
player.draw()
To be sure, just draw the background, no matter how you've implemented it, RIGHT AFTER the beginning of the main-loop's block. If you're updating/drawing your game in a separate procedure like:
def update():
background.draw() # What you should do.
player.draw()
while not done:
update()
BE SURE that you're drawing the background in the separate function before anything. Remember: BACKground, you should always draw it "at the back" of anything (previously/before) :).
Upvotes: 2