-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.py
359 lines (250 loc) · 11 KB
/
controls.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'''Controls module for the project, contains keyboard, mouse and joypad handling'''
import pygame as PG, random
import toolsV2, actors, lists, constants, pathing
import loadImages
#movement list
movementCtrlsDict = {'w': 'up',
'a': 'left',
's': 'down',
'd': 'right',
}
dirDict= {'up': (0, -1),
'down': (0, 1),
'left': (-1, 0),
'right': (1, 0),
}
shooterCtrlsDict = {'c': 'shootBullet',
}
debugCtrlsDict = {'b': 'createAnything',
}
#----------------------------------------------------------------------
def initJoysticks():
"""counts all the joysticks,assigns to a list"""
print 'BEGIN JOYSTICK LOAD'
#number of joysticks connected
njoy = PG.joystick.get_count()
print '\tFound', njoy, 'joysticks.'
#add each to list
global gamePad
gamePad = []
for pad in xrange(njoy):
joy = PG.joystick.Joystick(pad)
joy.init()
print '\t\tJoystick', pad, 'has', joy.get_numbuttons(), 'buttons'
gamePad.append(joy)
print 'END JOYSTICK LOAD\n'
#except:
#print 'no gamepad'
#----------------------------------------------------------------------
def inputHandler(surface):
"""handles mouse and keyboard events, surface is the main screen"""
for e in PG.event.get():
if e.type == PG.KEYDOWN \
or e.type == PG.KEYUP:
keyHandler(e, surface)
elif e.type == PG.MOUSEBUTTONDOWN \
or e.type == PG.MOUSEBUTTONUP \
or e.type == PG.MOUSEMOTION:
mouseHandler(e, surface)
pass
#----------------------------------------------------------------------
def timeTheShit(n):
"""time the two calls, one is local the other is module"""
def here():
print 'this ones here'
import timeit
a = timeit.timeit(here, number=n)
b = timeit.timeit(toolsV2.there, number=n)
from toolsV2 import there
c = timeit.timeit(there, number=n)
print a, b, c
#----------------------------------------------------------------------
def movementControls(action, surface):
"""handles movement controls. Ex: wasd"""
print '\taction:', action
if not lists.ANYTHINGs[0].moving:
lists.ANYTHINGs[0].moving = True
print '\tdict result', dirDict[action]
lists.ANYTHINGs[0].direction = dirDict[action]
print '\tnew dir', lists.ANYTHINGs[0].direction
#else:
#lists.ANYTHINGs[0].moving = False
pass
#----------------------------------------------------------------------
def shooterControls(action):
"""handles shooter controls. Ex: shoot bullet"""
if action == 'shootBullet':
print '\t',
lists.ANYTHINGs[0].components['shooter'].fire()
pass
#----------------------------------------------------------------------
def debugControls(action, surface):
"""handles debug controls. Ex: createActor"""
if action == 'createAnything':
try:
tile = random.choice(lists.all_tiles)
pos_chosen = random.choice(tile.coords)
actors.spawnAnything(surface, loadImages.loadImages(
r'./art/man/', 'png'),
pos=pos_chosen)
except IndexError as e:
print str(e).upper(), 'grid\'s probably not made, right click and try again'
#----------------------------------------------------------------------
def keyHandler(e, surface):
"""handles only keyboard"""
#letter keys
#try:
#if chr(e.key) in range(256):
#isLetter = 1
#else:
#isLetter = 0
if e.type == PG.KEYDOWN and e.key in xrange(256):
print 'caught the "', chr(e.key), '" key'
#save the keydown key
lists.keysDown.append(chr(e.key))
#print 'added', e.key
#movement
if chr(e.key) in movementCtrlsDict.keys():
print 'start movement controls'
movementControls(movementCtrlsDict[chr(e.key)], surface)
print 'end movement controls'
#shooter
elif chr(e.key) in shooterCtrlsDict.keys():
print 'start shooter controls'
shooterControls(shooterCtrlsDict[chr(e.key)])
print 'end shooter controls'
#debug
elif chr(e.key) in debugCtrlsDict.keys():
print 'start debug controls'
debugControls(debugCtrlsDict[chr(e.key)], surface)
print 'end debug controls'
#other
else:
print e.key, 'is not assigned to a list'
if isKey(e.key,'l'):
flames = loadImages.loadImages(r'./art/flames/', 'png', (16, 16))
lists.flames = flames
constants.FLAMES = True
elif isKey(e.key,'z'):
plr = lists.ANYTHINGs[0]
#create a random anything instance
lists.ANYTHINGs[0].point2 = toolsV2.vectors.rectPerimeter(
lists.ANYTHINGs[0].rect,
lists.ANYTHINGs[0].direction)
#new way
lists.ANYTHINGs[0].point2 = toolsV2.vectors.intersect_perimeter(
plr.direction[0],
plr.direction[1],
plr.rect.w,
plr.rect.h, )
plr.point2 = toolsV2.vectors.add(plr.point2, plr.rect.center)
elif isKey(e.key,'m'):
x, y = lists.ANYTHINGs[0].pos()
PG.mouse.set_pos(x, y)
elif isKey(e.key, 'q'):
print 'facing', lists.ANYTHINGs[0].facing
print 'direction', lists.ANYTHINGs[0].direction
print 'pos', lists.ANYTHINGs[0].pos()
print 'mouse', PG.mouse.get_pos()
elif isKey(e.key, 'h'):
#loads the image of a house and counts the points that are transparent
house = loadImages.loadImages(r'./art/house/', 'house1.png', 'orig')
trans_points = []
colorkey = house.get_colorkey()
for x in xrange(house.get_rect().w-1):
for y in xrange(house.get_rect().h-1):
color = house.get_at((x, y))
if color == colorkey:
trans_points.append((x, y))
pass
#adds the transparent points to the list
lists.DEBUGs['house'] = house, trans_points
print 'number of transparent points in house', len(trans_points)
elif isKey(e.key, 'p'):
global path_found
path_found = pathing.AStar(lists.TILEs[0][0], lists.TILEs[10][22])
elif e.type == PG.KEYUP and e.key in xrange(256):
lists.keysDown.remove(chr(e.key))
#print 'removed', e.key, lists.keysDown
mixedList = [k for k in lists.keysDown if k in movementCtrlsDict.keys()]
#print 'mixed', len(mixedList)
if len(mixedList) == 0:
lists.ANYTHINGs[0].moving = False
#----------------------------------------------------------------------
def isKey(event, key):
"""if event key is key return true"""
try:
event = chr(event)
except:
print 'not a character key'
#print 'event, key:', event, key
if event == key:
return True
else: return False
#----------------------------------------------------------------------
def mouseHandler(e, surface):
"""handles only mouse events"""
#if left-click
if e.type == PG.MOUSEBUTTONDOWN and e.button == 1: # or e.type == PG.MOUSEBUTTONUP:
pass
lists.ANYTHINGs[0].moving = True
#try:
#find the angle between player, direction and mouse
isSameAngle = 0
count = 0
while not isSameAngle:
angle = toolsV2.vectors.angleOfTwoPoints(lists.ANYTHINGs[0].pos(),
lists.ANYTHINGs[0].direction,
e.pos)
print 'angle between mouse and direction', angle
#rotate direction by angle
newDirRotated = toolsV2.vectors.rotate(lists.ANYTHINGs[0].direction,
angle)
angle = toolsV2.vectors.angleOfTwoPoints(lists.ANYTHINGs[0].pos(),
newDirRotated,
e.pos)
print 'angle between mouse and newDir', angle, 'that number should be zero'
#change player dir
lists.ANYTHINGs[0].direction = newDirRotated
if angle == 0:
isSameAngle = 1
else:
#make sure it doesn't run forever
count += 1
if count > 100:
print 'stopped inf loop in rotation'
break
#except RuntimeError:
#print 'runtme error'
#pass
print '\n'
elif e.type == PG.MOUSEBUTTONUP and e.button == 1:
try:
lists.ANYTHINGs[0].moving = False
except:
print 'no one to move'
#elif right-click
elif e.type == PG.MOUSEBUTTONDOWN and e.button == 3:
lists.TILEs = toolsV2.createGrid()
pass
#elif mouse moved
elif e.type == PG.MOUSEMOTION:
pass
#elif scroll up
elif e.type == PG.MOUSEBUTTONDOWN and e.button == 4:
##rotate all by -45
#for thing in lists.ANYTHINGs:
#thing.rotate(-45)
#lists.ANYTHINGs[0].bodySurface = \
#PG.transform.scale2x(lists.ANYTHINGs[0].bodySurface)
PG.display.iconify()
#elif scroll down
elif e.type == PG.MOUSEBUTTONDOWN and e.button == 5:
##rotate player by 45
#lists.ANYTHINGs[0].rotate(45)
lists.ANYTHINGs[0].bodySurface = lists.ANYTHINGs[0].spriteList[1]
pass
#elif other buttons
elif e.type == PG.MOUSEBUTTONDOWN:
print 'button', e.button
print 'event:', e