-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
51 lines (41 loc) · 1.43 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
39
40
41
42
43
44
45
46
47
48
49
50
51
import controller
from constants import *
class Player(object):
def __init__(self, x, y, game, id=0):
self.x = x
self.y = y
self.game = game
self.game.board[x][y] = (self, id)
self.controller = controller.KeyboardController()
self.has_been_hit = False
self.id = id
self.age = 0
def __str__(self):
return PLAYER_TILE
def update(self, events):
if self.has_been_hit:
self.die()
else:
self.controller.update(events)
def move(self):
""" Moves in a direction determined by the controller. Then, if the new location is occupied, dies. """
dead_players = set()
self.game.board[self.x][self.y] = (TAIL_TILE, self.id)
dead_players = set()
my_move = self.controller.get_move()
self.x += my_move[0]
self.y += my_move[1]
# Move to new tile if not empty
collision_tile = self.game.board[self.x][self.y]
if str(collision_tile[0]) is EMPTY_TILE:
self.game.board[self.x][self.y] = (self, self.id)
elif str(collision_tile[0]) is PLAYER_TILE:
dead_players.add(self)
dead_players.add(collision_tile[0])
else:
dead_players.add(self)
self.age += 1
return dead_players
def die(self):
# self.game.board[self.x][self.y] = (TAIL_TILE, self.id)
self.game.players.remove(self)