Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/9dc3bec2980073b58033 to your computer and use it in GitHub Desktop.
Save QuantumFractal/9dc3bec2980073b58033 to your computer and use it in GitHub Desktop.
Design Patterns
'''
This is my implementation of the Command design pattern.
It decouples simple actions into a "Command" class that is
inherited by FireCommand, etc.
Written in python for simplicity sake.
For more info >> http://gameprogrammingpatterns.com/command.html
'''
class Command:
def execute(self, actor):
pass
class FireCommand(Command):
def execute(self, actor):
actor.fire()
class JumpCommand(Command):
def execute(self, actor):
actor.jump()
class YellCommand(Command):
def execute(self, actor):
actor.yell()
class GameActor():
def __init__(self, name=None, hp=100):
self.name , self.hp = name , hp
def fire(self):
print "Pew pew pew!"
def jump(self):
print "I jumped!"
def yell(self):
print "I AM "+str(self.name)+"!"
#Setup our command handlers
fire = FireCommand()
jump = JumpCommand()
yell = YellCommand()
#Make a game actor object
actor = GameActor("Tom", 200)
#Fire off some commands to an actor
fire.execute(actor)
jump.execute(actor)
yell.execute(actor)
'''
Output:
Pew pew pew!
I jumped!
I AM Tom!
'''
'''
Flyweight. A design pattern used solely for efficiency with
large arrays of nearly indentical objects.
A simple world generator
Written in python for simplicity sake.
For more info >> http://gameprogrammingpatterns.com/command.html
'''
from random import *
class World:
def __init__(self, width, height):
self.grass = Terrain(1, False, "grass.png")
self.hill = Terrain(3, False, "hill.png")
self.water = Terrain(2, True, "water.png")
self.WIDTH, self.HEIGHT = width, height
self.grid = [[None for i in range(height)] for j in range(width)]
def __str__(self):
output = ""
for x in xrange(0, self.WIDTH):
for y in xrange(0, self.HEIGHT):
output+= "["+str(self.grid[x][y].get_movement_cost())+"]"
output+= "\n"
return output
def paint_terrain(self):
for x in range(self.WIDTH):
for y in range(self.HEIGHT):
if randint(1,10) == 10:
self.grid[x][y] = self.hill
else:
self.grid[x][y] = self.grass
def get_movement_cost(self,x,y):
return self.grid[x][x].get_movement_cost()
class Terrain:
def __init__(self, movement_cost, is_water, texture):
self.movement_cost, self.is_water, self.texture = movement_cost , is_water, texture
def get_movement_cost(self):
return self.movement_cost
def is_water(self):
return is_water
new_world = World(20, 15)
new_world.paint_terrain()
print new_world
'''
Output:
[1][1][3][1][3][1][1][3][1][3][1][1][3][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[1][1][1][1][3][1][1][1][1][1][3][1][1][1][1]
[1][3][3][1][3][1][1][1][1][1][1][1][1][3][1]
[1][1][1][1][1][1][1][3][1][1][1][1][1][1][1]
[1][1][1][1][3][1][3][1][1][1][1][3][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][3]
[1][1][1][3][1][1][1][1][1][1][1][3][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[3][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[3][1][3][1][1][3][1][1][1][1][1][1][1][1][1]
[1][3][3][1][3][3][1][1][1][1][1][1][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[1][1][1][3][3][3][1][1][1][1][1][1][1][1][1]
[1][1][1][3][1][3][1][1][3][1][1][1][1][1][1]
[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
[1][1][1][1][1][1][1][3][1][1][1][1][1][1][1]
[1][1][1][3][1][1][1][1][1][3][1][1][1][1][1]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment