generated from mwc/project_game
Update #2, making some progress after being stuck
This commit is contained in:
parent
b1541ddf77
commit
cf8eefcb8b
Binary file not shown.
Binary file not shown.
|
@ -1,27 +0,0 @@
|
|||
from man import Man
|
||||
from snack import Snack
|
||||
from mine import Mine
|
||||
from random import shuffle
|
||||
|
||||
|
||||
class Board:
|
||||
|
||||
def __init__(self,width, height,snack_density):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.snack_density = snack_density
|
||||
|
||||
def get_agents(self):
|
||||
positions = self.get_all_positions()
|
||||
shuffle(positions)
|
||||
num_snacks = round((self.snack_density * len(positions)))
|
||||
snacks = [Snack(p) for p in positions[1:num_snacks+1]]
|
||||
agents=[Man(positions[0])] + snacks
|
||||
return agents
|
||||
|
||||
def get_all_positions(self):
|
||||
positions=[]
|
||||
for i in range(self.width):
|
||||
for j in range(self.height):
|
||||
positions.append((i,j))
|
||||
return positions
|
|
@ -1,16 +0,0 @@
|
|||
from retro.game import Game
|
||||
from board import Board
|
||||
|
||||
width = 25
|
||||
height = 25
|
||||
board = Board(width,height,.01)
|
||||
score = 0
|
||||
state= {}
|
||||
#score = 0
|
||||
#man = Man(board_size)
|
||||
game = Game(
|
||||
board.get_agents(),
|
||||
state,
|
||||
board_size = (width, height)
|
||||
)
|
||||
game.play()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,66 @@
|
|||
from man import Man
|
||||
from snack import Snack
|
||||
from mine import Mine
|
||||
from random import shuffle
|
||||
from retro import game
|
||||
|
||||
|
||||
class Board:
|
||||
|
||||
def __init__(self,width, height,num_snacks,num_mines):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.num_snacks = num_snacks
|
||||
self.num_mines = num_mines
|
||||
|
||||
def get_agents(self,num_snacks,num_mines):
|
||||
all_positions = self.get_all_positions()
|
||||
shuffle(all_positions)
|
||||
man= [Man(all_positions[0])]
|
||||
snacks = [Snack(p) for p in all_positions[1:(num_snacks+1)]]
|
||||
mines = [Mine(p) for p in all_positions[(num_snacks + 2):(num_snacks + num_mines +2)]]
|
||||
agents = man + snacks + mines
|
||||
return agents
|
||||
'''
|
||||
def get_agents(self,num_snacks,num_mines):
|
||||
man = self.get_man(num_snacks,num_mines)
|
||||
snacks = self.get_snacks(num_snacks)
|
||||
mines = self.get_mines(num_mines,num_snacks)
|
||||
|
||||
agents = snacks + mines + man
|
||||
return agents
|
||||
|
||||
def get_man(self,num_snacks,num_mines):
|
||||
|
||||
#man_location = [Man(all_positions[0])]
|
||||
#return man_location
|
||||
shuffle(all_positions)
|
||||
for position in all_positions:
|
||||
#if position in self.get_snacks(num_snacks):
|
||||
# all_positions.remove(position)
|
||||
if position in self.get_mines(num_mines,num_snacks):
|
||||
all_positions.remove(position)
|
||||
man_location = [Man(all_positions[0])]
|
||||
return man_location
|
||||
|
||||
def get_mines(self,num_mines, num_snacks):
|
||||
all_positions = self.get_all_positions()
|
||||
shuffle(all_positions)
|
||||
for position in all_positions:
|
||||
if position in self.get_snacks(num_snacks):
|
||||
all_positions.remove(position)
|
||||
mines = [Mine(p) for p in all_positions[1:num_mines+1]]
|
||||
return mines
|
||||
|
||||
def get_snacks(self,num_snacks):
|
||||
all_positions = self.get_all_positions()
|
||||
shuffle(all_positions)
|
||||
snacks = [Snack(p) for p in all_positions[1:num_snacks+1]]
|
||||
return snacks
|
||||
'''
|
||||
def get_all_positions(self):
|
||||
positions=[]
|
||||
for i in range(self.width):
|
||||
for j in range(self.height):
|
||||
positions.append((i,j))
|
||||
return positions
|
|
@ -18,11 +18,14 @@ class Man:
|
|||
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'''
|
||||
def try_to_move(self, vector, game):
|
||||
x,y = self.position
|
||||
vx,vy = vector
|
||||
|
@ -37,11 +40,13 @@ class Man:
|
|||
|
||||
if on_board:
|
||||
if obstacle:
|
||||
pass
|
||||
if obstacle.snack:
|
||||
score +=1
|
||||
game.state['Score'] += 1
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
else:
|
||||
score -=1
|
||||
game.state['Score'] -= 1
|
||||
game.remove_agent(agents[0])
|
||||
|
||||
else:
|
||||
self.position = future_position
|
||||
|
@ -51,6 +56,8 @@ class Man:
|
|||
self.color = "black"
|
||||
game.end()
|
||||
|
||||
|
||||
|
||||
|
||||
'''
|
||||
def __init__(self, position):
|
|
@ -0,0 +1,19 @@
|
|||
from retro.game import Game
|
||||
from board import Board
|
||||
|
||||
width = 25
|
||||
height = 25
|
||||
num_snacks = 10
|
||||
num_mines = 10
|
||||
board = Board(width,height,num_snacks,num_mines)
|
||||
score = 0
|
||||
state= {"Score":0}
|
||||
#man = Man(board_size)
|
||||
game = Game(
|
||||
board.get_agents(num_snacks, num_mines),
|
||||
state,
|
||||
board_size = (width, height)
|
||||
)
|
||||
#print(board.get_agents())
|
||||
print(len(board.get_agents(10,10)))
|
||||
game.play()
|
Loading…
Reference in New Issue