-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
75 lines (64 loc) · 2.75 KB
/
Copy pathcontroller.py
File metadata and controls
75 lines (64 loc) · 2.75 KB
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
# These are the game controllers: included are the keyboard controller class and
# the CPU spinner class.
from events import *
import pygame
from pygame.locals import *
DIRECTION_UP = 0
DIRECTION_DOWN = 1
DIRECTION_LEFT = 2
DIRECTION_RIGHT = 3
#------------------------------------------------------------------------------
class KeyboardController:
"""KeyboardController takes Pygame events generated by the
keyboard and uses them to control the model, by sending Requests
or to control the Pygame display directly, as with the QuitEvent
"""
def __init__(self, evManager):
self.evManager = evManager
self.evManager.RegisterListener( self )
#----------------------------------------------------------------------
def Notify(self, event):
if isinstance( event, TickEvent ):
for event in pygame.event.get():
ev = None
if event.type == QUIT:
ev = QuitEvent()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
ev = QuitEvent()
elif event.type == KEYDOWN and event.key == K_w:
direction = DIRECTION_UP
ev = CharactorMoveRequest(direction)
elif event.type == KEYDOWN and event.key == K_s:
direction = DIRECTION_DOWN
ev = CharactorMoveRequest(direction)
elif event.type == KEYDOWN and event.key == K_a:
direction = DIRECTION_LEFT
ev = CharactorMoveRequest(direction)
elif event.type == KEYDOWN and event.key == K_d:
direction = DIRECTION_RIGHT
ev = CharactorMoveRequest(direction)
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
ev = TogglePauseRequest(True)
if event.button == 3:
ev = TogglePauseRequest(False)
if ev:
self.evManager.Post( ev )
#------------------------------------------------------------------------------
class CPUSpinnerController:
"""..."""
def __init__(self, evManager):
self.evManager = evManager
self.evManager.RegisterListener( self )
self.keepGoing = 1
#----------------------------------------------------------------------
def Run(self):
while self.keepGoing:
event = TickEvent()
self.evManager.Post( event )
#----------------------------------------------------------------------
def Notify(self, event):
if isinstance( event, QuitEvent ):
self.keepGoing = False
if __name__ == "__main__":
print("wasn't expecting that")