0

I currently am working on a 'Flappy Bird' remake in Pygame using Python 3.2. I thought it would be good for practice, and relativly simple. However, it is proving to be hard. Currently, I am having a problem when drawing a rectangle at different heights but keeping the rectangle at the height it is set to.

Here is my Pipe class

class Pipe:
    def __init__(self,x):
        self.drawn = True
        self.randh = random.randint(30,350)
        self.rect = Rect((x,0),(30,self.randh))

    def update(self):
        self.rect.move_ip(-2,0)

    def draw(self,screen):
        self.drawn = True
        pygame.draw.rect(screen,(0,130,30),self.rect)

My while Loop is as follows:

while True:
    for event in pygame.event.get():
        movey = +0.8
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                movey = -2


    x += movex
    y += movey


    screen.blit(background,(0,0))
    screen.blit(bird,(x,y))

    Pipe1 = Pipe(scrollx)

    if Pipe1.drawn == True:
        Pipe1.update()
    else:
        Pipe1 = Pipe(scrollx)
        Pipe1.draw(screen)

    scrollx -= 0.3

    pygame.display.update()

I have being wrestling with this code for over a week, and I really appreciate any help you can give.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Harvey
  • 384
  • 5
  • 15

2 Answers2

1

I'm not following the logic of this part:

Pipe1 = Pipe(scrollx)

if Pipe1.drawn == True:
    Pipe1.update()
else:
    Pipe1 = Pipe(scrollx)
    Pipe1.draw(screen)

The drawn attribute is set to True at the constructor, so when do you expect the else condition to be triggered? Remember you are recreating this pipe every frame.

Have you tried drawing the pipe the same you way you did with the bird?

Edit: suggestion for you for loop:

PIPE_TIME_INTERVAL = 2

pipes = []    # Keep the pipes in a list.
next_pipe_time = 0

while True:
    [... existing code to handle events and draw the bird ...]

    for pipe in pipes:
        pipe.move(10)     # You'll have to write this `move` function.
        if pipe.x < 0:    # If the pipe has moved out of the screen...
            pipes.pop(0)  # Remove it from the list.

    if current_time >= next_pipe_time:   # Find a way to get the current time/frame.
        pipes.append(Pipe())  # Create new pipe.
        next_pipe_time += PIPE_TIME_INTERVAL  # Schedule next pipe creation.
BoppreH
  • 8,014
  • 4
  • 34
  • 71
  • What I intended to happen is for the drawn variable to be set to False, at which point a pipe would be drawn, then as it is True, it would be moved not drawn – Harvey Feb 14 '14 at 18:56
  • And the previously created pipe would stop moving? I think you should use a timer for that, not an attribute of Pipe. – BoppreH Feb 14 '14 at 18:58
  • Ive got slightly further now by moving the creation outise of the while loop. Now, all I have to do is work out how to loop when Pipe1's x = 0... – Harvey Feb 14 '14 at 19:11
  • I've edited the answer to include a suggestion on how to handle more than one pipe. – BoppreH Feb 14 '14 at 19:17
0

You are creating a new Pipe on every loop, but never hang on to the old one(s), so you get a new random height each time. Move this line:

Pipe1 = Pipe(scrollx)

outside the while loop. Better yet, have a list of pipes you can add new ones to and easily update them all. You never set self.drawn = False within Pipe either.

Also, you are resetting movey for every event, try:

movey = 0.8 # no need for plus 
for event in pygame.event.get():
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437