generated from mwc/project_game
Start of nav_game
I started my game modelled after the beast game. This is my work so far, but there is still a lot to do!
This commit is contained in:
parent
6acd59b370
commit
66869aac1a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
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
|
|
@ -0,0 +1,30 @@
|
|||
#List of helper functions to move the man
|
||||
|
||||
def add(vec0, vec1):
|
||||
"""Adds two vectors.
|
||||
Got tired of doing this by hand.
|
||||
"""
|
||||
x0, y0 = vec0
|
||||
x1, y1 = vec1
|
||||
return (x0 + x1, y0 + y1)
|
||||
|
||||
def get_occupant(game, position):
|
||||
"""Returns the agent at position, if there is one.
|
||||
This function slightly simplifies the process of getting the
|
||||
agent at a position: the game returns a list of agents at a position,
|
||||
because some games allow more than one agent at a position.
|
||||
"""
|
||||
positions_with_agents = game.get_agents_by_position()
|
||||
if position in positions_with_agents:
|
||||
agents_at_position = positions_with_agents[position]
|
||||
return agents_at_position[0]
|
||||
|
||||
def distance(vec0, vec1):
|
||||
"""Returns the distance between two vectors, using the
|
||||
"manhattan distance," or the distance if you can only
|
||||
move in the x-direction or the y-direction, but not
|
||||
diagonally. Just like walking blocks in Manhattan :)
|
||||
"""
|
||||
x0, y0 = vec0
|
||||
x1, y1 = vec1
|
||||
return abs(x1 - x0) + abs(y1 - y0)
|
|
@ -0,0 +1,79 @@
|
|||
from retro.agent import ArrowKeyAgent
|
||||
#from retro.game import Game
|
||||
#from helpers import add, get_occupant
|
||||
|
||||
|
||||
direction_vectors = {
|
||||
"KEY_RIGHT": (1, 0),
|
||||
"KEY_UP": (0, -1),
|
||||
"KEY_LEFT": (-1, 0),
|
||||
"KEY_DOWN": (0, 1),
|
||||
}
|
||||
|
||||
class Man:
|
||||
character = "&" #try google asci full table?,
|
||||
color = "blue"
|
||||
#name = "man"
|
||||
|
||||
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):
|
||||
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:
|
||||
pass
|
||||
if obstacle.snack:
|
||||
score +=1
|
||||
else:
|
||||
score -=1
|
||||
|
||||
else:
|
||||
self.position = future_position
|
||||
|
||||
def die(self,game):
|
||||
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'''
|
|
@ -0,0 +1,8 @@
|
|||
class Mine:
|
||||
character = "*"
|
||||
color = "green"
|
||||
snack=False
|
||||
|
||||
|
||||
def __init__(self,position):
|
||||
self.position = position
|
|
@ -0,0 +1,16 @@
|
|||
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()
|
|
@ -0,0 +1,8 @@
|
|||
class Snack:
|
||||
character = "o"
|
||||
color = "red"
|
||||
snack=True
|
||||
|
||||
|
||||
def __init__(self,position):
|
||||
self.position = position
|
Loading…
Reference in New Issue