-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
executable file
·38 lines (33 loc) · 1.42 KB
/
player.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
import pygame, constants
from geometry import *
class PlayerController(object):
def __init__(self, tank, turret):
self.tank = tank
self.turret = turret
# TODO: REMOVE delta from this call. Instead should register e.g. intent to
# turn left with the object, then this should be evaluated elsewhere with
# the delta.
def control(self, events, pressed, mouse, delta):
bullet_request = None
mine_request = None
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
bullet_request = { 'turret' : self.turret }
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
mine_request = { 'tank' : self.tank }
if pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
self.tank.turn_left(delta)
elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
self.tank.turn_right(delta)
if pressed[pygame.K_UP] or pressed[pygame.K_w]:
self.tank.accelerate(delta)
elif pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
self.tank.decelerate(delta)
else:
self.tank.neutral(delta)
# turret control
# This is not quite accurate, as in cases where the tank hits a wall,
# this will anticipate the tank's movement which has not actually
# occurred. Probably not a real issue.
self.turret.turn(delta, Point(float(mouse[0]) / constants.TILE_SIZE, float(mouse[1]) / constants.TILE_SIZE))
return (bullet_request, mine_request)