Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Objects with Pygame
#1
G'day world,
So, I have a school assessment where we have to create a Flappy Bird game on Pygame. I am doing quite well so-far but I have came to a roadblock. When I generate the pipes, only the very last pipe becomes an object. I know why this is, but don't have a clue on how to fix it. I have tried putting them in lists but I get an error message. This is the code:
pipex = 600
pipesinfo = []
pipegap = 250
pipelast = 0
for something in range(10):
        direction = random.choice(["up", "down"])
        if direction == "up":
                pipey = random.randint(250, 500)
        if direction == "down":
                pipey = random.randint(-500, -250)
        pipex = pipex + pipegap
        deff = (pipex, pipey, direction)
        pipesinfo.append(deff)


#Show the pics
screen.blit(background_image, (0, 0))
birdobj = screen.blit(bird, (birdx, birdy))
#pipeobj = []
#for pipex, pipey, direction in pipesinfo:

#    if direction == "down":
#        pipeobj = screen.blit(down, (pipex, pipey))

#    elif direction == "up":
#        pipeobj = screen.blit(pipe, (pipex, pipey))
    
    
The commented lines is where I feel the problem is. I think this because when ever it blits, it over rides the pipeobj object. (I think)

If anyone can help it would be greatly appreciated because I have been banging my head Wall trying to figure this out.

Thanks :)
Reply
#2
pipeobj is a list, surface.blit returns a pygame.Rect object. So when you write:

pipeobj = screen.blit(down, (pipex, pipey))
you are re-assigning the list to be a rect, and obviously this will take the values of the last rect. If you want to keep it as a list write;


pipeobj.append(screen.blit(down, (pipex, pipey)))
Does this help?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020