- Get link
- X
- Other Apps
import pygame
import sys
from random import randint
# Initialize Pygame
pygame.init()
# Screen Dimensions
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 122, 255)
GREEN = (0, 200, 0)
# Game Variables
gravity = 0.5
bird_movement = 0
score = 0
# Bird
bird = pygame.Rect(100, 300, 30, 30)
# Pipes
pipe_width = 60
pipe_gap = 150
pipe_x = 400
pipe_height = randint(100, 400)
# Clock
clock = pygame.time.Clock()
# Game Loop
while True:
screen.fill(WHITE)
# Event Handling
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_movement = -10
# Bird Movement
bird_movement += gravity
bird.y += bird_movement
# Draw Bird
pygame.draw.ellipse(screen, BLUE, bird)
# Draw Pipes
pipe_x -= 5
if pipe_x < -pipe_width:
pipe_x = WIDTH
pipe_height = randint(100, 400)
score += 1
pygame.draw.rect(screen, GREEN, (pipe_x, 0, pipe_width, pipe_height))
pygame.draw.rect(screen, GREEN, (pipe_x, pipe_height + pipe_gap, pipe_width, HEIGHT - pipe_height - pipe_gap))
# Collision Detection
if bird.colliderect((pipe_x, 0, pipe_width, pipe_height)) or bird.colliderect((pipe_x, pipe_height + pipe_gap, pipe_width, HEIGHT - pipe_height - pipe_gap)):
print("Game Over! Score:", score)
pygame.quit()
sys.exit()
# Draw Score
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(30)
Comments
Post a Comment