generated from mwc/project_game
Update #2, making some progress after being stuck
This commit is contained in:
86
gamefiles/man.py
Normal file
86
gamefiles/man.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from retro.agent import ArrowKeyAgent
|
||||
#from retro.game import Game
|
||||
#from helpers import add, get_occupant
|
||||
|
||||
|
||||
direction_vectors = {
|
||||
"KEY_RIGHT": (1, 0),
|
||||
"KEY_UP": (0, -1),
|
||||
"KEY_LEFT": (-1, 0),
|
||||
"KEY_DOWN": (0, 1),
|
||||
}
|
||||
|
||||
class Man:
|
||||
character = "&" #try google asci full table?,
|
||||
color = "blue"
|
||||
#name = "man"
|
||||
|
||||
def __init__(self,position):
|
||||
self.position = position
|
||||
|
||||
'''Describes how a keystroke is received'''
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
if keystroke.name in direction_vectors:
|
||||
vector = direction_vectors[keystroke.name]
|
||||
self.try_to_move(vector, game)
|
||||
|
||||
'''Checks if a space is avialable to move'''
|
||||
def try_to_move(self, vector, game):
|
||||
x,y = self.position
|
||||
vx,vy = vector
|
||||
future_position = (x +vx, y+vy)
|
||||
on_board = game.on_board(future_position)
|
||||
agents_by_position = game.get_agents_by_position()
|
||||
agents = agents_by_position[future_position]
|
||||
if len(agents)>0:
|
||||
obstacle = agents[0]
|
||||
else:
|
||||
obstacle= None
|
||||
|
||||
if on_board:
|
||||
if obstacle:
|
||||
if obstacle.snack:
|
||||
game.state['Score'] += 1
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
else:
|
||||
game.state['Score'] -= 1
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
else:
|
||||
self.position = future_position
|
||||
|
||||
def die(self,game):
|
||||
game.state["message"] = "Game Over"
|
||||
self.color = "black"
|
||||
game.end()
|
||||
|
||||
|
||||
|
||||
|
||||
'''
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
if keystroke.name in direction_vectors:
|
||||
vector = direction_vectors[keystroke.name]
|
||||
self.try_to_move(vector, game)
|
||||
|
||||
def try_to_move(self, vector, game):
|
||||
"""Tries to move the player in the direction of vector.
|
||||
If the space is empty and it's on the board, then the move succeeds.
|
||||
If the space is occupied, then if the occupant can be pushed, it gets
|
||||
pushed and the move succeeds. Otherwise, the move fails.
|
||||
"""
|
||||
future_position = add(self.position, vector)
|
||||
on_board = game.on_board(future_position)
|
||||
obstacle = get_occupant(game, future_position)
|
||||
if obstacle:
|
||||
if obstacle.deadly:
|
||||
self.die(game)
|
||||
elif obstacle.handle_push(vector, game):
|
||||
self.position = future_position
|
||||
elif on_board:
|
||||
self.position = future_position'''
|
||||
Reference in New Issue
Block a user