Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Adventure Game ->object

by bigj (Monk)
on May 14, 2003 at 06:49 UTC ( [id://257982]=note: print w/replies, xml ) Need Help??


in reply to Adventure Game ->object

In object oriented design, you first want to find out what are the given (natural) objects and the "doings" that have to be done. I normally start with a simple description of the used objects and actions done that shall be implemented to the application.
First of all, you have rooms in your advanture game. Every room has a name and there are several items in there. Then there is a map. The map can be loaded and initializes all room objects. It also can tell (in the initialization) to the rooms what are their neighbours.
Then there is of course a player. The player has an inventory of items, he/she knows where he/she is (his/her current room), he/she has a point score. He/She can do one of the following actions: move(to north/south/west/east), take an item, look to an item (both perhaps only described by its name).
The whole is united as game, consisting of map, one or several players and the possibility to initialize a game, to start and end it.
After this description it should be easy to find out an object oriented design. Thus we have the following objects and methods (might be not full complete, but it's you who have the overview of your game, not me :-))
Item + new($name, $points) + getName, getPoints Room + new($name, $description, $room_north, $room_south, $room_west, $room +_east, @item) + addItem($item), removeItem($name) + getName, getDescription, ... Map + load($filename) + initialize_rooms($filename) + getRoom($name), getRoom($row, $col) Player + new($name, $current_room, $points) + move($direction) + take($item_name) + drop($item_name) # perhaps a good addition + throw_away($item_name) # perhaps also a good addition + look($item_name) + getName, getCurrentRoom, getPoints, getInventory : returns @item Game + new($map, %player_name_and_current_rooms) + quit + getMap, getPlayer : return @player
Having all these objects, now it is easy to write someting like a main server. In pseudo code it's quite that simple:
my %player_names_and_start_room_names = ( bob => 'dark_hall', jay => 'forest', ); $map = Map->load($map_filename); $map->initialize_rooms($room_filename); $game->new($map, %player_names...);
While a client (one player) would now only do the graphics (pseudocode):
while (1) { print "You are in room", $player->getCurrentRoom->getName; print "There are several items in this room:", map {$_->getName}, $player->getCurrentRoom->getItems; print "In the north, there is ", $player->getCurrentRoom->getNorthRoom->getName; ... print "Choose: move (N),(S),(W),(E); (T)ake, (L)ook, (I)nventory, ( +Q)uit"; my $action = read_action; if ($action =~ /[NSWE]/) { $player->move($action) or print "Couldn't move to $action"; } elsif ($action =~ /[TL]/) { my $item = read_item; if ($action eq 'T') { $player->take($item) or print "Couldn't take $item"; } else { print $player->getCurrentRoom->getItem($item)->getDescripti +on || "No description available"; } } }
Of course, that's quite only my idea of an object oriented design. But to answer your main question, in my opinion, it's important to seperate the parts that are valid in all adventure games (rules, basic actions, a map) from them that are only valid in your (your current map, nr of players, ...) specific adventure games.

Greetings,
Janek

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://257982]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found