pythonpython-3.xpygameflappy-bird-clone

Why my upper pipe is not appearing in PyGame?


Was just tryin' to make a Flappy Bird clone in PyGame but the upper pipe did not appear for some reason. I Did update the screen, nothing is just appearing. It goes invisble after the first go that it reaches a x lower than 0 and it resets the position, or we could just say it goes invisible after the first reset. Please someone help me

import pygame
import random

WIDTH, HEIGHT = 600, 700
BG_COLOR = (100,255,255)

PLAYER_WIDTH = PLAYER_HEIGHT = 50
PLAYER_COLOR = (255, 255, 50)
PLAYER_SPEED = 1

PIPE_COLOR = (100, 255, 100)
PIPE_WIDTH, PIPE_HEIGHT = 50, 300

velocity = 0

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flat Bird")



def draw(player, pipe_up, pipe_down):
    pygame.draw.rect(WIN, PLAYER_COLOR, player)
    pygame.draw.rect(WIN, PIPE_COLOR, pipe_up)
    pygame.draw.rect(WIN, PIPE_COLOR, pipe_down)
    pygame.display.update()



def move_player(player,speed,velocity):
    keys=pygame.key.get_pressed()
    velocity -= speed
    if keys[pygame.K_SPACE]:
        velocity = 1
    player.y -= velocity
    print(velocity)



def generate_pipe_pos(pipe_up, pipe_down):
    offset = random.randint(-40, 40)
    pipe_up.height = -40
    pipe_down.height = 440
    pipe_up.x = 600
    pipe_down.x = 600





def main():
    player = pygame.Rect(100, 350, PLAYER_WIDTH, PLAYER_HEIGHT)
    pipe_up = pygame.Rect(600, -40, PIPE_WIDTH, PIPE_HEIGHT)
    pipe_down = pygame.Rect(600, 440, PIPE_WIDTH, PIPE_HEIGHT)

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        move_player(player, PLAYER_SPEED, velocity)
        pipe_up.x -= PLAYER_SPEED
        pipe_down.x -= PLAYER_SPEED
        if pipe_up.x < 0:
                generate_pipe_pos()
        draw(player, pipe_up, pipe_down)
        WIN.fill(BG_COLOR)
    pygame.quit()

main()

I Tried to do it the same way I did it with the lower pipe and I expected it to just work fine and show itself.


Solution

  • If you put the pipe at the beginning, then change the height of the pipe to -40. Likely you wanted to set the y coordinate instead of the height:

    def generate_pipe_pos(pipe_up, pipe_down):
        offset = random.randint(-40, 40)
        pipe_up.y = -40
        pipe_down.y = 440
        pipe_up.x = 600
        pipe_down.x = 600