-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.py
88 lines (65 loc) · 2.26 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
88
import pygame
import assets
import configs
from objects.background import Background
from objects.bird import Bird
from objects.column import Column
from objects.floor import Floor
from objects.gameover_message import GameOverMessage
from objects.gamestart_message import GameStartMessage
from objects.score import Score
pygame.init()
screen = pygame.display.set_mode((configs.SCREEN_WIDTH, configs.SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird Game v1.0.2")
img = pygame.image.load('assets/icons/red_bird.png')
pygame.display.set_icon(img)
clock = pygame.time.Clock()
column_create_event = pygame.USEREVENT
running = True
gameover = False
gamestarted = False
assets.load_sprites()
assets.load_audios()
sprites = pygame.sprite.LayeredUpdates()
def create_sprites():
Background(0, sprites)
Background(1, sprites)
Floor(0, sprites)
Floor(1, sprites)
return Bird(sprites), GameStartMessage(sprites), Score(sprites)
bird, game_start_message, score = create_sprites()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == column_create_event:
Column(sprites)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not gamestarted and not gameover:
gamestarted = True
game_start_message.kill()
pygame.time.set_timer(column_create_event, 1500)
if event.key == pygame.K_ESCAPE and gameover:
gameover = False
gamestarted = False
sprites.empty()
bird, game_start_message, score = create_sprites()
if not gameover:
bird.handle_event(event)
screen.fill(0)
sprites.draw(screen)
if gamestarted and not gameover:
sprites.update()
if bird.check_collision(sprites) and not gameover:
gameover = True
gamestarted = False
GameOverMessage(sprites)
pygame.time.set_timer(column_create_event, 0)
assets.play_audio("hit")
for sprite in sprites:
if type(sprite) is Column and sprite.is_passed():
score.value += 1
assets.play_audio("point")
pygame.display.flip()
clock.tick(configs.FPS)
pygame.quit()