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:
import random
# Monster class
class Monster(object):
"""Monster"""
def __init__(self,name,level):
self.name = name
self.level = level
# Monsters
Green_Blob = Monster("Green Blob","1")
Red_Blob = Monster("Red Blob","2")
Blue_Blob = Monster("Blue Blob","3")
Yellow_Blob = Monster("Yellow Blob","4")
Black_Blob = Monster("Black Blob","5")
def make_monster():
# 2 = monster in room; else, no monster
monster_in = random.randrange(3)
if monster_in == (0 or 1):
return 0
monster = random.randrange(5)
if monster == 0:
monster = Green_Blob
elif monster == 1:
monster = Red_Blob
elif monster == 2:
monster = Blue_Blob
elif monster == 3:
monster = Yellow_Blob
elif monster == 4:
monster = Black_Blob
return monster
# Room class
class Room(object):
""" Room """
def __init__(self,name,description,monster_y_n):
self.name = name
self.description = description
self.monster_y_n = monster_y_n
if self.monster_y_n == "y":
monster = make_monster()
self.monster = monster
# Rooms
# last input is coordinates (n,e,s,w)
#
# ++++ Room Map +++++
#
# ===================
# | |
# | |
# | 1 |
# | |
# | 3 2 |
# | |
# | |
# | |
# | |
# ===================
#
# Template:
# room# = Room(name,description,monster in it? (y/n))
# Then add the coordinates below the room creators
room1 = Room("Bedroom","You are you in your own bedroom.\nTo the south, there is a garden past the back door.", "n")
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")
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")
# Room coordinates (had to create all the rooms to assign them to coordinates)
room1.coordinates = [0,0,room2,0]
room2.coordinates = [room1,0,0,room3]
room3.coordinates = [0,room2,0,0]
#room4 = Room("Classroom","You are in a classroom with a 5 rows of desks that face a whiteboard.")
# Character class
class Character(object):
def __init__(self,name,gender,hair,age,location = room1):
self.name = name
self.gender = gender
self.hair = hair
self.age = age
self.location = location
self.inv = []
def look(self):
place = self.location
print place.description
if place.monster_y_n == "y":
if place.monster != 0:
room_monster = place.monster
print "There is a",room_monster.name,"in the room.\n"
else:
print ""
#if item == True:
#print item.description
#class item(object):
#def __init__(self,description):
#self.description = description
characters = []
Zyrkan = Character("Zyrkan","m","black",20)
characters.append(Zyrkan)
currentchar = Zyrkan
print "Welcome to Mouche's first text based game."
print
print 'Type "commands" to see the command list'
print "You are currently:",currentchar.name
# Menu
# "commands" shows the commands available
# "look" looks around in the current room
#
while True:
command = raw_input("")
if command == "commands":
print '"n","e","s", and "w" make your character go north, east, south, and west respectively'
print '"end" to break'
print '"look" to look around the room'
print '"players" to see the player list'
if command == "look":
currentchar.look()
if command == ("n" or "north"):
if currentchar.location.coordinates[0] == 0:
print "You cannot go that way."
else:
currentchar.location = currentchar.location.coordinates[0]
currentchar.look()
if command == ("e" or "east"):
if currentchar.location.coordinates[1] == 0:
print "You cannot go that way."
else:
currentchar.location = currentchar.location.coordinates[1]
currentchar.look()
if command == ("s" or "south"):
if currentchar.location.coordinates[2] == 0:
print "You cannot go that way."
else:
currentchar.location = currentchar.location.coordinates[2]
currentchar.look()
if command == ("w" or "west"):
if currentchar.location.coordinates[3] == 0:
print "You cannot go that way."
else:
currentchar.location = currentchar.location.coordinates[3]
currentchar.look()
if command == "end":
break
if command == "players":
print "You are",currentchar
print
i = 1
print "Players:"
for player in characters:
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)