-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
222 lines (171 loc) · 5.76 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
# Python
import asyncio
import os
# Pygame
import pygame
from pygame.locals import *
# Game
import events
import game
import slitherzenith as sz
from components.support.settings import fps, base_dir
from components.support import settings
import components.support.input as inp
from components.timer.timer_manager import update_timers
__debug = False
# Set app icon
icon_path = os.path.join(base_dir,
"../assets", "icons",
"slitherzenith_black.png")
pygame_icon = pygame.image.load(icon_path)
pygame.display.set_icon(pygame_icon)
# Setup game
game.setup()
# pygame setup
flags = pygame.DOUBLEBUF | pygame.HWSURFACE
pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.init()
screen = pygame.display.set_mode(sz.screen_size, flags, 16)
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])
# Controller
joysticks = {}
# Setup game loop
clock = pygame.time.Clock()
running = 1
# Set amount of audio channels
pygame.mixer.set_num_channels(32)
# Print debug message when in debug mode
def __debug_log(msg: str) -> None:
"""
Print debug message when in debug mode
:param msg: The message to print
:return: None
"""
if __debug:
print(msg)
# Render objects in draw buffer
def __render_objects_on_screen() -> None:
"""
Render objects in draw buffer
:return: None
"""
for obj in sz.__draw_buffer:
obj.draw(screen)
# === Keyboard input === #
def __handle_keyboard_events(event_args: pygame.event) -> None:
"""
Handle keyboard events
:param event_args: The event arguments
:return: None
"""
events.handle_keyboard_input(event_args)
if not (event_args.type == KEYDOWN or event_args.type == KEYUP):
return
key = events.key_to_str(event_args.key)
if event_args.type == KEYDOWN:
sz.__key_status[key] = True
elif event_args.type == KEYUP:
sz.__key_status[key] = False
# ==== Mouse input ==== #
def __handle_mouse_events(event_args: pygame.event) -> None:
"""
Handle mouse events
:param event_args: The event arguments
:return: None
"""
events.handle_mouse_input(event_args)
# Wheel event
if event_args.type == MOUSEWHEEL:
__debug_log("Wheel")
if event_args.y > 0:
sz.__scroll_up = True
elif event_args.y < 0:
sz.__scroll_down = True
# Motion event
if event_args.type == MOUSEMOTION:
__debug_log("Movement")
sz.mouse_position = event_args.pos
# Stop execution when no mouse event detected
if not (event_args.type == MOUSEBUTTONDOWN or
event_args.type == MOUSEBUTTONUP):
return
# Fetch pressed button (if possible)
button = events.button_to_str(event_args.button)
if event_args.type == MOUSEBUTTONDOWN or event_args.type == MOUSEBUTTONUP:
__debug_log(f"Received {button} event")
# Button event
if event_args.type == MOUSEBUTTONDOWN:
sz.__mouse_status[button] = True
elif event_args.type == MOUSEBUTTONUP:
sz.__mouse_status[button] = False
# === Controller input === #
def __handle_controller_events(event_args: pygame.event) -> None:
"""
Handle controller events
:param event_args: The event arguments
:return: None
"""
responsive_buttons = [0, 1, 2, 3, 11, 12, 13, 14]
# Button down event
if event_args.type == pygame.JOYBUTTONDOWN:
if event_args.button in responsive_buttons:
sz.__nintendo_switch_button_status[event_args.button] = True
# Button up event
elif event_args.type == pygame.JOYBUTTONUP:
if event_args.button in responsive_buttons:
sz.__nintendo_switch_button_status[event_args.button] = False
# Application entry point
async def main() -> None:
"""
Application entry point
:return: None
"""
global running
# Game loop
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
__handle_keyboard_events(event)
__handle_mouse_events(event)
__handle_controller_events(event)
# Handle hot plugging
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for
# every joystick, filling up the list without needing to
# create them manually.
joy = pygame.joystick.Joystick(event.device_index)
sz.__nintendo_switch_joystick[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
if event.type == pygame.JOYDEVICEREMOVED:
del joysticks[event.instance_id]
print(f"Joystick {event.instance_id} disconnected")
# Quit game
if event.type == pygame.QUIT:
running = 0
# fill the screen with a color to wipe away anything from last frame
pygame.display.set_caption(sz.screen_title)
screen.fill(sz.background_color)
# Update mouse position
sz.mouse_position = pygame.mouse.get_pos()
# Get elapsed time between frames
delta_time = clock.tick(fps) / 1000.0
settings.delta_time = delta_time
# Update & render game
game.update()
game.draw()
__render_objects_on_screen()
update_timers()
# Update input timer
if not inp.clickable:
inp.click_timer += delta_time
if inp.click_timer >= inp.click_delay:
inp.clickable = True
inp.click_timer = 0
# flip() the display to put your work on screen
pygame.display.flip()
sz.__draw_buffer.clear()
await asyncio.sleep(0)
pygame.quit()
return
asyncio.run(main())