I am currently trying to make a pygame version of flappy bird (way more complicated than I thought) and am stuck on making the pipes generate at random heights. Could anyone help me with this? To make this same animated I want to generate the pipes a little past the right of the screen and delete them a little past the left. When I run the current code the pipe images are on top of each other in the top left corner and don't move. (The bird works though)
import pygame, random, sys
pygame.init()
icon = pygame.image.load('flappybirdicon.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode([284, 512])
pygame.display.set_caption("Flappy Bird")
bg = pygame.image.load('flappybirdbackground.png')
bgrect = bg.get_rect()
clock = pygame.time.Clock()
pipex = 335
class Bird(pygame.sprite.Sprite):
def __init__(self, image, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.pos = [x, y]
class Pipe(pygame.sprite.Sprite):
def __init__(self, image, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.height = height
self.pos = [pipex, height]
def scroll(self):
self.rect.move_ip(-3, 0)
self.pos[0] -= 3
self.rect.center = self.pos
def draw_pipes():
pipe1_height = random.randint(115, screen.get_height())
pipe1 = Pipe('flappybirdpipe.png', pipe1_height)
pipe2_height = 397 - pipe1_height
pipe2 = Pipe('flappybirdpipe2.png', pipe2_height)
screen.blit(pipe1.image, pipe1.rect)
screen.blit(pipe2.image, pipe2.rect)
bird = Bird('flappybirdbird.png', 142, 256)
draw_pipes()
while True:
clock.tick(30)
bird.pos[1] += 5
bird.rect.center = bird.pos
screen.blit(bg, bgrect)
pipe1.scroll()
pipe2.scroll()
screen.blit(bird.image, bird.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.pos[1] -= 75
elif event.type == pygame.MOUSEBUTTONDOWN:
bird.pos[1] -= 75