patternpythonMinor
Adventure game in a forest
Viewed 0 times
forestgameadventure
Problem
I've started writing a game and I need help, because I know there are many ways to improve what I am doing. I am doing it the basic way I know I can but I know there is more complex ways of doing it.
```
import time
print"Created on 3/12/2013"
print"ver 1.2"
print"Created by: Daniel Vargas"
print'At any point type "help" for commands'
print"===== Hello ====="
print"Welcome to ZOTD3000!"
time.sleep(2)
print("To find your fate")
### This is the first Fate ###
while True:
fate = raw_input('Write A, B, or C:\n')
if fate == 'A':
print"You have selected choice A"
time.sleep(2)
break
elif fate == 'help':
print"You can only type A, B, or C."
else:
print"You can only type A, B, or C."
time.sleep(2)
### Here is the first decision ###
### From Fate A ###
def Forest(Health,Hunger,Inventory):
print'You wake up in the middle of the forest'
Inventory = 'Inventory: '
Squirrel = 'Squirrel'
while True:
Choice1 = raw_input('You...\n')
if Choice1 in ('Life', 'life'):
print('Health: '+str(Health))
print('Hunger: '+str(Hunger))
elif Choice1 in ('Look', 'look'):
print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.'
elif Choice1 in ('Pickup', 'pickup'):
p1 = raw_input('Pickup what?\n')
if p1 == Squirrel:
if Inventory == 'Inventory: ':
print'You picked up a Squirrel!'
Inventory = Inventory + Squirrel + ', '
elif Inventory == 'Inventory: Squirrel, ':
print'You already picked that up!'
else:
print"You can't find a "+str(p1)+"."
elif Choice1 in ('Inventory', 'inventory'):
print Inventory
elif Choice1 in ('Eat', 'eat'):
print Inventory
```
import time
print"Created on 3/12/2013"
print"ver 1.2"
print"Created by: Daniel Vargas"
print'At any point type "help" for commands'
print"===== Hello ====="
print"Welcome to ZOTD3000!"
time.sleep(2)
print("To find your fate")
### This is the first Fate ###
while True:
fate = raw_input('Write A, B, or C:\n')
if fate == 'A':
print"You have selected choice A"
time.sleep(2)
break
elif fate == 'help':
print"You can only type A, B, or C."
else:
print"You can only type A, B, or C."
time.sleep(2)
### Here is the first decision ###
### From Fate A ###
def Forest(Health,Hunger,Inventory):
print'You wake up in the middle of the forest'
Inventory = 'Inventory: '
Squirrel = 'Squirrel'
while True:
Choice1 = raw_input('You...\n')
if Choice1 in ('Life', 'life'):
print('Health: '+str(Health))
print('Hunger: '+str(Hunger))
elif Choice1 in ('Look', 'look'):
print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.'
elif Choice1 in ('Pickup', 'pickup'):
p1 = raw_input('Pickup what?\n')
if p1 == Squirrel:
if Inventory == 'Inventory: ':
print'You picked up a Squirrel!'
Inventory = Inventory + Squirrel + ', '
elif Inventory == 'Inventory: Squirrel, ':
print'You already picked that up!'
else:
print"You can't find a "+str(p1)+"."
elif Choice1 in ('Inventory', 'inventory'):
print Inventory
elif Choice1 in ('Eat', 'eat'):
print Inventory
Solution
The existing code is not easy to maintain. If you want to add new places or new actions you will have to write a lot of very specific and deeply nested code.
I've never written a game like this, but it's a good case for modelling with classes. You have a
To illustrate one aspect of this, I would consider something like:
Which will allow you to set up your world separately like:
Your character could look something like (as a starting point)
There are ways to make the direction choices robust, for example making sure that a's southern neighbour is b's northern neighbour and so on, but hopefully this gives you a general sense of what I am talking about. Eventually you will get a feel for what the entities in your code are and what they do and how they interact.
I've never written a game like this, but it's a good case for modelling with classes. You have a
Place, which has a set of neighbours which are also Places. You have a Player who has an inventory which contains Items. The Player has a level of health and hunger which can be modified by Items in his inventory. An Item can be a medicine, a source of food, a physical object, etc.To illustrate one aspect of this, I would consider something like:
class Place(object):
def __init__(self, name):
self.name = name
self.neighbours = {"North": None,
"South" : None
"East" : None
"West" : None}
def add_neighbour(self, direction, neighbour):
self.neighbours[direction] = neighbour
def leave(self):
# this is maybe one way to handle movement
# if the player stores a reference to their location
# but by no means the only way or the best way
# get direction from user
return self.neighbours[direction]Which will allow you to set up your world separately like:
Waterfall = Place("Waterfall")
Forest = Place("Forest")
Waterfall.add_neighbour("South", Forest)Your character could look something like (as a starting point)
class Character(object):
def __init__(self, name, initial_location=None):
self.name = name
self.inventory = []
self.location = initial_location
self.health = 100
self.hunger = 0There are ways to make the direction choices robust, for example making sure that a's southern neighbour is b's northern neighbour and so on, but hopefully this gives you a general sense of what I am talking about. Eventually you will get a feel for what the entities in your code are and what they do and how they interact.
Code Snippets
class Place(object):
def __init__(self, name):
self.name = name
self.neighbours = {"North": None,
"South" : None
"East" : None
"West" : None}
def add_neighbour(self, direction, neighbour):
self.neighbours[direction] = neighbour
def leave(self):
# this is maybe one way to handle movement
# if the player stores a reference to their location
# but by no means the only way or the best way
# get direction from user
return self.neighbours[direction]Waterfall = Place("Waterfall")
Forest = Place("Forest")
Waterfall.add_neighbour("South", Forest)class Character(object):
def __init__(self, name, initial_location=None):
self.name = name
self.inventory = []
self.location = initial_location
self.health = 100
self.hunger = 0Context
StackExchange Code Review Q#23808, answer score: 4
Revisions (0)
No revisions yet.