generated from mwc/project_game
almost done, just one issue with display score
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
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
|
||||
|
||||
'''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),
|
||||
@@ -15,7 +15,7 @@ direction_vectors = {
|
||||
}
|
||||
|
||||
class Man:
|
||||
character = "&" #try google asci full table?,
|
||||
character = "&"
|
||||
color = "blue"
|
||||
snack=False
|
||||
mine=False
|
||||
@@ -23,18 +23,17 @@ class Man:
|
||||
def __init__(self,position):
|
||||
self.position = position
|
||||
|
||||
'''Describes how a keystroke is received'''
|
||||
|
||||
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)
|
||||
|
||||
'''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):
|
||||
'''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)
|
||||
@@ -45,7 +44,6 @@ class Man:
|
||||
obstacle = agents[0]
|
||||
else:
|
||||
obstacle= None
|
||||
|
||||
if on_board:
|
||||
if obstacle:
|
||||
if obstacle.snack:
|
||||
@@ -61,38 +59,8 @@ class Man:
|
||||
else:
|
||||
self.position = future_position
|
||||
|
||||
|
||||
def die(self,game):
|
||||
'''Indicates what happens when the game is over.'''
|
||||
game.state["message"] = "Game 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'''
|
||||
Reference in New Issue
Block a user