-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappybird.py
126 lines (94 loc) · 2.72 KB
/
flappybird.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'''
Simplistic Flappy Bird clone game
for CodeLab
'''
import pygame
import pygame.freetype
from random import randint
DIST = 1500
GAP = 200
JUMP_FORCE = 0.75
DOWN_FORCE = 0.003
class Bird(pygame.sprite.Sprite):
def __init__(self, group):
super().__init__(group)
self.image = pygame.image.load('bird.png').convert()
self.rect = self.image.get_rect()
self.x = 300
self.y = 200
self.vel = 0
def update(self):
self.vel += DOWN_FORCE
self.y += self.vel
self.rect.x = round(self.x)
self.rect.y = round(self.y)
def jump(self):
self.vel = -JUMP_FORCE
class Pipe(pygame.sprite.Sprite):
def __init__(self, group, height, lower):
super().__init__(group)
self.image = pygame.image.load('pipe.png').convert()
self.image = pygame.transform.scale(self.image, (60, height))
self.rect = self.image.get_rect()
self.x = 900
if lower:
self.y = size[1] - height
else:
self.y = 0
self.rect.x = self.x
self.rect.y = self.y
self.vel = 0
def update(self):
self.x -= 0.25
self.rect.x = round(self.x)
self.rect.y = round(self.y)
pygame.init()
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
sprites = pygame.sprite.Group()
font = pygame.freetype.Font(pygame.freetype.match_font('sans'), 24)
bird = Bird(sprites)
pipes = []
score = 0
c = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYUP and event.key == pygame.K_SPACE:
bird.jump()
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
bird.update()
if bird.y < 0 or bird.y > height:
# Out of game bounds; add ML stuff here
score = 0
bird.vel = 0
sprites = pygame.sprite.Group()
sprites.add(bird)
pipes = []
bird.y = 200
for pipe in pipes:
pipe.update()
if pygame.sprite.collide_rect(bird, pipe):
# Collision with pipe; add ML stuff here
score = 0
bird.vel = 0
sprites = pygame.sprite.Group()
sprites.add(bird)
pipes = []
bird.y = 200
if pipe.x == 300 - 60 and pipe.y != 0:
score += 1
if pipe.x < -60:
sprites.remove(pipe)
pipes.remove(pipe)
if c % DIST == 0:
h = randint(0, 400)
pipes.append(Pipe(sprites, h, False))
pipes.append(Pipe(sprites, height - h - GAP, True))
sprites.draw(screen)
font.render_to(screen, (10, 10), f'Score: {score}', (200, 200, 200))
c += 1
pygame.display.flip()
pygame.quit()