-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
69 lines (53 loc) · 1.87 KB
/
controller.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
#!/usr/bin/env python
import pygame
class EventDispatcher(object):
def __init__(self):
self.__key_down_listeners = []
self.__key_up_listeners = []
def add_key_down_listener(self, listener):
self.__key_down_listeners.append(listener)
def add_key_up_listener(self, listener):
self.__key_up_listeners.append(listener)
def update(self):
for event in pygame.event.get():
if (event.type == pygame.KEYDOWN):
for listener in self.__key_down_listeners:
listener.refresh(event.key)
if (event.type == pygame.KEYUP):
for listener in self.__key_up_listeners:
listener.refresh(event.key)
class Listener(object):
def __init__(self):
self.__last_data = None
@property
def last(self):
return self.__last_data
def refresh(self, event_data):
self.__last_data = event_data
class Controller(Listener):
def __init__(self, game=None):
Listener.__init__(self)
self.__game = game
def control(self, game):
self.__game = game
@property
def controlled_game(self):
return self.__game
class Player1(Controller):
def __init__(self, game=None):
Controller.__init__(self, game)
def refresh(self, key):
if self.controlled_game is None:
return
if key == pygame.K_LEFT:
self.controlled_game.move_left()
elif key == pygame.K_RIGHT:
self.controlled_game.move_right()
elif key == pygame.K_DOWN:
self.controlled_game.move_down()
elif key == pygame.K_UP:
self.controlled_game.rotate_CW()
elif key == pygame.K_SPACE:
self.controlled_game.drop_piece()
elif key == pygame.K_ESCAPE:
self.controlled_game.quit()