0

I'm trying to create flappybird style game and I'm having trouble trying to get the pipes to spawn one after another with the same interval just like the real game. I'm having trouble with having them load more than one image at a time. I've tried to use time.sleep but that just waits on the whole game function. I'm not sure what to do at this point, any help is super appreciated!

import pygame
import sys
import time
import random


class FlappyBird:

def __init__(self):
    print("init ran")
# Display
dis = None

# Colors
white = (255, 255, 255)
background_blue = (113, 197, 207)
bird_yellow = (224, 215, 150)
pipe_green = (113, 191, 48)

# Background image
bg = pygame.image.load("fbbg.png")

# Ground image
ground = pygame.image.load("fbground.png")
ground = pygame.transform.smoothscale(ground, (1050, 80))
ground_rect = ground.get_rect()
ground_mask = pygame.mask.from_surface(ground)
ground_rect.center = (500, 730)

# Pipe image
fbpipe = []
fbpipe_rect = []
fbpipe_mask = []

num_of_pipes = 10

for i in range(num_of_pipes):
    fbpipe.append(pygame.image.load("fbpipe.png"))
    fbpipe_rect.append(fbpipe[i].get_rect())
    fbpipe_mask.append(pygame.mask.from_surface(fbpipe[i]))
    fbpipe_rect[i].centerx = 1200
    fbpipe_rect[i].centery = random.randrange(200, 800)

# Bird image
bird = pygame.image.load("fbbird.png")
bird = pygame.transform.smoothscale(bird, (100, 100))
bird_rect = bird.get_rect()
bird_mask = pygame.mask.from_surface(bird)
bird_rect.center = (150, 400)

# Game Over set to False
game_over = False

# Game Start
game_start = False

# Game Clock
clock = pygame.time.Clock()

# Detects pixel collision
def pixel_collision(self, mask1, rect1, mask2, rect2):
    offset_x = rect2[0] - rect1[0]
    offset_y = rect2[1] - rect1[1]
    # See if the two masks at the offset are overlapping.
    overlap = mask1.overlap(mask2, (offset_x, offset_y))
    return overlap

def spawn_pipe(self):
    self.dis.blit(self.fbpipe[self.i], self.fbpipe_rect[self.i])

# Initializes game
def game_start(self):

    # Initialize pygame import
    pygame.init()

    # Set game title
    pygame.display.set_caption("Flappy Bird")

    # Create pygame window and setting size
    dis = pygame.display.set_mode((1024, 768))

    # Birds falling speed
    bird_fall_speed = 3.5

    # Pipe speed
    pipe_speed = 3

    # Wing flap noise
    wing_sound = pygame.mixer.Sound('wing.wav')
    wing_sound.set_volume(0.2)

    # Hit sound
    hit_sound = pygame.mixer.Sound('hit.wav')
    hit_sound.set_volume(0.1)

    self.dis = dis

    # Bird flap
    while not self.game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.game_start = True
                    self.bird_rect.y += -100
                    wing_sound.play()

        # Bird falling/Gravity
        if self.game_start == True:
            self.bird_rect.y += bird_fall_speed

        # Pipe movement
        if self.game_start == True:
            for i in range(self.num_of_pipes):
                self.fbpipe_rect[i].x += -pipe_speed
            if self.pixel_collision(self.bird_mask, self.bird_rect, self.fbpipe_mask[self.i], self.fbpipe_rect[self.i]):
                hit_sound.play()
                time.sleep(0.5)
                self.game_over = True
                print("game over")

        # Set background color
        self.dis.fill(self.background_blue)

        # Set background
        self.dis.blit(self.bg, (0, 0))

        # Draw bird
        self.dis.blit(self.bird, self.bird_rect)

        # Spawn pipe
        self.spawn_pipe()

        # Set ground
        self.dis.blit(self.ground, self.ground_rect)

        if self.pixel_collision(self.bird_mask, self.bird_rect, self.ground_mask, self.ground_rect):
            hit_sound.play()
            time.sleep(0.5)
            self.game_over = True
            print("game over")

        # Update display
        pygame.display.update()

        # Sets the frame ticks to 60
        self.clock.tick(60)

def main():
flappy_bird_game = FlappyBird()
flappy_bird_game.game_start()

if __name__ == "__main__":
main()
sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You have to keep track of the time. If more than a second (for example) has elapsed since your last spawn, then you spawn a new one. – Tim Roberts Aug 15 '22 at 05:43
  • I think tracking the time is a bad idea, because the fps might fluctuate. But if needed you can get the exact time with `time.time()`. I would define a variable `distance_flew`, which increases every tick and `pipe_space`, which is a constant for the space between two pipes. When `distance_flew % pipe_space == 0` a new pipe will be created. – Jerry Aug 15 '22 at 05:53

0 Answers0