Text Adventure -- Classes

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Text Adventure -- Classes

 
0
  #1
Oct 10th, 2006
I'm working on a text adventure game just for fun. I am not planning to complete it, but I decided to try it just to learn classes. Seeing the text game in another post using functions, I realize that that is a very nice way to make a simple game, but I went about this for educational purposes, not for ease.

I started out creating a character class and gave it some attributes just for fun. Then I created a room class and made some coordinates for the rooms (and a commented map). Finally I made a monster class and a menu system for the user.

I've learned a bit about classes from "Python Programmin for the Absolute Beginner," and have had to try to apply those concepts to this evolving game.

Honestly, I don't have a direct problem or anything except "where do I go next?"

I'd love to figure out a monster fighting system. I think a unique monster is put in a room, but I'm not sure how that works. If a monster is picked from a list and has a 1/3 chance of being placed in the room, is it really unique?


Current Goal:
* 4 rooms: bedroom, garden, pathway, kitchen
- Bedroom: has a bed that you can sleep in to regain health; one open exit to the garden; one locked exit to the kitchen (unlocked if you have key; uses key so it's no longer in your inventory)
- Garden: has one first level monster that can be easily beat; two exits.. one to bedroom one to pathway; a weapon is dropped by the monster (respawned after going to another map; 50% chance you can flee)
- Pathway: second level monster; drops key to get into kitchen
- Kitchen: For a later date...to figure out how to create a NPC (non-playing character); have an item in the kitchen which only appears after you talk to someone in a forest (yes, new room) who wants the item; take the item and bring it back to the person and they reward you with something...which then unlocks a new room...etc.
* Simple fighting system; (atk points for char and monsters...weapons = +atk ??)


Here's my current code:

  1. import random
  2.  
  3. # Monster class
  4. class Monster(object):
  5. """Monster"""
  6. def __init__(self,name,level):
  7. self.name = name
  8. self.level = level
  9.  
  10. # Monsters
  11. Green_Blob = Monster("Green Blob","1")
  12. Red_Blob = Monster("Red Blob","2")
  13. Blue_Blob = Monster("Blue Blob","3")
  14. Yellow_Blob = Monster("Yellow Blob","4")
  15. Black_Blob = Monster("Black Blob","5")
  16.  
  17. def make_monster():
  18. # 2 = monster in room; else, no monster
  19. monster_in = random.randrange(3)
  20. if monster_in == (0 or 1):
  21. return 0
  22. monster = random.randrange(5)
  23. if monster == 0:
  24. monster = Green_Blob
  25. elif monster == 1:
  26. monster = Red_Blob
  27. elif monster == 2:
  28. monster = Blue_Blob
  29. elif monster == 3:
  30. monster = Yellow_Blob
  31. elif monster == 4:
  32. monster = Black_Blob
  33. return monster
  34.  
  35. # Room class
  36. class Room(object):
  37. """ Room """
  38. def __init__(self,name,description,monster_y_n):
  39. self.name = name
  40. self.description = description
  41. self.monster_y_n = monster_y_n
  42. if self.monster_y_n == "y":
  43. monster = make_monster()
  44. self.monster = monster
  45.  
  46. # Rooms
  47. # last input is coordinates (n,e,s,w)
  48. #
  49. # ++++ Room Map +++++
  50. #
  51. # ===================
  52. # | |
  53. # | |
  54. # | 1 |
  55. # | |
  56. # | 3 2 |
  57. # | |
  58. # | |
  59. # | |
  60. # | |
  61. # ===================
  62. #
  63. # Template:
  64. # room# = Room(name,description,monster in it? (y/n))
  65. # Then add the coordinates below the room creators
  66.  
  67. room1 = Room("Bedroom","You are you in your own bedroom.\nTo the south, there is a garden past the back door.", "n")
  68. room2 = Room("Garden","You are in a garden with many flowers and a narrow stone path. \nTo the north, you see the backdoor of your house that enters your bedroom.\nA pathway leads west.","y")
  69. room3 = Room("Pathway","You are in a narrow stone path with hedges on both sides of you.\nTo the east, there is a garden.","y")
  70.  
  71. # Room coordinates (had to create all the rooms to assign them to coordinates)
  72. room1.coordinates = [0,0,room2,0]
  73. room2.coordinates = [room1,0,0,room3]
  74. room3.coordinates = [0,room2,0,0]
  75. #room4 = Room("Classroom","You are in a classroom with a 5 rows of desks that face a whiteboard.")
  76.  
  77. # Character class
  78. class Character(object):
  79. def __init__(self,name,gender,hair,age,location = room1):
  80. self.name = name
  81. self.gender = gender
  82. self.hair = hair
  83. self.age = age
  84. self.location = location
  85. self.inv = []
  86.  
  87. def look(self):
  88. place = self.location
  89. print place.description
  90. if place.monster_y_n == "y":
  91. if place.monster != 0:
  92. room_monster = place.monster
  93. print "There is a",room_monster.name,"in the room.\n"
  94. else:
  95. print ""
  96. #if item == True:
  97.  
  98. #print item.description
  99.  
  100. #class item(object):
  101. #def __init__(self,description):
  102. #self.description = description
  103.  
  104. characters = []
  105. Zyrkan = Character("Zyrkan","m","black",20)
  106. characters.append(Zyrkan)
  107. currentchar = Zyrkan
  108. print "Welcome to Mouche's first text based game."
  109. print
  110. print 'Type "commands" to see the command list'
  111. print "You are currently:",currentchar.name
  112.  
  113. # Menu
  114. # "commands" shows the commands available
  115. # "look" looks around in the current room
  116. #
  117. while True:
  118. command = raw_input("")
  119. if command == "commands":
  120. print '"n","e","s", and "w" make your character go north, east, south, and west respectively'
  121. print '"end" to break'
  122. print '"look" to look around the room'
  123. print '"players" to see the player list'
  124. if command == "look":
  125. currentchar.look()
  126. if command == ("n" or "north"):
  127. if currentchar.location.coordinates[0] == 0:
  128. print "You cannot go that way."
  129. else:
  130. currentchar.location = currentchar.location.coordinates[0]
  131. currentchar.look()
  132. if command == ("e" or "east"):
  133. if currentchar.location.coordinates[1] == 0:
  134. print "You cannot go that way."
  135. else:
  136. currentchar.location = currentchar.location.coordinates[1]
  137. currentchar.look()
  138. if command == ("s" or "south"):
  139. if currentchar.location.coordinates[2] == 0:
  140. print "You cannot go that way."
  141. else:
  142. currentchar.location = currentchar.location.coordinates[2]
  143. currentchar.look()
  144. if command == ("w" or "west"):
  145. if currentchar.location.coordinates[3] == 0:
  146. print "You cannot go that way."
  147. else:
  148. currentchar.location = currentchar.location.coordinates[3]
  149. currentchar.look()
  150. if command == "end":
  151. break
  152. if command == "players":
  153. print "You are",currentchar
  154. print
  155. i = 1
  156. print "Players:"
  157. for player in characters:
  158. print player.name,"(" + str(i) + ")"
Don't take this post as a request to complete my program for me. I have little resources (I know more than my programming teacher because I'm ahead of the class and he's learning a couple days ahead of the students (first year teaching this class))...so he's just having me do whatever I want just to learn... It'd be great to receive both some conceptual help and some coding help. I've read a bit about class inheritance and I think that might be handy for rooms, but I'm not sure how I would implement that.

Thank you very much. (glad there are resources like this on the web)
Last edited by LaMouche; Oct 10th, 2006 at 4:11 am. Reason: Add information
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Text Adventure -- Classes

 
0
  #2
Oct 10th, 2006
I have to applaud your teacher for teaching Python rather than the usual "ugly syntax stuff" like C, C++ or Java. Colleges with their all too often ultra conservative teachers are just warming up to Python. So you and your teacher are way ahead!

I will be playing around with your code to get a feel, then let you know what I think.

Here is a quote from Donald Knuth who has written many books on computer science:
Donald E. Knuth, December 1974 -->
"We will perhaps eventually be writing only small
modules which are identified by name as they are
used to build larger ones, so that devices like
indentation, rather than delimiters, might become
feasible for expressing local structure in the
source language."
Last edited by Ene Uran; Oct 10th, 2006 at 3:31 pm. Reason: conservatism
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Text Adventure -- Classes

 
0
  #3
Oct 10th, 2006
Originally Posted by Ene Uran View Post
I have to applaud your teacher for teaching Python rather than the usual "ugly syntax stuff" like C, C++ or Java. Colleges with their all too often ultra conservative teachers are just warming up to Python. So you and your teacher are way ahead!
I have barely touched C++, but I understand how complex it is and its alien syntax. Python is so much easier to read and write. As a student who understands much of the programming concepts, I have realized that just because the syntax is easier doesn't mean it's not powerful and useful. Someone at school was arguing that C is more powerful. I was thinking... "More powerful for what? What do you need to do that wields such power? I'm just tinkering around with learning classes and such."

I think all programmers should start off with a higher level language such as Python to get the feel for programming concepts: variables, conditionals, loops, functions, eventually classes. Then, if they want more power, they write modules in C or however that works...or, if need be, completely turn over to C/C++.

Meh, I don't know a lot about programming, but I'm starting to understand what you can do with it, and that's exciting.


Thanks for taking the time to look at the code and help me.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,207
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 538
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Text Adventure -- Classes

 
0
  #4
Oct 10th, 2006
i did this once in C++

i just made race classes e.g human/drwarf/elf each with different properties. Worked well, less than 8 pages of code can make a good engine

It will save you alot fo time if ou learn .txt file I/O and get the engine to read the text from thier for the mission e.g standard encounter but the x's (from text file) change e.g

you find yourself in x - it is x and you see an x
Last edited by jbennet; Oct 10th, 2006 at 6:38 pm.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Text Adventure -- Classes

 
0
  #5
Oct 10th, 2006
For the person that thinks C is so much more powerful than Python, give this small problem to solve in C. The Python code is here ...
  1. # split a text string into its words and sort the words ...
  2.  
  3. text = "explain to me why C is so much more powerful than Python"
  4.  
  5. word_list = text.split()
  6.  
  7. # sort case insensitive (using lower case)
  8. word_list.sort(key=str.lower)
  9.  
  10. # show the result ...
  11. for word in word_list:
  12. print word
Another coding problem would be to print out the monthly calendar for October 2006.
  1. October 2006
  2. Mo Tu We Th Fr Sa Su
  3. 1
  4. 2 3 4 5 6 7 8
  5. 9 10 11 12 13 14 15
  6. 16 17 18 19 20 21 22
  7. 23 24 25 26 27 28 29
  8. 30 31
Here is the Python code that will do it ...
  1. import calendar
  2.  
  3. calendar.prmonth(2006, 10)
Would be fun, and shut up a big mouth!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Text Adventure -- Classes

 
0
  #6
Oct 10th, 2006
Seeing that you seem to be one of the head Python programmers at these forums,vegaseat, could you check out my intital post please?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Text Adventure -- Classes

 
0
  #7
Oct 10th, 2006
Your class constructs are fine.
  1. command = raw_input("Enter command: ").lower() # added prompt text and lower()
You need to do a little more hand holding for the user. At the start of the game you are simply stuck. Give me some info here in the prompt. Also, you may want to start with a "look".
This type of code has to be changed ...
  1. #if command == ("e" or "east"):
  2. if command == "e" or command == "east":
I realize this is just the start, once I leave my bedroom I can go south then west and that's the end.
Last edited by vegaseat; Oct 10th, 2006 at 9:36 pm. Reason: spelling
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Text Adventure -- Classes

 
0
  #8
Oct 10th, 2006
Thanks...

I guess what I need some suggestions on is the monster attacking system. How can I most effectively make it to where a monster heals a little for each room that you move that you're not in it and fight it when you do see it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Text Adventure -- Classes

 
0
  #9
Oct 11th, 2006
Each time you move s, n, e, or west heal the monster(s) a little. That would be in your while loop's if command == 's' etc.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Text Adventure -- Classes

 
0
  #10
Oct 12th, 2006
Originally Posted by Ene Uran View Post
Each time you move s, n, e, or west heal the monster(s) a little. That would be in your while loop's if command == 's' etc.
Right. I would probably have a Maze class, Room class, and a Monster class, and then maintain a dictionary like this:

[php]

class Maze(object):

def __init__(self):

...
self.monsters = {room1:None, room2:None, ...}

class Room(object):

def __init__(self, maze):
...
self.maze = maze
...

def enter(self):
if self.maze.monsters[self] == None:
self.create_monster()

for m in monsters:
if monsters[m]:
monsters[m].regenerate()

def create_monster(self):
monster_type = random.choice(MONSTER_NAMES)
monster_level = random.randrange(1,MAX_MONSTER_LEVEL)
self.maze.monsters[self] = Monster(monster_type, monster_level)

class Monster(object):

...

def regenerate(self):

self.hp += 0.1 *self.max_hp
if self.hp > self.max_hp:
self.hp = self.max_hp

# or replace the three lines above with the sexy way:
# self.hp = (self.hp + 0.1 * self.max_hp) % self.max_hp
[/php]
BTW, give your teacher my regards: I'm teaching Computer Programming for the first time this year, using "Python Programming for the Absolute Beginner." Good book choice!

Jeff

P.S. Some free advice ... create the __str__ function for each of your classes early on, so that you can print your objects easily for debugging!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC