project_game/gamefiles/man.py

98 lines
3.0 KiB
Python

from retro.agent import ArrowKeyAgent
#from retro.game import Game
#from helpers import add, get_occupant
from retro.game import Game
from random import shuffle
from snack import Snack
width=25
height=25
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"
snack=False
mine=False
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. Also adds 1 point if the space
contains a snack and then removes the snack. Deducts 10 points if the
space contains a mine. Checks that the score is not negative and if it
is, ends the game.'''
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'] -= 10
game.remove_agent(agents[0])
if game.state['Score'] <0:
self.die(game)
else:
pass
else:
self.position = future_position
def die(self,game):
'''Indicates what happens when the game is over.'''
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'''