-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (49 loc) · 1.71 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
# common imports
import sys
import pygame
from common.core import BaseWidget, run, App
from src.game import Game
from src.user_input import UserInput
from src.button import Button
class MainWidget(BaseWidget):
def __init__(self):
super().__init__()
# Determine what controller type should be used
num_args = len(sys.argv)
self.control_type = "keyboard"
if num_args > 1:
self.control_type = sys.argv[1].lower()
# Init control input
if self.control_type == "controller":
# Init pygame controller input
print("Initiated controller")
pygame.joystick.init()
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
pygame.init()
elif self.control_type == "keyboard":
pass
else:
print("INVALID CONTROL TYPE")
return
# Init game
self.game = Game()
self.canvas.add(self.game)
def on_update(self):
if self.control_type == "controller":
button = UserInput.on_controller_input(self.joystick)
if button:
self.game.on_player_input(button)
self.game.on_update()
def on_key_down(self, keycode, modifiers):
if self.control_type == "keyboard":
button = UserInput.on_keyboard_input(keycode, modifiers)
if button == Button.ESC:
App.get_running_app().stop()
elif button:
self.game.on_player_input(button)
# will get called when the window size changes
def on_layout(self, win_size):
self.game.on_layout(win_size)
if __name__ == "__main__":
run(MainWidget, fullscreen=True)