Its like a floppy bird

import pygame
import random

Initialize Pygame

pygame.init()

Set up the screen

screen_width = 288
screen_height = 512
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(“Flappy Bird”)

Load assets

background_image = pygame.image.load(“background.png”).convert()
bird_image = pygame.image.load(“bird.png”).convert_alpha()
pipe_image = pygame.image.load(“pipe.png”).convert_alpha()

Set up variables

clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)
score = 0

Set up the bird

class Bird(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.image = bird_image
self.rect = self.image.get_rect()
self.rect.center = (screen_width/2, screen_height/2)
self.velocity = 0

def update(self):
    self.velocity += 0.5
    self.rect.y += self.velocity

    if self.rect.bottom > screen_height:
        self.rect.bottom = screen_height
        self.velocity = 0

def jump(self):
    self.velocity = -10

Set up the pipes

class Pipe(pygame.sprite.Sprite):
def init(self, x, y):
pygame.sprite.Sprite.init(self)
self.image = pipe_image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self):
    self.rect.x -= 3

Set up the sprite groups

bird_group = pygame.sprite.Group()
pipe_group = pygame.sprite.Group()

Add the bird to the sprite group

bird = Bird()
bird_group.add(bird)

Set up the initial pipes

for i in range(3):
pipe = Pipe(screen_width + i*200, random.randint(100, 400))
pipe_group.add(pipe)

Main game loop

while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()

# Update the game state
bird_group.update()
pipe_group.update()

# Check for collisions
if pygame.sprite.groupcollide(bird_group, pipe_group, False, False):
    pygame.quit()
    sys.exit()

# Check for points
for pipe in pipe_group:
    if pipe.rect.right < bird.rect.left and not pipe.scored:
        pipe.scored = True
        score += 1

# Remove pipes that have gone off the screen
for pipe in pipe_group:
    if pipe.rect.right < 0:
        pipe_group.remove(pipe)
        new_pipe = Pipe(screen_width, random.randint(100, 400))
        pipe_group.add(new_pipe)

# Draw the screen
screen.blit(background_image, (0,0))
bird_group.draw(screen)
pipe_group.draw(screen)
score_text = font.render("Score: " + str(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))

# Update the screen
pygame.display.flip()

# Set the frame rate
clock.tick(60)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.