-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_game.py
77 lines (60 loc) · 1.85 KB
/
pi_game.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
from PIL import ImageFont
from time import sleep
import pwd
from buttons_HW import Buttons_HW, HWKey
from display_LCD import Display_LCD
from menu_mode import MenuMode
from game_select import GameSelect
from equations import Equations
class PiGame:
_index = 0
_display = None
_menu_mode = MenuMode.GAME_SELECT
_game_select = None
_equations = None
def __init__(self):
is_debug = self._debug_detect()
if not is_debug:
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
buttons = Buttons_HW(is_debug)
buttons.listen_buttons(self._buttons_callback)
self._game_select = GameSelect()
self._game_select.set_callback(self._change_menu_mode)
self._equations = Equations()
self._equations.set_callback(self._change_menu_mode)
self._display = Display_LCD(is_debug, self._draw)
if is_debug:
self._draw(self._display.debug_draw())
self._display.debug_show()
def _draw(self, draw):
if self._menu_mode == MenuMode.GAME_SELECT:
self._game_select.draw(draw)
elif self._menu_mode == MenuMode.EQUATIONS:
self._equations.draw(draw)
elif self._menu_mode == MenuMode.MATH_SQUARE:
draw.text((2, 2), "TODO", fill="white")
def _buttons_callback(self, button):
print(button)
if self._menu_mode == MenuMode.GAME_SELECT:
self._game_select.handle_button(button)
elif self._menu_mode == MenuMode.EQUATIONS:
self._equations.handle_button(button)
elif self._menu_mode == MenuMode.MATH_SQUARE:
pass
if self._debug_detect():
self._display.debug_clear()
self._draw(self._display.debug_draw())
self._display.debug_show()
def _change_menu_mode(self, menu_mode):
self._menu_mode = menu_mode
def _debug_detect(self):
try:
# Check if user named `pi` exists
# If exists then we known that code is running on Raspberry
pwd.getpwnam('pi')
return False
except KeyError:
return True
PiGame()