-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidevice.py
More file actions
167 lines (136 loc) · 4.63 KB
/
Copy pathidevice.py
File metadata and controls
167 lines (136 loc) · 4.63 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pygame
import application
class IDevice(object):
""" A Generic input device. We will define classes that
inherit from this. """
deadZone = 0.1
def __init__(self):
self.horiz = 0.0 # Movement in the x-direction [-1.0 ... 1.0]
self.vert = 0.0 # ................y-........................
self.actions = {"attack" : False,
"pattack" : False,
"weapon swap" : False,
"use powerup" : False
}
def update(self, eList, app):
""" Check for change of state """
pass
class Gamepad(IDevice):
""" Controls input from 360 controller. Derived from IDevice class."""
def __init__(self, controllerNumber):
IDevice.__init__(self)
self.gamepad = pygame.joystick.Joystick(controllerNumber)
self.gamepad.init()
self.buttonIDs = [0,1,2,3,(0,1),(1,0),(-1,0),(0,-1)]
self.value = 0 # temp
self.id = controllerNumber + 1
print(self.id)
def update(self, eList, app):
self.horiz = self.gamepad.get_axis(0)
self.vert = self.gamepad.get_axis(1)
#app.onMovement(self.horiz, self.vert)
if abs(self.horiz) <= IDevice.deadZone and abs(self.vert) <= IDevice.deadZone:
self.horiz, self.vert = self.gamepad.get_hat(0)
self.vert = -(self.vert)
if abs(self.horiz) <= IDevice.deadZone and abs(self.vert) <= IDevice.deadZone:
self.horiz = 0
self.vert = 0
for e in eList:
if e.type == pygame.JOYAXISMOTION:
#print(e.joy, e.axis, e.value)
pass
#if e.value >= 0.6 or e.value <= -0.6:
#if self.horiz >= 0.6 or self.horiz <= -0.6 or \
# self.vert >= 0.6 or self.vert <= -0.6:
# app.onMovement(self.horiz, self.vert)
#Trigger Attack Buttons
if self.gamepad.get_axis(2) <= -0.5:
self.actions["attack"] = True
if self.gamepad.get_axis(2) >= 0.5:
self.actions["pattack"] = True
if not self.gamepad.get_button(0):
self.actions["attack"] = False
if not self.gamepad.get_button(1):
self.actions["pattack"] = False
if e.type == pygame.JOYBUTTONDOWN:
if self.gamepad.get_button(5):
self.actions["attack"] = True
#print("attack")
app.onAction("attack", self.id)
#print("0, Attack")
if self.gamepad.get_button(4):
self.actions["pattack"] = True
app.onAction("pattack", self.id)
#print("1, P-Attack")
if self.gamepad.get_button(2):
self.actions["weapon swap"] = True
#print("2, weapon swap")
if self.gamepad.get_button(3):
self.actions["use powerup"] = True
#print("3, Use Powerup")
elif e.type == pygame.JOYBUTTONUP:
#if not self.gamepad.get_button(0):
if e.button == 5:
self.actions["attack"] = False
if e.button == 4:
self.actions["pattack"] = False
if e.button == 2:
self.actions["weapon swap"] = False
if e.button == 3:
self.actions["use powerup"] = False
elif e.type == pygame.JOYHATMOTION:
app.onMovement(e.value[0], -e.value[1])
class Keyboard(IDevice):
""" Controls input from keyboard and mouse. Derived from IDevice class."""
def __init__(self):
IDevice.__init__(self)
def update(self, eList, app):
keysPressed = pygame.key.get_pressed()
if keysPressed[pygame.K_LEFT] or keysPressed[pygame.K_a]:
self.horiz = -1
elif keysPressed[pygame.K_RIGHT] or keysPressed[pygame.K_d]:
self.horiz = 1
else:
self.horiz = 0
if keysPressed[pygame.K_UP] or keysPressed[pygame.K_w]:
self.vert = -1
elif keysPressed[pygame.K_DOWN] or keysPressed[pygame.K_s]:
self.vert = 1
else:
self.vert = 0
for e in eList:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_LEFT or e.key == pygame.K_a:
app.onMovement(self.horiz, self.vert)
if e.key == pygame.K_RIGHT or e.key == pygame.K_d:
app.onMovement(self.horiz, self.vert)
if e.key == pygame.K_UP or e.key == pygame.K_w:
app.onMovement(self.horiz, self.vert)
if e.key == pygame.K_DOWN or e.key == pygame.K_s:
app.onMovement(self.horiz, self.vert)
if e.key == pygame.K_f:
self.actions["weapon swap"] = True
if e.key == pygame.K_e:
self.actions["use powerup"] = True
app.onAction("use powerup", 0)
if e.key == pygame.K_SPACE:
app.onAction("attack", 0)
if e.key == pygame.K_RETURN:
app.onAction("pattack", 0)
if e.key == pygame.K_r:
app.refresh()
if e.type == pygame.MOUSEBUTTONDOWN:
if e.button == 1:
self.actions["attack"] = True
app.onAction("attack", 0)
if e.button == 3:
self.actions["pattack"] = True
app.onAction("pattack", 0)
if e.type == pygame.KEYUP:
if e.key == pygame.K_f:
self.actions["weapon swap"] = False
if e.type == pygame.MOUSEBUTTONUP:
if e.button == 1:
self.actions["attack"] = False
if e.button == 3:
self.actions["pattack"] = False