Skip to content

Commit

Permalink
Final porting commit
Browse files Browse the repository at this point in the history
- Everything's been ported over
- Drawing and coordinate system was improved and now makes much more sense
- Other minor improvements to game logic
  • Loading branch information
dtgay committed May 16, 2012
1 parent 8a7bee2 commit a0bb4d6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 42 deletions.
3 changes: 3 additions & 0 deletions pyCaveExplorer/constants.py
Expand Up @@ -15,6 +15,9 @@

MINIMUM_MOVES = 3 # Number of moves the goal must be from the start point

# Directions
DIRECTIONS = ['n', 's', 'e', 'w']

# Colors
COLOR_BLACK = 0, 0, 0
COLOR_BLUE = 0, 0, 255
Expand Down
9 changes: 5 additions & 4 deletions pyCaveExplorer/game.py
Expand Up @@ -32,7 +32,7 @@ def populate_grid(self):
'''
# SOLVER
self.solver = Solver()
self.solver.populate()
self.solver.populate(self.solver.grid)
for row in self.solver.grid:
for item in row:
square_surface = pygame.Surface((TILESIZE_X, TILESIZE_Y))
Expand All @@ -43,8 +43,7 @@ def populate_grid(self):
print "item x", item.x # DEBUG
print "item y", item.y # DEBUG

element = self.solver.grid[item.x / TILESIZE_X] \
[item.y / TILESIZE_Y]
element = self.solver.grid[item.x][item.y]

print "Element", element # DEBUG

Expand Down Expand Up @@ -74,7 +73,9 @@ def populate_grid(self):
square_surface.blit(contents_surface, (10, 10))

# Draw the square onto the grid
self.window.blit(square_surface, (item.x, item.y))
self.window.blit(square_surface, (item.x * TILESIZE_X,
TILESIZE_Y * item.y))
self.solver.get_grid_path()

def draw(self, surface):
'''
Expand Down
57 changes: 35 additions & 22 deletions pyCaveExplorer/path.py
Expand Up @@ -12,46 +12,59 @@
'''

class Path(GameElement):
def __init__(self, *args, **kwargs):
def __init__(self, ps, *args, **kwargs):
super(Path, self).__init__(*args, **kwargs)
self.group = ELEMENT_PATH
self.neighbors = {'n': None, 's': None, 'e': None, 'w': None}
self.contents = [] # tile's "inventory"
self.light = 0 # how brightly lit the tile is (0 is unlit)
self.passable = True # can the player be in this square?
# May reimplement functionality of the below variable... -- odd
# May reimplement functionality of the below variables... -- odd
self.map_checked = False # has this path been map checked?

self.parent_solver = ps

def show_items(self):
for item in self.contents:
pass # PORT: game.doc.addChild(contents[i]);

def find_neighbors(self):
# West
if self.x > 0 and game.solver.grid[x-1][y].group == ELEMENT_WALL:
self.neighbors['w'] = None
else:
self.neighbors['w'] = game.solver.grid[x-1][y]
if self.x > 0:
if self.parent_solver.grid[self.x-1][self.y].group == ELEMENT_WALL:
self.neighbors['w'] = None
else:
self.neighbors['w'] = self.parent_solver.grid[self.x-1][self.y]

# North
if self.y > 0 and game.solver.grid[x][y-1].group == ELEMENT_WALL:
self.neighbors['n'] = None
else:
self.neighbors['n'] = game.solver.grid[x][y-1]
if self.y > 0:
if self.parent_solver.grid[self.x][self.y-1].group == ELEMENT_WALL:
self.neighbors['n'] = None
else:
self.neighbors['n'] = self.parent_solver.grid[self.x][self.y-1]

# East
if (self.x + 1) < len(game.solver.grid) \
and game.solver.grid[x+1][y].group == ELEMENT_WALL:
self.neighbors['e'] = None
else:
self.neighbors['e'] = game.solver.grid[x+1][y]
if (self.x + 1) < len(self.parent_solver.grid):
if self.parent_solver.grid[self.x+1][self.y].group == ELEMENT_WALL:
self.neighbors['e'] = None
else:
self.neighbors['e'] = self.parent_solver.grid[self.x+1][self.y]

# South
if (self.y + 1) < len(game.solver.grid[x]) \
and game.solver.grid[x][y+1].group == ELEMENT_WALL:
self.neighbors['s'] = None
else:
self.neighbors['s'] = game.solver.grid[x][y+1]

if (self.y + 1) < len(self.parent_solver.grid[self.x]):
if self.parent_solver.grid[self.x][self.y+1].group == ELEMENT_WALL:
self.neighbors['s'] = None
else:
self.neighbors['s'] = self.parent_solver.grid[self.x][self.y+1]

def grab_neighbors(self, path, step=0):
self.map_checked = True
step += 1
print 'grab_neighbors() step print:', step # DEBUG

if self.contents[0].group == ELEMENT_GOAL:
print 'Found goal at step', step
self.parent_solver.path_lengths.append(step)
for d in DIRECTIONS:
if self.neighbors[d] != None and self.neighbors[d] != path \
and self.neighbors[d].map_checked != False:
self.neighbors[d].grab_neighbors(self, step)
66 changes: 50 additions & 16 deletions pyCaveExplorer/solver.py
Expand Up @@ -2,7 +2,7 @@
Generates and solves the grid in pyCaveExplorer
'''

import random # perhaps be more specific?
import random # perhaps be more specific? -- odd
from constants import *
from path import Path
from wall import Wall
Expand All @@ -14,50 +14,84 @@ class Solver:
def __init__(self):
# GAME INITIALIZATION #
self.grid = [
[Path(i, x) for x in range(GRID_HEIGHT)]
[Path(self, i, x) for x in range(GRID_HEIGHT)]
for i in range(GRID_WIDTH)
]
self.path_lengths = [] # stores lengths of working paths
self.shortest_path = -1 # shortest working path from start to goal

def populate(self):
def populate(self, pop_grid):
'''Populate the grid'''
# TODO: X and Y coordinates for drawing are being done strangley.
# Once we get to screen drawing, we'll have to figure that out.
# I sort of half-assedly did it for now. Not sure if it'd work.
self.start_point = None # start point has not been placed
self.goal_point = None # end point has not been placed
for row in self.grid:
for row in pop_grid:
for col in row:
square_type = random.randint(0, 1)
# 0 : make a wall.
# 1 : make a path.
if square_type == 0:
self.grid[col.x][col.y] = \
Wall(col.x * TILESIZE_X, col.y * TILESIZE_Y)
pop_grid[col.x][col.y] = \
Wall(col.x, col.y)
print("Wall generated at {}, {}".format(col.x, col.y))
elif square_type == 1:
self.grid[col.x][col.y] = \
Path(col.x * TILESIZE_X, col.y * TILESIZE_Y)
pop_grid[col.x][col.y] = \
Path(self, col.x, col.y)
print("Path generated at {}, {}".format(col.x, col.y)),
path_type = random.randint(0, (TILE_TYPES - 1))
# 0 : make starting point (if not set)
# 1 : make goal point (if not set)
# 2 : make treasure
# 3-9 : do nothing
self.grid[col.x][col.y].contents = []
pop_grid[col.x][col.y].contents = []
if path_type == 0 and not self.start_point:
self.start_point = Start()
self.grid[col.x][col.y].contents.append(self.start_point)
self.start_point = Start(col.x, col.y)
pop_grid[col.x][col.y].contents.append(self.start_point)
print "\tStart point placed here!"
elif path_type == 1 and not self.goal_point:
self.goal_point = Goal()
self.grid[col.x][col.y].contents.append(self.goal_point)
self.goal_point = Goal(col.x, col.y)
pop_grid[col.x][col.y].contents.append(self.goal_point)
print "\tGoal point placed here!"
elif path_type == 2:
self.grid[col.x][col.y].contents.append(Treasure())
pop_grid[col.x][col.y].contents.append(Treasure(
col.x, col.y))
print "\tTreasure placed here!"
else:
print "\t"
if self.start_point != None and self.goal_point != None:
print 'Solver has start and goal, tracing path...' # DEBUG
for row in pop_grid:
for col in row:
if pop_grid[col.x]\
[col.y].group != ELEMENT_WALL:
pop_grid[col.x]\
[col.y].find_neighbors()
else:
print 'Missing start or goal, trying again...' # DEBUG
# there was something commented out here about "wallSpaces"? -- odd
self.populate(pop_grid)

def test_grid(self):
'''Test to see if the grid is good for play'''
def set_player_start(self):
'''Drop player on the start location'''
# could set player x/y here
# we'll do this later, just so we do it right based on how
# I've ported it -- odd
pass

def correct_display_order(self):
'''Shift special objects to top of display list'''
pass # nothing was in this function when I ported it -- odd

def get_grid_path(self):
'''Find a path to the goal to check if grid is solvable'''
self.grid[self.start_point.x][self.start_point.y].grab_neighbors(
self.grid[self.start_point.x][self.start_point.y], -1)
# The above seems kinda janky. Man, I love that word. "Janky". -- odd
if len(self.path_lengths) > 0:
print 'Found paths, lengths:' # DEBUG
for l in self.path_lengths:
print l # DEBUG
else:
print 'No paths found'

0 comments on commit a0bb4d6

Please sign in to comment.