Skip to content

Commit 5299204

Browse files
committed
pygame - flappy bird
1 parent 75447cb commit 5299204

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

pygame/__flappy_bird__/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Image:
3+
4+
![#1](images/flappy_bird.png?raw=true)
10.1 KB
Loading

pygame/__flappy_bird__/main.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2019.12.03
4+
# https://stackoverflow.com/questions/59149195/how-to-resize-an-image-in-pygame-to-reach-the-top-bottom-of-screen
5+
6+
import pygame
7+
import random
8+
9+
# --- constants --- (UPPER_CASE_NAMES)
10+
11+
SCREEN_WIDTH = 800
12+
SCREEN_HEIGHT = 600
13+
14+
FPS = 60
15+
16+
BLACK = (0, 0, 0)
17+
WHITE = (255, 255, 255)
18+
GREEN = (0, 255, 0)
19+
20+
# --- classes --- (CamelCaseNames)
21+
22+
# empty
23+
24+
# --- main ---
25+
26+
gap_size = 200
27+
ball_size = gap_size//4
28+
29+
pygame.init()
30+
31+
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
32+
screen_rect = screen.get_rect()
33+
34+
image = pygame.image.load("Obrazy/images/paddle.png").convert()
35+
image = pygame.transform.scale(image, (50, SCREEN_HEIGHT))
36+
image_rect = image.get_rect()
37+
38+
ball_rect = pygame.Rect((0, 0, ball_size, ball_size))
39+
ball_rect.center = screen_rect.center
40+
41+
ball_speed = 5
42+
43+
# --- reset ---
44+
pipes = []
45+
46+
# --- add ---
47+
pipe1_rect = image_rect.copy()
48+
pipe2_rect = image_rect.copy()
49+
50+
pipe1_rect.left = screen_rect.right # move to right of screen
51+
pipe2_rect.left = screen_rect.right # move to right of screen
52+
53+
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
54+
pipe2_rect.top = pipe1_rect.bottom + gap_size
55+
56+
pipes.append( (pipe1_rect, pipe2_rect) )
57+
# ---
58+
59+
# --- mainloop ---
60+
61+
clock = pygame.time.Clock()
62+
63+
running = True
64+
while running:
65+
66+
# --- events ---
67+
for event in pygame.event.get():
68+
if event.type == pygame.QUIT:
69+
running = False
70+
71+
elif event.type == pygame.KEYUP:
72+
if event.key == pygame.K_ESCAPE:
73+
running = False
74+
75+
if event.type == pygame.KEYDOWN:
76+
if event.key == pygame.K_UP:
77+
ball_speed = -3
78+
79+
if event.type == pygame.KEYUP:
80+
if event.key == pygame.K_UP:
81+
ball_speed = 4
82+
83+
# --- changes/moves/updates ---
84+
85+
ball_rect.y += ball_speed
86+
87+
for pipe1_rect, pipe2_rect in pipes:
88+
pipe1_rect.x -= 1
89+
pipe2_rect.x -= 1
90+
91+
for pipe1_rect, pipe2_rect in pipes:
92+
if pipe1_rect.colliderect(ball_rect) or pipe2_rect.colliderect(ball_rect):
93+
print('Game Over')
94+
# --- reset ---
95+
pipes = []
96+
97+
# --- add ---
98+
pipe1_rect = image_rect.copy()
99+
pipe2_rect = image_rect.copy()
100+
101+
pipe1_rect.left = screen_rect.right # move to right of screen
102+
pipe2_rect.left = screen_rect.right # move to right of screen
103+
104+
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
105+
pipe2_rect.top = pipe1_rect.bottom + gap_size
106+
107+
pipes.append( (pipe1_rect, pipe2_rect) )
108+
109+
for pipe1_rect, pipe2_rect in pipes:
110+
if pipe1_rect.left == 0:
111+
print("POINT")
112+
113+
if not screen_rect.contains(ball_rect):
114+
print('Game Over')
115+
# --- reset ---
116+
pipes = []
117+
118+
# --- add ---
119+
pipe1_rect = image_rect.copy()
120+
pipe2_rect = image_rect.copy()
121+
122+
pipe1_rect.left = screen_rect.right
123+
pipe2_rect.left = screen_rect.right
124+
125+
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
126+
pipe2_rect.top = pipe1_rect.bottom + gap_size
127+
128+
pipes.append( (pipe1_rect, pipe2_rect) )
129+
130+
if pipes[-1][0].right < SCREEN_WIDTH - 200:
131+
# --- add ---
132+
pipe1_rect = image_rect.copy()
133+
pipe2_rect = image_rect.copy()
134+
135+
pipe1_rect.left = screen_rect.right
136+
pipe2_rect.left = screen_rect.right
137+
138+
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
139+
pipe2_rect.top = pipe1_rect.bottom + gap_size
140+
141+
pipes.append((pipe1_rect, pipe2_rect))
142+
143+
# --- draws ---
144+
145+
screen.fill(BLACK)
146+
147+
for pipe1_rect, pipe2_rect in pipes:
148+
screen.blit(image, pipe1_rect)
149+
screen.blit(image, pipe2_rect)
150+
151+
pygame.draw.rect(screen, GREEN, ball_rect)
152+
153+
pygame.display.flip()
154+
155+
# --- FPS ---
156+
157+
ms = clock.tick(FPS)
158+
#pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
159+
fps = clock.get_fps()
160+
pygame.display.set_caption('FPS: {}'.format(fps))
161+
162+
# --- end ---
163+
164+
pygame.quit()

0 commit comments

Comments
 (0)