This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
executable file
·329 lines (279 loc) · 11.9 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
## DEBUG OPTIONS ##
Fast_Compile = False # does not pre-compile the environment (meant for testing ui)
Debug_Mode = False # get put directly into the 3D environment
Debug_Track = "MarioCircuit" # track to load when starting in debug mode
import pygame
import numpy
import os
#import time
from Modules import Game
from Modules import Menus
from Modules import GhostManager
from Modules.UIObjects import Fade as FadeObject
from Modules.Track import Tracks
from Modules.Driver import Driver
from PyGame3D import Engine_3D
## CONFIG ##
Resolution = 2.5 # resolution multiplier; larger = more lag
Window = (800, 600) # Window size (default is 800 x 600)
FPS = 60 # FPS Cap
## Colors ##
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 204, 0)
## Assets ##
COIN = Engine_3D.get_image("Assets/Sprites/Coin.png")
COIN = pygame.transform.scale(COIN, (45, 45))
DRIFT = Engine_3D.get_image("Assets/Sprites/DriftParticle.png")
MINI = Engine_3D.get_image("Assets/Sprites/MiniTurbo.png")
SUPER = Engine_3D.get_image("Assets/Sprites/SuperTurbo.png")
ULTRA = Engine_3D.get_image("Assets/Sprites/UltraTurbo.png")
# initialize tracks
tracks = Tracks()
pygame.init()
# joystick
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
font = pygame.font.SysFont("Comic Sans MS", 32)
def initialize():
# code to prepare camera
Camera = Engine_3D.Camera(Resolution, Window, (0.0, 0.0, 0.0, 0.0), True)
Mario, MarioSize = Camera.get_sprites("Assets/Racers/Mayro/MayroDrive.png", 12, 32, 32)
MPos = (952, 1, 751)#(935, 1, 444)
MSp = Engine_3D.Sprite(Mario, 12, MPos, True)
MSp.Direction = numpy.deg2rad(-90.01)
Camera.add_sprite(MSp)
Camera.PImg = [DRIFT, MINI, SUPER, ULTRA]
MDriver = Driver(MSp, 4.0, 0.4)
MDriver.attach_camera(Camera)
tracks.MarioCircuit.load_track(Camera)
cp = tracks.MarioCircuit.Checkpoints
Coins = tracks.MarioCircuit.spawn_coins(Camera)
CoinAnims = []
Camera.preload()
# main program
def main():
clock = pygame.time.Clock()
run = True
# add a loading screen for while the camera compiles
#text = font.render('Loading environment...', True, BLUE)
#textRect = text.get_rect()
#textRect.center = (Window[0] // 2, Window[1] // 2)
WIN = pygame.display.set_mode((Window[0], Window[1]))
#WIN.blit(text, textRect)
pygame.display.update()
pygame.display.set_caption("Loading game...")
#Menus.create_menu_buttons()
# initialize/preload the camera
if not Fast_Compile:
loadEnv = Game.TimeTrial(tracks.MarioCircuit, "Mayro", 100, False, False)
del loadEnv
#initialize()
# create game
#pygame.mouse.set_visible(False)
game = None #Game.TimeTrial(tracks.MarioCircuit, "Mario", 100)
screen = "Main"
menu_animations = True
# game determining variables
# defaults
selected_cc = 100
selected_char = "Mayro"
selected_track = tracks.MarioCircuit
VS_Ghost = False
# fade variables
Fading = False
Fade = FadeObject()
# create the menu object
Menu = None
if not Debug_Mode:
Menu = Menus.MainMenu(menu_animations)
Menu.Music.play(-1)
# set fps to 30 for main menu
FPS = 30
else:
# load the debug mode
# create the game
pygame.mouse.set_visible(False)
game = Game.TimeTrial(getattr(tracks, Debug_Track), "Mayro", 100, False)
screen = "Game"
FPS = 60
PauseMenu = Menus.Pause()
while run:
clock.tick(FPS)
pygame.display.set_caption("FPS: " + str(int(clock.get_fps())))
#Menus.title_screen(WIN)
#pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
# if the user decides to quit
run = False
if event.type == pygame.KEYDOWN and screen == "Game" and game.RoundRunning:
if GhostManager.is_valid_key(event.key):
# create the frame
game.GhostData.add_point(game.Timer, False, game.Driver.Coins, pygame.key.get_pressed())
if event.type == pygame.KEYUP and screen == "Game" and game.RoundRunning:
if GhostManager.is_valid_key(event.key):
# create the frame
game.GhostData.add_point(game.Timer, False, game.Driver.Coins, pygame.key.get_pressed())
if event.type == pygame.MOUSEBUTTONDOWN and not Fading:
# mouse clicked
if screen == "Main":
# check for clicking menu buttons
todo = Menu.buttons_hovering()
if Menu.ButtonType == "CC_Menu" and todo != "Back" and todo != "None":
screen = "Character"
selected_cc = todo
Fading = True
"""
cc = todo
# create the game
pygame.mouse.set_visible(False)
del Menu
game = Game.TimeTrial(tracks.MarioCircuit2, "Mario", todo)
screen = "Game"
FPS = 60
"""
elif Menu.ButtonType == "Main" and todo == "CC":
Menu.create_cc_buttons()
elif Menu.ButtonType == "Main" and todo == "Quit":
run = False
elif screen == "Character":
# check for clicking menu buttons
todo = Menu.buttons_hovering()
if todo == "Back":
screen = "Main"
Fading = True
elif todo != "None":
selected_char = todo
screen = "Tracks"
Fading = True
elif screen == "Tracks":
# check for clicking menu buttons
todo = Menu.buttons_hovering()
if todo == "Back":
screen = "Character"
Fading = True
elif todo != "None":
selected_track = getattr(tracks, todo)
VS_Ghost = False#GhostManager.has_ghost(selected_track.Name, selected_cc)
screen = "GameLoad"
Fading = True
if event.type == pygame.JOYBUTTONDOWN:
#### CONTROLLER STUFF ####
if screen == "Main":
# check controller inputs
todo = Menu.controller_inputs(event.button)
#if event.button == 1:
#todo = "CC"
# check for selecting menu buttons
#todo = Menu.buttons_hovering()
if Menu.ButtonType == "CC_Menu" and todo != "Back" and todo != "None":
screen = "Character"
selected_cc = todo
Fading = True
elif Menu.ButtonType == "Main" and todo == "CC":
Menu.create_cc_buttons()
elif Menu.ButtonType == "Main" and todo == "Quit":
run = False
elif event.type == pygame.JOYAXISMOTION:
pass
#### MENU MANAGEMENT STUFF ####
if screen == "Game":
status = game.update(WIN)
if game.Paused:
PauseMenu.draw_menu(WIN)
# check if mouse down
if pygame.mouse.get_pressed()[0]:
# check for clicking menu buttons
todo = PauseMenu.buttons_hovering()
del PauseMenu
PauseMenu = Menus.Pause()
if todo == "Quit":
# return to title screen
screen = "MainTransition"
Fading = True
elif todo == "Resume":
# resume the game
game.pause()
elif todo == "Restart":
# restart the race
screen = "GameRestart"
Fading = True
# check the status of the game
if status == 0:
run = False
elif status == 2:
screen = "MainTransition"
Fading = True
else:
if screen != "GameLoad" and screen != "MainTransition" and screen != "GameRestart":
Menu.draw_menu(WIN)
if Fading:
# manage fading
s, pos = Fade.render()
WIN.blit(s, pos)
#pygame.display.update()
# if at the peak
if Fade.at_peak():
if screen == "Character":
Menu.Music.stop()
del Menu
Menu = Menus.CharacterSelection()
#time.sleep(0.05) # wait to make sure music has loaded
Menu.Music.play(-1)
elif screen == "Main":
# return to cc menu
Menu.Music.stop()
del Menu
Menu = Menus.MainMenu(menu_animations)
Menu.create_cc_buttons()
Menu.Music.play(-1)
elif screen == "Tracks":
# show the track list
Menu.Music.stop()
del Menu
Menu = Menus.TrackSelection(selected_cc)
Menu.Music.play(-1)
elif screen == "GameLoad":
# create the game
pygame.mouse.set_visible(False)
Menu.Music.stop()
del Menu
game = Game.TimeTrial(selected_track, selected_char, selected_cc, VS_Ghost)
screen = "Game"
FPS = 60
Fade.Increment = Fade.Increment/2
elif screen == "MainTransition":
FPS = 30
Fade.Increment = Fade.Increment*2
Menu = Menus.MainMenu(menu_animations)
pygame.mouse.set_visible(True)
# stop all running music in the game
game.Sounds["Theme_Regular"].stop()
game.Sounds["Theme_Final_Lap"].stop()
# save the ghost
if game.SaveGhost:
game.GhostData.save_data(selected_track.Name)
Menu.Music.play(-1)
del game # clear the game
screen = "Main"
# update the time trial times
Menus.update_time_data()
elif screen == "GameRestart":
# restart the game
pygame.mouse.set_visible(False)
# stop all running music in the game
game.Sounds["Theme_Regular"].stop()
game.Sounds["Theme_Final_Lap"].stop()
# delete game object and create a new one
del game
game = Game.TimeTrial(selected_track, selected_char, selected_cc, VS_Ghost)
screen = "Game"
# if at the finish
elif Fade.at_finish():
Fading = False
Fade.reset()
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()