[Tutor] More class questions
Ara - I found your question about the Pyparsing-based adventure game that I wrote. You can find more info on this from the presentation I made at PyCon'06, (http://www.python.org/pycon/2006/papers/4/). This link opens up at the title page, there are navigation controls in the lower right corner of the screen if you move your mouse over them. This program uses the following classes: - Room - Item - Player - Command The Room and Item instances are created during the game setup. Each Room contains pointers to neighboring rooms to the north, south, east, and west. Rooms also have a list of items that are currently in the room. As the game ensues, items can be picked up and dropped, so this list will change over time. I guess you could change the room connections also - wouldn't be hard - perhaps as a result of using a special Item while in the Room. Items are fairly passive, free-standing objects, containing some attributes, and a possible useAction. They don't have much behavior, they don't know what room they are in, they can be picked up, dropped, and used, and they have a name that describes them when you look in a room, or list your player's inventory. Player is the status object of the game. Player has an inventory of Items, and has a reference to the Room the player is currently in. I think an easy mistake when writing a game is to make the Player status and attributes global variables. This will work okay, but by keeping this info in an object, the game could easily extend to having multiple players, just by adding a second instance, and adding support for the players to take turns giving commands. Command is the class that actually makes things happen. Command itself is an abstract class, that defines the basic form of what different commands can do, and how they are created. There are several subclasses of Command: - TakeCommand - DropCommand - InventoryCommand - UseCommand - LookCommand - DoorsCommand - MoveCommand - HelpCommand - QuitCommand Commands are created based on the input that the game player types in at the game prompt (this is where pyparsing comes in). The pyparsing grammar parses the input string, and if it is a valid command, the grammar's parse actions create the appropriate Command subclass. For instance, typing in help will create a HelpCommand instance. Typing in take shovel will create a TakeCommand, with the target object of shovel. After the Command is created, it is executed against the Player object. The results of the Command can: - have the Player take something from the current Room - have the Player drop something in his inventory, and leave it in the current Room - list the Player's inventory - etc. The MoveCommand will move the player to an adjoining room. To tie it all together, the game engine runs in a basic loop: # create a player, let's call him Bob player = Player(Bob) # give Bob the sword for protection player.take( Item.items[sword] ) # read commands, and then invoke them on Bob (and his surroundings) while not player.gameOver: cmdstr = raw_input( ) cmd = parser.parseCmd(cmdstr) if cmd is not None: cmd.command( player ) And that's it. All of the logic about the moving from room to room is captured in the N,S,E,W references between Room objects. Moving Bob from room to room is done by MoveCommands, as they are dynamically created based on user input. I hope that gives you a little more idea of how the pyparsing adventure game works. -- Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More class questions
Ara Kooser [EMAIL PROTECTED] wrote What I have so far is a very simple text adventure with two rooms, two items, and some exits. So presumably you have a Room class, an Item class and maybe an Exit class? Two question which relates directly to classes: Do you create all your instances at the end of the program or just as needed? You create objects as needed. There is no mystery you have been using objects almost since you started using Python. Strings are objects and you create them as you need them. Same with Files. Your own classes are no different except that the instantiation mechanism is a little bit different. call functions from other classes within a class Yes, If your Room class contains a list of Items you can send messages to thoise items from your Room methods.: class Item: def __innit__(self,n): self.n = n def say(self): print 'item', n class Room: def __init__(self,id) self.items = [Item(42),Item(26)] self.id = id def describe(self): print 'Room:',self.id,'has:' for item in items: item.say() r = R('hall') r.describe() Notice that Room.describe calls the Item.say method... Also note that the Room constructor creates two instances of Item. 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. You have a Player class and it has a move method. You create instances of players and ask them to move. If they are in a Room you move within the room - perhaps you move to an exit which will in turn return a new room which you assign to the player. Something like: class Exit: def __init__(self, toRroom) self.room = toRoom def pass(self): return self.room class Player: def __init__(self,location=None): self.location = location def move(self, direction): exit = self.location.exitAt(direction) self.location = exit.pass() Now when you call move('N') the player goes tonthe North exit and passes throough to the room on the other side. 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 doubt it, movement is an action not an object (usually), its a behaviour of other objects. If more than players can move you may create a Movableobject superclass and inherit the movement methods from that. 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. Sorry, cant help there. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] More class questions
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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor