forked from williamfiset/Survival
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventResponder.py
115 lines (76 loc) · 3.66 KB
/
EventResponder.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
"""
The eventResponder module takes care of all user input (mouse & keyboard)
"""
import pygame
import sys
from Tile import Tile
from Bullet import Bullet
from Character import Direction
__author__ = 'William Fiset, Alex Mason'
class EventResponder:
def __init__(self):
pass
@staticmethod
def userInteraction(screen, survivor):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Press 'e' to cycle character weapon
if event.key == pygame.K_e:
# Change weapon
survivor.cycle_weapon()
# Grab a list containing all the pressed keys
keys = pygame.key.get_pressed()
""" Character rotation with w,a,s,d keys """
if keys[pygame.K_w]: # North
future_tile_number = survivor.get_number() - Tile.V
if future_tile_number in range(1, Tile.total_tiles + 1):
future_tile = Tile.get_tile(future_tile_number)
if future_tile.walkable:
survivor.set_target(future_tile)
survivor.rotate(Direction.NORTH)
# survivor.y -= survivor.height
if keys[pygame.K_s]: # South
future_tile_number = survivor.get_number() + Tile.V
if future_tile_number in range(1, Tile.total_tiles + 1):
future_tile = Tile.get_tile(future_tile_number)
if future_tile.walkable:
survivor.set_target(future_tile)
survivor.rotate(Direction.SOUTH)
# survivor.y += survivor.height
if keys[pygame.K_a]: # West
future_tile_number = survivor.get_number() - Tile.H
if future_tile_number in range(1, Tile.total_tiles + 1):
future_tile = Tile.get_tile(future_tile_number)
if future_tile.walkable:
survivor.set_target(future_tile)
survivor.rotate(Direction.WEST)
# survivor.x -= survivor.width
if keys[pygame.K_d]: # East
future_tile_number = survivor.get_number() + Tile.H
if future_tile_number in range(1, Tile.total_tiles + 1):
future_tile = Tile.get_tile(future_tile_number)
if future_tile.walkable:
survivor.set_target(future_tile)
survivor.rotate(Direction.EAST)
# survivor.x += survivor.width
""" Character firing Bullet with arrow keys """
bullet_velocity = survivor.current_weapon.bullet_velocity
if keys[pygame.K_LEFT]:
survivor.rotate(Direction.WEST)
Bullet(survivor.centerx, survivor.centery, -bullet_velocity, 0,
Direction.WEST, survivor.get_bullet_type_based_on_weapon())
elif keys[pygame.K_RIGHT]:
survivor.rotate(Direction.EAST)
Bullet(survivor.centerx, survivor.centery, bullet_velocity, 0,
Direction.EAST, survivor.get_bullet_type_based_on_weapon())
elif keys[pygame.K_UP]:
survivor.rotate(Direction.NORTH)
Bullet(survivor.centerx, survivor.centery, 0, -bullet_velocity,
Direction.NORTH, survivor.get_bullet_type_based_on_weapon())
elif keys[pygame.K_DOWN]:
survivor.rotate(Direction.SOUTH)
Bullet(survivor.centerx, survivor.centery, 0, bullet_velocity,
Direction.SOUTH, survivor.get_bullet_type_based_on_weapon())