[Tutor] More class questions
Ara Kooser
ghashsnaga at gmail.com
Mon Sep 3 19:47:09 CEST 2007
Hello again,
Thank you again for your help. I have classes somewhat figured out
and I am beginning to understand the syntax involved in using them.
What I have so far is a very simple text adventure with two rooms, two
items, and some exits.
Two question which relates directly to classes: Do you create all
your instances at the end of the program or just as needed? Can you
call functions from other classes within a class (So I create an
Object class for items and I want then instances to appear in the Room
class as they are called).
Third question. Using function I understand how to move a player
around in different rooms using raw_input and if statements but how do
you do that with classes. I created a player class and I want one of
the methods to be moving from room to room. Or should movement be a
separate class?
I am looking at the code that Paul McGuire wrote for using
pyparsing and I don't quite understand how it all works. I have to
book Game Programming: The L line coming in two weeks. I am just
trying to get a head start.
Thank you.
Ara
CODE BELOW
#####################################################################
#Text Advenuture - Roguelike
#By Ara Kooser
#Thanks to Chris, e.,Tino and Steven at python tutor
####################################################################
class Player:
#What makes this a player.
pass
#def Move(self):
class Area:
#What makes it an area?
def __init__(self, name, description):
#Number of arguements in the _init_ there must be defined
self.name = name
self.description = description
self.contents = []
self.paths = [None,None,None,None]
#Methods. What you can do.
def AddObject(self,thing):
self.contents.append(thing)
def AddPaths(self,direction):
self.paths.append(direction)
def look(self):
print "Look around the place you are in"
print "You are in the",self.name
print self.description
print "Your exits are:"
print self.paths
def search(self):
print "You search the area and find..."
print self.contents
###############################################################################
# MAIN
# Start of program
###############################################################################
first_instance = Area("Outside", "You are standing outside")
first_instance.AddObject("Stick")
first_instance.AddPaths("North")
first_instance.look()
first_instance.search()
print
print
second_instance = Area("Inside", "You are standing inside")
second_instance.AddObject("Iron pot")
second_instance.AddPaths("South")
second_instance.look()
second_instance.search()
--
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.
More information about the Tutor
mailing list