update, need help with man.py generate_new_snack

This commit is contained in:
Chris Mekelburg 2024-12-04 21:45:52 -05:00
parent cf8eefcb8b
commit 729e1085ee
10 changed files with 61 additions and 17 deletions

Binary file not shown.

View File

@ -1,12 +1,14 @@
#List of helper functions to move the man #List of helper functions to move the man
def add(vec0, vec1): '''def add(vec0, vec1):
"""Adds two vectors. """Adds two vectors.
Got tired of doing this by hand. Got tired of doing this by hand.
""" """
x0, y0 = vec0 x0, y0 = vec0
x1, y1 = vec1 x1, y1 = vec1
return (x0 + x1, y0 + y1) return (x0 + x1, y0 + y1)
'''
def get_occupant(game, position): def get_occupant(game, position):
"""Returns the agent at position, if there is one. """Returns the agent at position, if there is one.
@ -18,7 +20,7 @@ def get_occupant(game, position):
if position in positions_with_agents: if position in positions_with_agents:
agents_at_position = positions_with_agents[position] agents_at_position = positions_with_agents[position]
return agents_at_position[0] return agents_at_position[0]
'''
def distance(vec0, vec1): def distance(vec0, vec1):
"""Returns the distance between two vectors, using the """Returns the distance between two vectors, using the
"manhattan distance," or the distance if you can only "manhattan distance," or the distance if you can only
@ -27,4 +29,4 @@ def distance(vec0, vec1):
""" """
x0, y0 = vec0 x0, y0 = vec0
x1, y1 = vec1 x1, y1 = vec1
return abs(x1 - x0) + abs(y1 - y0) return abs(x1 - x0) + abs(y1 - y0)'''

View File

@ -1,7 +1,11 @@
from retro.agent import ArrowKeyAgent from retro.agent import ArrowKeyAgent
#from retro.game import Game #from retro.game import Game
#from helpers import add, get_occupant #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 = { direction_vectors = {
"KEY_RIGHT": (1, 0), "KEY_RIGHT": (1, 0),
@ -25,7 +29,10 @@ class Man:
vector = direction_vectors[keystroke.name] vector = direction_vectors[keystroke.name]
self.try_to_move(vector, game) 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): def try_to_move(self, vector, game):
x,y = self.position x,y = self.position
vx,vy = vector vx,vy = vector
@ -43,22 +50,39 @@ class Man:
if obstacle.snack: if obstacle.snack:
game.state['Score'] += 1 game.state['Score'] += 1
game.remove_agent(agents[0]) game.remove_agent(agents[0])
self.generate_new_snack()
else: else:
game.state['Score'] -= 1 game.state['Score'] -= 10
game.remove_agent(agents[0]) game.remove_agent(agents[0])
if game.state['Score'] <0:
self.die(game)
else:
pass
else: else:
self.position = future_position self.position = future_position
'''Indicates what happens when the game is over.'''
def die(self,game): def die(self,game):
game.state["message"] = "Game Over" game.state["message"] = "Game Over"
self.color = "black" self.color = "black"
game.end() 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): def __init__(self, position):
self.position = position self.position = position

View File

@ -6,3 +6,5 @@ class Mine:
def __init__(self,position): def __init__(self,position):
self.position = position self.position = position

View File

@ -1,19 +1,19 @@
from retro.game import Game from retro.game import Game
from board import Board from board import Board
from score import num_mines
width = 25 width = 25
height = 25 height = 25
num_snacks = 10
num_mines = 10
board = Board(width,height,num_snacks,num_mines)
score = 0
state= {"Score":0} state= {"Score":0}
#man = Man(board_size) score =state["Score"]
num_snacks = 10
num_mines = num_mines(score)
board = Board(width,height,num_snacks,num_mines)
game = Game( game = Game(
board.get_agents(num_snacks, num_mines), board.get_agents(num_snacks, num_mines),
state, state,
board_size = (width, height) board_size = (width, height)
) )
#print(board.get_agents())
print(len(board.get_agents(10,10)))
game.play() game.play()

15
gamefiles/score.py Normal file
View File

@ -0,0 +1,15 @@
def num_mines(score):
if score < 5:
num_mines = 10
if 10 <= score <50:
num_mines = 20
if 50 <= score <75:
num_mines = 30
if 75 <= score <100:
num_mines = 40
#else:
#num_mines = 50
return num_mines

View File

@ -6,3 +6,4 @@ class Snack:
def __init__(self,position): def __init__(self,position):
self.position = position self.position = position