Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDinko Galetic <dgaletic@everflame.(none)>2010-06-09 19:45:34 (GMT)
committer Dinko Galetic <dgaletic@everflame.(none)>2010-06-09 19:45:34 (GMT)
commita77440ff9043748385368dc474b9af0ba3d6500f (patch)
tree5befd101e2b4847b5778c2a752aacd5bcc7ec326
parentcfdcfc33f5bb9e3d9313295f9451b728fdb36ff7 (diff)
Several changes to scholar.py
1) Changed the way names of objects are handled. GameObject.name is now a property which returns/sets GameObject.aliases[0] A few minor adjustments had to be made so this works with everything. 2) Room.contains[] now contains not only items, but player(s) and exits as well. This enabled me to use a single for loop in unalias() 3) added some TODO and other comments
-rw-r--r--library/pippy/scholar/scholar.py48
1 files changed, 34 insertions, 14 deletions
diff --git a/library/pippy/scholar/scholar.py b/library/pippy/scholar/scholar.py
index 87d246a..13d6245 100644
--- a/library/pippy/scholar/scholar.py
+++ b/library/pippy/scholar/scholar.py
@@ -7,19 +7,32 @@ class GameObject(object):
self.description = "Enter this for every object"
self.methods = ["examine", "look"]
self.aliases = []
- #self.ID = get_ID.next()
+ @property
+ def name(self):
+ return self.aliases[0]
+ @name.setter
+ def name(self, new_name):
+ if self.aliases:
+ self.aliases[0] = new_name
+ else:
+ self.aliases = [new_name]
+# TODO: possibly make an Exit class, use it instead of current dictionaries
class Direction(GameObject):
def __init__(self, name):
GameObject.__init__(self)
self.methods.append("go")
self.name = name
+# TODO: Player and NPCs should probably be instances of the same class.
+# But let's get there first.
class Player(GameObject):
def __init__(self, name):
GameObject.__init__(self)
self.name = name
- self.position = "A room object goes here!"
+ self.aliases.append("self")
+ self.position = None
+ #self.position = "A room object goes here!"
self.known_commands = ["examine", "go"]
class Room(GameObject):
@@ -28,7 +41,7 @@ class Room(GameObject):
self.contains = []
self.exits = {}
self.methods.append("go")
- self.aliases = ["room"]
+ self.aliases.append("room")
# TODO: Think about the case when two rooms share one object
# (such as a door)
@@ -38,10 +51,14 @@ class Room(GameObject):
if self.exits.has_key(through) == False:
if two_way:
if to.exits.has_key(opposite_exit(through)) == False:
+ # Hack, use setter.
to.exits[opposite_exit(through)] = self
+ to.contains.append(opposite_exit(through))
+
else:
return False
self.exits[through] = to
+ self.contains.append(through)
return True
else:
return False
@@ -108,7 +125,13 @@ def examine(player, obj):
# TODO: The option to move without typing "go", just "west", "up"...
def go(player, side):
if player.position.exits.has_key(side):
+ # (this should be handled by a Player.position property)
+ # 1) tell the old room that the player moved
+ # 2) move the player
+ # 3) inform the new room of the arrival
+ player.position.contains.remove(player)
player.position = player.position.exits[side]
+ player.position.contains.append(player)
examine(player, player.position)
else:
print "No such exit."
@@ -119,14 +142,10 @@ def unalias(player, player_input):
if player_input == "here" or player_input == "room":
return player.position
- for exit in player.position.exits:
- if exit.name == player_input:
- return exit
-
- for item in player.position.contains:
- if player_input in item.aliases:
- return item
- return None
+ for item in player.position.contains:
+ if player_input in item.aliases:
+ return item
+ return None
def game_loop():
@@ -139,6 +158,8 @@ def game_loop():
load_game()
player.position = rooms[0]
+ # temporary hack. Should be handled another way (property?)
+ player.position.contains.append(player)
examine(player, player.position)
while True:
@@ -192,8 +213,7 @@ Down = Direction("down")
# in create_some_rooms() (called from game_loop()).
starting_painting = GameObject()
starting_painting.description = "This is a painting of a young couple lying on the grass and hugging. Their eyes are of an extraordinary blue colour. The artist has really managed to capture the look of love on their faces. Oddly, the painting seems to be hanging upside down. On a more careful look, however, you realise that it was painted like this."
-starting_painting.aliases = ["painting"]
+starting_painting.aliases.append("painting")
starting_bed = GameObject()
starting_bed.description = "A cozy bed. The maids make it and put fresh sheets each morning."
-starting_bed.aliases = ["bed"]
-
+starting_bed.aliases.append("bed")