How to make the red block appear between grey and green block.
In the above picture i changed my rectangle x position of donkey to 160 then only i can able to see the red block if not means i'll hide under grey block.
this is my code
class Road(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((200, 500))
self.image.fill(grey)
self.rect = self.image.get_rect()
self.rect.centerx = width/2
#self.rect.left = width-200
#print self.rect.left
self.speedy = 5
def update(self):
self.rect.y += self.speedy
if self.rect.y > 0:
self.rect.y = 0
class Donkey(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((100, 100))
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.x = 200
print 'x:',self.rect.x
self.rect.y = -54
print 'y:',self.rect.y
self.speedy = random.randint(8,10)
def update(self):
self.choice = (200, 300)
self.rect.y += self.speedy
if self.rect.top > height:
self.rect.x = random.choice(self.choice)
print 'x:', self.rect.x
self.rect.y = random.randrange(-100, -40)
print 'y', self.rect.y
self.speedy = random.randint(8,10)
class Race_car(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((100, 100))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.x = 200
self.rect.bottom = height
self.speedy = -0.1
def update(self):
#self.speedx = 0
self.rect.y += self.speedy
keypressed = pygame.key.get_pressed()
if keypressed[pygame.K_RIGHT]:
self.rect.x = 300
elif keypressed[pygame.K_LEFT]:
self.rect.x = 200
#Group, object for game_hearth
game_hearth = pygame.sprite.Group()
road = Road()
game_hearth.add(road)
#Group, object for donkey
donkey = Donkey()
donkeys = pygame.sprite.Group()
game_hearth.add(donkey)
donkeys.add(donkey)
#Group, object for racecar
racecar = Race_car()
racecars = pygame.sprite.Group()
game_hearth.add(racecar)
racecars.add(racecar)
when i run my code after commenting the Race_car class the red blocks working fine, see
so the thing is i want to display the both red and green block above the grey road. so how to do it without commenting any class in my program. i know its some co-ordinate mistake but i don't know where to change it.
From the Pygame docs :
The Group does not keep sprites in any order, so the draw order is arbitrary.
You should probably switch to pygame.sprite.OrderedUpdates :
This class derives from pygame.sprite.RenderUpdates(). It maintains the order in which the Sprites were added to the Group for rendering. This makes adding and removing Sprites from the Group a little slower than regular Groups.
#Group, object for game_hearth
game_hearth = pygame.sprite.OrderedUpdates()
road = Road()
game_hearth.add(road)
#Group, object for donkey
donkey = Donkey()
donkeys = pygame.sprite.OrderedUpdates()
game_hearth.add(donkey)
donkeys.add(donkey)
#Group, object for racecar
racecar = Race_car()
racecars = pygame.sprite.OrderedUpdates()
game_hearth.add(racecar)
racecars.add(racecar)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.