I'm a newbie to game design and coding in general, and I've started working on a text-based adventure game project (like Zork) to work on my narrative writing skills while teaching myself Python. Any tips/pointing me in the right direction would be greatly appreciated.
Currently, I've established an inventory system, a room class, a player class, and an items class with a subclass for weapons.
The current thing that I'm stuck on is getting my player character to be able to move through the rooms in a consistent manner, and whether or not the code is actually reading the player character as being "in" certain rooms based on their position.
inventory = []
currentroom = 0
def addToInventory(item):
inventory.append(item)
class player:
def __init__(self, name, health, bag, position):
self.name = name
self.health = health
self.bag = bag
self.position = position
player.position = currentroom
player.bag = inventory
class Items:
def __init__(self, name, info, weight, position):
self.name = name
self.position = position
self.info = info
self.weight = weight
class Weapon(Items):
def __init__(self, name, info, damage, speed, weight, position):
super().__init__(name, info, weight, position)
self.damage = damage
self.speed = speed
class Room:
def __init__(self, description, exits, actions, roominv, position): # Runs every time a new room is created
self.description = description
self.exits = exits
self.actions = actions
self.roominv = roominv
self.position = position
sword = Weapon("Sword", "A sharp looking sword. Good for fighting goblins!", 7, 5, 5)
knife = Weapon("Knife", "A wicked looking knife, seems sharp!", 5, 7, 3)
stick = Weapon("Stick", "You could probably hit someone with this stick if you needed to", 2, 3, 3)
shackkey = Items("Shack Key", "A key! I wonder what it opens.", .01)
cottagekey = Items("Cottage Key", "An ornate key with an engraving of a small cottage on one side", .01)
Moonstone = Items("Moonstone", "A smooth white stone that seems to radiate soft white light", .05)
flower = Items("Flower", "A beautiful wildflower", .001)
introd = Room("You are in a forest, you can hear wildlife all around you. Your sword lies at your feet. There seems to be a clearing in the distance.", 'N' "clearing", {"Search the ground", "Go North"}, [sword], 0)
clearing = Room("You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. To the South is the way you entered the forest. A well worn path goes to the East. In the distance a harp can be heard.", {'S': "introd", 'E': "forest path"}, {"Take flower", "Go south", "Go East"}, [flower], 1)
forest_path = Room("You begin walking down a well beaten path. The sounds of the forest surround you. Ahead you can see a fork in the road branching to the South and East. You can smell smoke coming from the South, and can hear a stream to the East", {'S': "cottage", 'E': "stream"}, {"Go South", "Go East"}, [stick], 2)
stream = Room("You come upon a relaxing stream at the edge of the woods. To your South is a rickety looking shack, to your West is the forest path you came down", {'S': "shack", 'W': "forest path"}, {"Go South", "Go West"}, [shackkey], 3)
shack = Room("In front of you is a shack, possibly used as an outpost for hunting. It looks dilapidated.", {'S': "inside shack", 'N': "stream"}, {"Go South", "Go North"}, None, 4)
insideshack = Room("The inside of the shack is dirty. Bits of ragged fur are scattered about the floor and on a table against the back wall. A sharp looking knife is on the table. There is a key hanging on the wall by a string.", {'N': "shack"}, {"Go North", "Take Knife", "Take Key"}, [knife, cottagekey], 5)
cottage = Room("A quaint cottage sits in the middle of a small clearing, smoke drifting lazily from the chimney.", {'N': "forest path", 'S': "inside cottage"}, {"Go north", "Go South"}, [Moonstone], 6)
I figured that I could use this to determine what room the player is in, however I'm not sure if this will do what I want it to do. For example, if player.position = 0 then according to my code they are in the introd room
How can I make it so the player only has access to their current room's attributes and not the attributes of the other Room instances?
Am I heading in the right direction with this or should I be using different structure?
Any tips would be much appreciated, currently browsing forums and youtube but still struggling with the logic.
I'm sorry if this is worded poorly/I'm being unclear here.
player
class knowing itsRoom
via a number, theposition
variable could just store theRoom
itself. E.g, if theplayer
is in the cottage, then you might sayplayer.position = cottage
. Then, you can use the actions by doing say,player.position.actions
. This way, it would be overall simpler, and you can get rid of theposition
variable in theRoom
class as well. Win-win! \$\endgroup\$player
should be an instance of a classPlayer
, position should be a link to a room, rooms should either link directly to each other or else you need a global registry of room name string to Room instance, etc. \$\endgroup\$