-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
87 lines (67 loc) · 2.48 KB
/
main.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
import pygame, random
from classes.background import Background
from classes.bird import Bird
from classes.pipe import Pipe
pygame.init()
pygame.mixer.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 500, 768
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
FONT = pygame.font.Font('./assets/flappy.ttf', 144)
pygame.display.set_caption("NEAT - Flappy Bird")
flap_sound = pygame.mixer.Sound("./assets/bird/wing.mp3")
point_sound = pygame.mixer.Sound("./assets/point.mp3")
def display_score(score):
score_img = FONT.render("{}".format(score), True, (255, 255, 255))
score_rect = score_img.get_rect()
score_rect.center = (SCREEN_WIDTH // 2, 100)
SCREEN.blit(score_img, score_rect)
def main():
run = True
clock = pygame.time.Clock()
# Initialize a background
bg = Background(SCREEN_WIDTH, SCREEN_HEIGHT)
Bird.birds = [Bird(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, "yellow")]
score = 0
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
for bird in Bird.birds:
bird.jump()
flap_sound.play()
if len(Bird.birds) == 0:
pygame.quit()
dt = 1 / 60
SCREEN.fill((255, 255, 255))
if len(Pipe.pipes) == 0 or Pipe.pipes[-1].right_x() < SCREEN_WIDTH - 300:
bottom_y = random.randint(300, SCREEN_HEIGHT - 200)
top_y = random.randint(100, bottom_y - 200)
pipe = Pipe(SCREEN_WIDTH, bottom_y, top_y)
# Update and draw the background
bg.update(dt)
bg.draw(SCREEN)
# Update and draw the birds
for bird in Bird.birds:
bird.update(dt)
# Collisions
for pipe in Pipe.pipes:
if pipe.collide(bird):
Bird.birds.remove(bird)
if bird.rect.bottom < 0 or bird.rect.top > SCREEN_HEIGHT:
Bird.birds.remove(bird)
bird.draw(SCREEN)
for pipe in Pipe.pipes:
pipe.update(dt)
pipe.draw(SCREEN)
if pipe.right_x() < SCREEN_WIDTH // 2 and not pipe.scored:
score += 1
pipe.scored = True
point_sound.play()
display_score(score)
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()