forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.py
68 lines (53 loc) · 1.63 KB
/
flappy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
GRAVITY = 0.25
BIRD_JUMP = -4.5
PIPE_WIDTH = 50
PIPE_SPACING = 150
PIPE_VELOCITY = -4
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Flappy Bird')
# Load assets
bird_image = pygame.image.load('bird.png')
background_image = pygame.image.load('background.png')
pipe_image = pygame.image.load('pipe.png')
bird_rect = bird_image.get_rect()
bird_rect.center = (50, SCREEN_HEIGHT // 2)
pipes = []
# Game loop
while True:
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_velocity = BIRD_JUMP
# Update bird position
bird_velocity += GRAVITY
bird_rect.y += bird_velocity
# Update pipe positions
for pipe in pipes:
pipe['x'] += PIPE_VELOCITY
# Generate new pipes
if len(pipes) == 0 or pipes[-1]['x'] <= SCREEN_WIDTH - PIPE_SPACING:
new_pipe = {'x': SCREEN_WIDTH, 'y': PIPE_HEIGHT}
pipes.append(new_pipe)
# Remove off-screen pipes
pipes = [pipe for pipe in pipes if pipe['x'] > -PIPE_WIDTH]
# Draw everything
screen.blit(background_image, (0, 0))
screen.blit(bird_image, bird_rect)
for pipe in pipes:
screen.blit(pipe_image, (pipe['x'], pipe['y']))
screen.blit(pipe_image, (pipe['x'], pipe['y'] - PIPE_SPACING - pipe_image.get_height()))
pygame.display.update()