# Better way to code this game? [closed]

I'm making this little game for programming class. Not as an assignment, but as a small side project. Just wanted to point out I do not know a lot about Python, so please don't be harsh, but I can take constructive criticism.

from scene import *
from random import random
import console
import canvas

console.set_color(0.00, 0.00, 0.00)
console.set_font('Avenir', 18)

s = "Welcome to the Adventure."
width = 160
s = s.center(width)
print( s )

t = "To select a choice, simply type a number corresponding to your choice and press enter. Enjoy!"
width = 125
t = t.center(width)
print( t )

name = raw_input('\nWhat is your name?')

print 'You hear a strange sound, kind of like the tenderizing of meat.'
print '2. Keep them shut'
open = input('> ')

if open == "1":
print "You open your eyes, you look down and see you're dressed in a light blue gown. Upon further inspection you notice where you are, a hospital. To your left is a door blocked by a heavy cabinet. To your right is a window."
print '1. Look at your wrist tag'
print '2. Approach the barricaded door'
print '3. Approach the window'

rp = input('> ')

if rp == "1":
print'----------------------'
print name
print 'Camden, NJ'
print 'DOB: 1/13/89'
print 'Date: 10/15/13'
print'----------------------'
print'2013...?' # story will develop more

if rp == "2":
print 'You approach the door and try to open it, and the odd sounds of tenderizing meat stops...'
print 'BANG BANG! You jump back in fear...'

if rp == "3" or blue == "2":
print 'You step closer to the window...thick wooden boards lay'
print 'across the window, you manage to look through the small gaps, but'
print 'all you can see is some fries and a bench.'
print 'What next?'
print '1. Open Window'
print '2. Approach door'

var77 = input('> ')

if var77 == "1":
print 'You open the window, and hear a moaning sound'
print 'All of a sudden, the fries disappear...' # story will develop more

if var77 == "2":
print'You step closer to the door, on your last step eveything goes quiet'
print'BANG! BANG!'
print'Gunshots?' # story will develop more

if open == "2":
print 'You keep your eyes closed...'
print 'CLSHSHSSH, you hear the shattering of glass!'
print '2. Keep them shut'

insane = input('> ')

if insane == "1":
print 'You open your eyes and see a door with a small smashed window, brown dirty hands wave around where glass was once placed...'
print'1. Run back to the bed'
print'2. Find a weapon'

if insane == "2":
print 'You keep them shut and you hear more banging and crashing, footsteps and growling approach you. As you keep your eyes closed you feel dozens of hands go across your body! You start to feel an unbelievable sensation of burning, and pain shoots through your body...you know you are dying, and you keep your eyes shut out of fear. After a long minute of nothing you come to the conclusion that you are dead...'

yellow = input('> ')

if yellow == "1":
print'You run back to the bed and hide under the covers, all you can'
print'hear are hands waving and'
print'smacking each other, struggling to get inside.'

if yellow == "2":
print'You fly around the room searching everywhere for a weapon.'
print'You see a scalpel laying by a desk and a broom in the corner of the room.'
print'\nWhat do you want to take as a weapon?'
print'1. Scalpel'
print'2. Broom'

orn = input('> ')

if orn == "1":
print'You grab the scalpel, its kind of small, but the sharp edge will'
print'cut through skin if needed'

if orn == "2":
print'You grab the broom, decent weight, good for bashing.'
print'It could be broken in half if you need something with a point.'

if orn == "1" or "2":
print'You look around the room and see a door, and upon further inspection'
print'a window in the corner of the room'
print'1. Go towards the door'
print'2. Go towards the window'

blue = input('> ')

if blue == "1":
print'You step closer to the door, on your last step eveything goes quiet'
print'BANG! BANG!'
print'Gunshots?' # story will develop more


The biggest thing you can do to improve the code is to find ways to make the relationship between the game flow, the data, and the code more concise.

Right now the whole game is a series of if statements, and you're using variable names as the equivalent of old-fashioned GOTOs to jump around the code. For something this short that's bearable, but it won't scale to any larger size. Moreover, your structure is going to force the user to make one-time-only decisions at each point - you don't have a way for users to, say, go to the window and THEN look at the wristband. Or to go back, for that matter. That might be your design intention but if you change your mind the current structure will be a lot of work to fix. Last but not least, you don't have any error handling - if the user enters a wrong choice, the program will just move on to the next prompt without assistance.

On a very general level you need to establish a structure that reflects the nature of the game. Most text adventures are really a series of linked containers; the container (a 'room' in the old school dungeon game, a 'scene' in a conversation based game, etc) contains a descriptive text and a set of options for the user which link to other containers, and so on. The contents differ but the structures are basically identical; it's a good idea to write the code in a way that reflects the way the game actually works.

If you're familiar with object oriented programming, this is a perfect problem to tackle with classes. If not, you can still make your life easier by creating a function that handles the repetitive part and putting the data into clearly labeled variables.

Here's a very rudimentary example that uses dictionaries to represent each room and a dictionary of rooms to represent the map. The 'code' is just to keep calling the same function on whatever rooms the user gets from their choice of actions.

import sys

PROMPT = "> "

# rooms are just dictionaries, with a description and a list of named actions, which point at other rooms
# the 'exit' value indicates a room which ends the game
# the 'name' is used to give each room a unique  in the map
entrance_room = {
'name':'entrance',
'desc': 'You are in a dimly lit cave. To your left there is a winding staircase heading up into the darkness, to your right, a rusty iron gate',
'actions': {'go up': 'tower', 'break the gate': 'main_stairs'},
'exit':False
}

'name':'tower'  ,
'desc' : 'You emerge from the stairway in the ruins of an old tower. There is nothing here but broken stones and matted vines',
'actions': {'go down': 'entrance'},
'exit':False
}

main_stairs = {
'name':'main_stairs',
'desc':'The gate gives way after a few hard kicks, and clangs loundly down a flight of steps into the darkness. You hear the sound of water from down below.',
'actions': { 'go down':'pitfall', 'light torch':'stairs_torch'},
'exit':False
}

stairs_torch= {
'name':'stairs_torch',
'desc':'your torch sputters to life and reveals a large gap in the stairs about ten feet ahead of you. It might be possible to edge by it to the left side...',
'actions': { 'jump the gap':'pitfall', 'go_up':'entrance', 'edge around':'sneak_by'},
'exit':False
}

sneak_by = {
'name':'sneak_by',
'desc':'You carefully step past the gap. A few feet later you are handed a check for \$85,000,000 and the keys to a Tesla. Congratulations!',
'actions':{},
'exit':True}

pitfall = {
'name':'pitfall',
'desc':'You fall into a large gap in the stairs that you would have seen if you had only lit a torch. You plummet to a mercifully quick death in the icy water far below.',
'actions':{},
'exit':True
}

# this is shorthand way of making all of the above dictionaries into a dictionary keyed by the 'name' entries
# in a real game you'd want to make sure these all actually existed and that there were no typos!
map = dict ( (item['name'],item) for item in (entrance_room, tower_dead_end, main_stairs, stairs_torch, sneak_by, pitfall))

def game_loop(room, map):
# room is a dictionary describing an individual room or action
# map is a dictionary of rooms, keyed by name

sys.stdout.writelines(room['desc'])

if room['exit']:
sys.stdout.writelines('\nThanks for playing!')
return

next = None

while not next:
sys.stdout.writelines ("\nYou can...")
for item in room['actions'].keys():
sys.stdout.writelines("\n\t" + item)

sys.stdout.writelines("\n>")