Skip to content

Instantly share code, notes, and snippets.

@kennethlove
Last active August 29, 2015 14:17
Show Gist options
  • Save kennethlove/0321e604f2fde8189b62 to your computer and use it in GitHub Desktop.
Save kennethlove/0321e604f2fde8189b62 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import logging
from sys import exit
from random import randint
logging.basicConfig(filename='game.log', level=logging.DEBUG)
try:
# raw_input became input in Python 3
input = raw_input
except NameError:
# We're in Python 3 so we can ignore the exception
pass
class Scene(object):
def enter(self):
print("this scene is not yet configured. "
"subclass it and implement enter")
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
logging.info("current: {}".format(current_scene))
logging.info("last: {}".format(last_scene))
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
logging.info("next: {}".format(next_scene_name))
current_scene.enter()
class Death(Scene):
quips = [
"you died. you're bad at this game",
"unreal man, this is awful! you ded",
"were you playing the game, or did your mom tell you to pause it?",
"that is the worst game playing i've ever seen,\n and we're all "
"more stupid from having watched it."
]
def enter(self):
print(Death.quips[randint(0, len(self.quips)-1)])
exit(1)
class CentralCorridor(Scene):
def enter(self):
print("The Gothons of plane Percal #25 have invaded your ship "
"and destroyed")
print("your entire crew. you are the last surviving member and "
"your last")
print("mission is to get the neutron destruct bomb from the weapons "
"armory,")
print("put it in the bringe, and blow the ship up after getting into "
"an escape pod.")
print("\n")
print("You're running down the central corridor to the weapons "
"armory when ")
print("a Gothon jummps out, red scaly skin, dark grimy teeth, and "
"evil clown costume")
print("flowin around his hate filled body. he's blocking the door to "
"the")
print("armory and about to pull a weapon to blast you")
print("you can SHOOT, DODGE or TELL A JOKE")
action = input('> ')
if action == "shoot":
print("Quick on the draw you yank out your blaster and fire it at "
"the Gothon.")
print('his clown costume is flowing and moving around his body, '
'which throws')
print('off your aim. Your laser hits his costume but misses him '
'completely. this')
print('completely ruins his brand new costume his mother bought '
'him, which')
print('makes him fly into an insane rage and blas you \n repetedly'
' in the face until you are dead')
print('then he eats you')
return 'death'
elif action == 'dodge':
print('likie a world class boxer you dodge, weave, slip and slide '
'right')
print('as the Gothon\'s blaster cranks a laser past your head.')
print('in the middle of your artful dodge your food slips and you')
print('bang your head on the metal wall and pass out')
print('you wake up shortly after only to die as the gothon stomps '
'on')
print('your head and eats you')
return 'death'
elif action == 'tell a joke':
print('lucky for you they made you learn Gothon insults in the '
'academy.')
print(' you tell the one gothon joke you know:')
print('lbhe zbhurhkd ididdi nnndnnndn idjhfhi dsilskj;fu yhdhd '
'kelsh id hd ')
print('The Gothon stops, tries not to laugh, then busts out '
'laughing and can\'t move')
print('while he\'s laughing you run up and shoot him square in '
'the head')
print('pulling him down, then jump through the Weapon Armory '
'Door.')
return 'laser_weapon_armory'
else:
print('DOES NOT COMPUTE')
return 'central_corridor'
class LaserWeaponArmory(Scene):
def enter(self):
print('you do a dive roll into the Weapon Armory, crouch and scan '
'the room')
print('for more Gothons that might be hiding. It\'s dead quiet, '
'too quiet...')
print('you stand up and run to the far side of the room and find the ')
print('neutron bomb in it\'s container. Theres a keypad lock on the '
'box')
print('and you need the code to get the bomb out. if you get '
'the code')
print('wrong 10 times, then the lock closes forever and you can\'t ')
print('get the bomb. the code is 3 digits.')
code = '%d%d%d' % (randint(1, 9), randint(1, 9), randint(1, 9))
guess = input('[keypad]> ')
guesses = 0
while guess != code and guesses < 9:
print('BZZZZZZZED')
guesses += 10
guess = input('[keypad]> ')
if guess == code:
print('the container clicks open and the seal breaks, letting '
'gas out.')
print('you grab the neutron bomb and run as fast as you '
'can to the ')
print('bridge where you must place it in the right spot')
return 'the_bridge'
class TheBridge(Scene):
def enter(self):
print('you burst onto the bridge with the netron destruct bomb')
print('under your arm and surprise 5 Gothons who are trying to')
print(' take control of the ship. each of them has an even uglier')
print('clown costume than the last. they haven\'t pulled their')
print('weapons out yet, as they see the active bomb under your ')
print('arm and don\t want to set it off')
print('you can THROW THE BOMB or SLOWLY PLACE THE BOMB ')
action = input('> ')
if action == 'throw the bomb':
print('in a panic you throw the bomb at the group of gothons')
print('and make a leap for the door. right as you drop it a')
print('gothon shoots you right in the back killing you.')
print('As you die you see another gothon frantically try to '
'disarm')
print('the bomb. You die knowing they will probably blow up when')
print('it goes off')
return 'death'
elif action == 'slowly place the bomb':
print('you point your blaster at the bomb under your '
'arm')
print('and the gothons put their hands up and start to sweat.')
print('you inch backward to the door, open it and then carefully')
print('place the bomb on the floor, pointing your blaster at it.')
print('You then jump back through the door, punch the close '
'button')
print('and blast the lock so the Gothons can\'t get out')
print('Now that the bomb is placed you run to the escape pod to')
print('get off this tin can')
return 'escape_pod'
else:
print('DOES NOT COMPUTE!!')
return 'the_bridge'
class EscapePod(Scene):
def enter(self):
print('You rush through the ship desperately trying to make it to')
print('the escape pod before the whole ship explodes. it seems like')
print('hardly any Gothons are on the ship, so your run is clear of')
print('interference. You get to the chamber with the pods, and ')
print('now need to pick one to take. Some of them could be damaged')
print('but you don\'t have time to look. There are 5 pods, which one')
print('do you take?')
print('choose 1-5')
good_pod = randint(1, 6)
guess = input('[pod]> ')
if int(guess) != good_pod:
print('You jump into pod $s and hit the eject button.' % guess)
print('the pod escapes out into the void of space, then')
print('implodes as the hull ruptures, crushing your body')
print('into jam jelly')
return 'death'
else:
print('you jump into pod %s and hit the eject button.' % guess)
print('the pod easilyi slides out into space heading to')
print('the planet below. as it flies to the planet, you look')
print('back and see your ship implode then explode like a ')
print('bright star, taking out the Gothon ship at the same ')
print('time. You Won!')
return 'finished'
class Finished(Scene):
def enter(self):
print("Dude, some fine space flying and stuff. You Win!")
return 'finished'
class Map(object):
scenes = {
'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod(),
'death': Death(),
'finished': Finished(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment