generated from mwc/project_game
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
from retro.agent import ArrowKeyAgent
|
|
from retro.game import Game
|
|
from random import shuffle
|
|
from snack import Snack
|
|
width=25
|
|
height=25
|
|
|
|
'''Establishes keyboard arrow keys as direction vectors so each arrow key corresponds with movement
|
|
in a certain direction on the board.'''
|
|
direction_vectors = {
|
|
"KEY_RIGHT": (1, 0),
|
|
"KEY_UP": (0, -1),
|
|
"KEY_LEFT": (-1, 0),
|
|
"KEY_DOWN": (0, 1),
|
|
}
|
|
|
|
class Man:
|
|
character = "&"
|
|
color = "blue"
|
|
snack=False
|
|
mine=False
|
|
|
|
def __init__(self,position):
|
|
self.position = position
|
|
|
|
def handle_keystroke(self, keystroke, game):
|
|
'''Describes how a keystroke is received'''
|
|
if keystroke.name in direction_vectors:
|
|
vector = direction_vectors[keystroke.name]
|
|
self.try_to_move(vector, game)
|
|
|
|
def try_to_move(self, 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. Also stores the score so it can be used to determine if more mines are needed'''
|
|
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()
|