Featured
- Get link
- X
- Other Apps
Python Code for " Flappy Bird " game. Computer Science project
HERE'S A PYTHON CODE FOR FLAPPY BIRD GAME:
import pygame
import random
# Initialize pygame
pygame.init()
# Define the game window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# Set the game window title
pygame.display.set_caption("Flappy Bird")
# Define the game colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
# Load the game images
BIRD_IMAGE = pygame.image.load('bird.png')
PIPE_IMAGE = pygame.image.load('pipe.png')
# Define the bird class
class Bird:
def __init__(self):
self.x = 50
self.y = 250
self.velocity = 0
self.gravity = 0.5
def jump(self):
self.velocity = -10
def move(self):
self.velocity += self.gravity
self.y += self.velocity
def draw(self):
WINDOW.blit(BIRD_IMAGE, (self.x, self.y))
# Define the pipe class
class Pipe:
def __init__(self, x):
self.x = x
self.top = random.randint(50, 250)
self.bottom = self.top + 150
self.width = PIPE_IMAGE.get_width()
self.scored = False
def move(self):
self.x -= 5
def draw(self):
WINDOW.blit(PIPE_IMAGE, (self.x, self.top - PIPE_IMAGE.get_height()))
WINDOW.blit(PIPE_IMAGE, (self.x, self.bottom))
def collide(self, bird):
if bird.x + BIRD_IMAGE.get_width() > self.x and bird.x < self.x + self.width:
if bird.y < self.top or bird.y + BIRD_IMAGE.get_height() > self.bottom:
return True
return False
# Initialize the game objects
bird = Bird()
pipes = [Pipe(WINDOW_WIDTH + i * 200) for i in range(3)]
score = 0
# Define the game loop
clock = pygame.time.Clock()
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()
# Move the bird
bird.move()
# Move the pipes
for pipe in pipes:
pipe.move()
# Check if the bird collides with a pipe
if pipe.collide(bird):
game_over = True
# Check if the bird passes a pipe
if not pipe.scored and pipe.x + pipe.width < bird.x:
pipe.scored = True
score += 1
# Check if the pipe is off the screen
if pipe.x + pipe.width < 0:
pipe.x = WINDOW_WIDTH
pipe.top = random.randint(50, 250)
pipe.bottom = pipe.top + 150
pipe.scored = False
# Draw the game objects
WINDOW.fill(WHITE)
bird.draw()
for pipe in pipes:
pipe.draw()
# Draw the score
font = pygame.font.SysFont('comicsansms', 30)
text = font.render('Score: ' + str(score), True, BLACK)
WINDOW.blit(text, 10)
- Get link
- X
- Other Apps
Popular Posts
Python code for " Simple Calculator " . Computer science project
- Get link
- X
- Other Apps
Python Code for " To-Do List ". Computer Science project for BEGINNER
- Get link
- X
- Other Apps

Comments
Post a Comment