-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (110 loc) · 3.32 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)
import pygame
from game import Game
from game_over import GameOver
from home_screen import HomeScreen
from instructions import Instructions
from credits import Credits
pygame.init()
pygame.font.init()
pygame.mixer.init()
WIDTH = 900
HEIGHT = 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("BYOG 2021 Game")
pygame.display.set_icon(pygame.image.load(os.path.join("assets", "icon.ico")))
game = None
game_over = None
home_screen = None
instructions = None
game_credits = None
score = 0
def set_score(new_score):
global score
score = new_score
def redraw_screen():
WIN.fill("white")
if game:
game.draw()
if game_over:
game_over.draw()
if home_screen:
home_screen.draw()
if instructions:
instructions.draw()
if game_credits:
game_credits.draw()
pygame.display.flip()
def start_game():
global game, home_screen, game_over, instructions, game_credits
home_screen = None
game_over = None
instructions = None
game_credits = None
ADD_ENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADD_ENEMY, 1500, 1)
game = Game(WIN, WIDTH, HEIGHT, ADD_ENEMY, show_game_over, set_score, show_home_screen)
pygame.mixer.music.load(os.path.join("assets", "sounds", "game_music.ogg"))
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)
def show_game_over():
global game, home_screen, game_over, instructions, game_credits
home_screen = None
game = None
instructions = None
game_credits = None
game_over = GameOver(WIN, score, show_home_screen, start_game)
def show_home_screen():
global game, home_screen, game_over, instructions, game_credits
game = None
game_over = None
if instructions is None and game_credits is None:
play = True
else:
play = False
instructions = None
game_credits = None
home_screen = HomeScreen(WIN, start_game, show_instructions, show_credits)
if play:
pygame.mixer.music.load(os.path.join("assets", "sounds", "background_music.mp3"))
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(1)
def show_instructions():
global game, home_screen, game_over, instructions, game_credits
game = None
game_over = None
home_screen = None
game_credits = None
instructions = Instructions(WIN, show_home_screen)
def show_credits():
global game, home_screen, game_over, instructions, game_credits
game = None
game_over = None
home_screen = None
instructions = None
game_credits = Credits(WIN, show_home_screen)
def main():
running = True
clock = pygame.time.Clock()
show_home_screen()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if game:
game.on_event(event)
if home_screen:
home_screen.on_event(event)
if game_over:
game_over.on_event(event)
if instructions:
instructions.on_event(event)
if game_credits:
game_credits.on_event(event)
redraw_screen()
clock.tick(15)
pygame.quit()
main()