Skip to content

Commit

Permalink
Random movers implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
lackofcheese committed May 3, 2010
1 parent 9f24025 commit 493968f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
64 changes: 56 additions & 8 deletions game.py
Expand Up @@ -2,9 +2,9 @@
import string, random
import pygame


from grid import Grid
from terrain import TerrainData, MeteorTerrainGenerator
from mover import RandomMover

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
Expand Down Expand Up @@ -39,7 +39,7 @@
terrain = TerrainData()
gameGrid.add_node((x, y, 0), terrain)

#gameGrid.connect_grid()
gameGrid.connect_grid()

generator = MeteorTerrainGenerator()
generator.apply(gameGrid)
Expand All @@ -48,18 +48,25 @@
font = pygame.font.Font(font_file, FONT_SIZE)
font.set_bold(True)

movers = []

def rectRender(obj, loc):
""" Renders a renderable object as a rectangle
at the given grid square. """
rectX, rectY, rectZ = loc
newrectX = (rectX - viewportX) * zoom
newrectY = (rectY - viewportY) * zoom
rect = pygame.Rect(newrectX, newrectY, zoom, zoom)
obj.render(rect, screen)

# updates the screen to show the appropriate visible nodes
def updateDisplay():
screen.fill((0,0,0))
for x in range(viewportX, viewportX + columns):
for y in range(viewportY, viewportY + rows):
terrainNode = gameGrid.get_node_at((x, y, viewportZ))
if terrainNode != None:
rectx, recty, rectz = terrainNode.location
newrectx = (rectx - viewportX) * zoom
newrecty = (recty - viewportY) * zoom
rect = pygame.Rect(newrectx, newrecty, zoom, zoom)
terrainNode.contents.render(rect, screen)
if terrainNode is not None:
rectRender(terrainNode.contents, terrainNode.location)

# show current x, y, z in top left corner
current_view = (viewportX, viewportY, viewportZ, zoom, columns, rows)
Expand All @@ -68,14 +75,52 @@ def updateDisplay():
rect.x, rect.y = (0,0)
screen.blit(text, rect)

displayMovers()

pygame.display.update()

def moveMovers():
for mover in movers:
mover.move()

def isOnScreen(loc):
x, y, z = loc
x_on = x >= viewportX and x < viewportX + columns
y_on = y >= viewportY and y < viewportY + rows
z_on = z == viewportZ
return x_on and y_on and z_on

def displayMovers():
for mover in movers:
loc = mover.get_location()
if isOnScreen(loc):
rectRender(mover, loc)

updateDisplay()

pygame.key.set_repeat(800, 20) # Key repeating

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
locX = x / zoom + viewportX
locY = y / zoom + viewportY
loc = (locX, locY, viewportZ)
if event.button == 1: # Add mover
rm = RandomMover(gameGrid, (locX, locY, viewportZ))
movers.append(rm)
if event.button == 3: # Remove mover
for mover in movers:
if mover.get_location() == loc:
movers.remove(mover)
break

updateDisplay()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
viewportY += Y_SCROLL
Expand All @@ -101,4 +146,7 @@ def updateDisplay():
zoom -= ZOOM_INCREMENT
columns = SCREEN_WIDTH / zoom
rows = SCREEN_HEIGHT / zoom
if event.key == pygame.K_SPACE:
moveMovers()

updateDisplay()
2 changes: 1 addition & 1 deletion grid.py
Expand Up @@ -51,5 +51,5 @@ def connect_grid(self):

for edge in edges:
if edge is not None:
if not self.has_edge(node, edge):
if not self.has_edge((node, edge)):
self.add_edge((node, edge))
27 changes: 27 additions & 0 deletions mover.py
@@ -0,0 +1,27 @@
import random

class Mover(object):
""" A renderable object with a location, color, and movement method. """
def __init__(self, grid, location, color):
self.grid = grid
self.node = grid.get_node_at(location)
self.color = color

def move(self):
pass

def get_location(self):
return self.node.location

def render(self, area, surface):
surface.fill(self.color, area)


class RandomMover(Mover):
""" A mover that moves randomly between adjacent nodes. """
def __init__(self, grid, location, color=(255, 0, 0)):
Mover.__init__(self, grid, location, color)

def move(self):
neighbors = self.grid.neighbors(self.node)
self.node = random.choice(neighbors)
2 changes: 1 addition & 1 deletion terrain.py
Expand Up @@ -44,7 +44,7 @@ def apply(self, grid):
sqrStrikeRadius = strikeRadius * strikeRadius

if strikeNode is None:
print "Out-of-range meteor strike at ",
print "Out-of-range meteor strike at ",
print strikeLocation

for node in grid.nodes():
Expand Down

1 comment on commit 493968f

@lackofcheese
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, now you can add and remove random movers for testing purposes. Left click on a grid square to add one, right click to remove it.
Space bar will move all movers on the grid one step - hold it down if you want them to move faster.

Please sign in to comment.