-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyaction.py
152 lines (119 loc) · 4.98 KB
/
keyaction.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
import pygame as pyg
import utility
from config import (SINGLEPLAYER_MODE, HOT_MULTIPLAYER_MODE, CO_OP_MODE)
class KeyAction():
def __init__(
self, game_mode, player_tanks, game_objects, space, quit_callback):
self.player1_tank = player_tanks[0]
self.player2_tank = player_tanks[1]
self.game_objects = game_objects
self.space = space
# Functions mapped to key presses.
self.keydown_mapping = {
pyg.K_ESCAPE: quit_callback,
pyg.K_UP: self.keydown_k_up,
pyg.K_DOWN: self.keydown_k_down,
pyg.K_LEFT: self.keydown_k_left,
pyg.K_RIGHT: self.keydown_k_right,
pyg.K_SPACE: self.keydown_k_space,
pyg.K_w: self.keydown_k_w,
pyg.K_s: self.keydown_k_s,
pyg.K_a: self.keydown_k_a,
pyg.K_d: self.keydown_k_d,
pyg.K_x: self.keydown_k_x
}
self.keyup_mapping = {
pyg.K_UP: self.keyup_k_up,
pyg.K_DOWN: self.keyup_k_down,
pyg.K_RIGHT: self.keyup_k_right,
pyg.K_LEFT: self.keyup_k_left,
pyg.K_w: self.keyup_k_w,
pyg.K_s: self.keyup_k_s,
pyg.K_a: self.keyup_k_a,
pyg.K_d: self.keyup_k_d
}
player_keys = {
"player1":
{pyg.K_UP, pyg.K_DOWN, pyg.K_LEFT, pyg.K_RIGHT, pyg.K_SPACE},
"player2":
{pyg.K_w, pyg.K_s, pyg.K_a, pyg.K_d, pyg.K_x}
}
self.accepted_keys = set()
self.accepted_keys.update(self.keyup_mapping.keys())
self.accepted_keys.update(set(self.keydown_mapping.keys()))
def set_singleplayer_keybinds():
self.accepted_keys.difference_update(player_keys["player2"])
def set_hot_multiplayer_keybinds():
return
def set_co_op_keybinds():
set_hot_multiplayer_keybinds()
assert False, "co-op not implemented!"
utility.lookup_call(game_mode, {
SINGLEPLAYER_MODE: set_singleplayer_keybinds,
HOT_MULTIPLAYER_MODE: set_hot_multiplayer_keybinds,
CO_OP_MODE: set_co_op_keybinds
})
def keydown_action(self, event):
""" Calls corresponding function to mapped keydown. """
event_key = event.key
if event_key in self.accepted_keys:
utility.lookup_call(event_key, self.keydown_mapping)
def keyup_action(self, event):
""" Calls corresponding function to mapped keyup. """
event_key = event.key
if event_key in self.accepted_keys:
utility.lookup_call(event_key, self.keyup_mapping)
def keydown_k_space(self):
""" Performs key down space keybinding functionality. """
self.player1_tank.shoot(self.game_objects)
def keydown_k_x(self):
""" Performs key down x keybinding functionality. """
self.player2_tank.shoot(self.game_objects)
def keydown_k_up(self):
""" Performs key down up arrow keybinding functionality. """
self.player1_tank.accelerate()
def keydown_k_down(self):
""" Performs key down down arrow keybinding functionality. """
self.player1_tank.decelerate()
def keydown_k_left(self):
""" Performs key down left arrow keybinding functionality. """
self.player1_tank.turn_left()
def keydown_k_right(self):
""" Performs key down right arrow keybinding functionality. """
self.player1_tank.turn_right()
def keyup_k_up(self):
""" Performs key up up arrow keybinding functionality. """
self.player1_tank.stop_moving()
def keyup_k_down(self):
""" Performs key up down arrow keybinding functionality. """
self.player1_tank.stop_moving()
def keyup_k_left(self):
""" Performs key up left arrow keybinding functionality. """
self.player1_tank.stop_turning()
def keyup_k_right(self):
""" Performs key up right arrow keybinding functionality. """
self.player1_tank.stop_turning()
def keydown_k_w(self):
""" Performs key down w keybinding functionality. """
self.player2_tank.accelerate()
def keydown_k_s(self):
""" Performs key down s keybinding functionality. """
self.player2_tank.decelerate()
def keydown_k_a(self):
""" Performs key down a keybinding functionality. """
self.player2_tank.turn_left()
def keydown_k_d(self):
""" Performs key down d keybinding functionality. """
self.player2_tank.turn_right()
def keyup_k_w(self):
""" Performs key up w keybinding functionality. """
self.player2_tank.stop_moving()
def keyup_k_s(self):
""" Performs key up s keybinding functionality. """
self.player2_tank.stop_moving()
def keyup_k_a(self):
""" Performs key up a keybinding functionality. """
self.player2_tank.stop_turning()
def keyup_k_d(self):
""" Performs key up d keybinding functionality. """
self.player2_tank.stop_turning()