generated from mwc/project_game
update, need help with man.py generate_new_snack
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
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),
|
||||
@@ -25,7 +29,10 @@ class Man:
|
||||
vector = direction_vectors[keystroke.name]
|
||||
self.try_to_move(vector, game)
|
||||
|
||||
'''Checks if a space is avialable to move'''
|
||||
'''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
|
||||
@@ -43,22 +50,39 @@ class Man:
|
||||
if obstacle.snack:
|
||||
game.state['Score'] += 1
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
self.generate_new_snack()
|
||||
else:
|
||||
game.state['Score'] -= 1
|
||||
game.state['Score'] -= 10
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
if game.state['Score'] <0:
|
||||
self.die(game)
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
self.position = future_position
|
||||
|
||||
'''Indicates what happens when the game is over.'''
|
||||
def die(self,game):
|
||||
game.state["message"] = "Game Over"
|
||||
self.color = "black"
|
||||
game.end()
|
||||
|
||||
|
||||
|
||||
def get_all_positions(self):
|
||||
positions=[]
|
||||
for i in range(width):
|
||||
for j in range(height):
|
||||
positions.append((i,j))
|
||||
return positions
|
||||
|
||||
'''issue here, is_empty is not generating a coordinate positon,
|
||||
but I am not sure why it is not'''
|
||||
def generate_new_snack(self):
|
||||
all_positions=self.get_all_positions()
|
||||
shuffle(all_positions)
|
||||
index =0
|
||||
while Game.is_empty(all_positions[index]) == False:
|
||||
index = index + 1
|
||||
Snack(all_positions[index])
|
||||
'''
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
Reference in New Issue
Block a user