Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- os.system("cls")
- class Gamestate:
- def __init__(self):
- self.rooms = {}
- self.import_rooms()
- self.commands = {}
- self.make_commands()
- self.player = Player(name="blank", health=100, damage=15, location=1, work_experiance=0)
- self.get_player_room()
- self.configure_rooms()
- def make_commands(self):
- self.commands = {"go": Commands(name="go", function=self.going_places, type="movement", description="To go to places"),
- "north": Commands(name="go", function=self.going_places, type="movement", description="To move to a north room"),
- "stats": Commands(name="stats", function=self.print_stats, type="verbosity", description="the print the stats of something"),
- "player": Commands(name="player", function=self.print_player_stats, type="verbosity", description="to print the players stats")}
- def configure_rooms(self):
- # configure north south east and west
- for room in self.rooms.keys():
- exits = []
- nsew = self.rooms[room].nsew
- for number in nsew:
- exits.append(int(number))
- self.rooms[room].nsew = exits
- for room in self.rooms.keys():
- north = self.rooms[room].nsew[0]
- south = self.rooms[room].nsew[1]
- east = self.rooms[room].nsew[2]
- west = self.rooms[room].nsew[3]
- self.rooms[room].north = north
- self.rooms[room].south = south
- self.rooms[room].east = east
- self.rooms[room].west = west
- # working on exits now
- for room in self.rooms.keys():
- exits = []
- for exit in self.rooms[room].exits:
- exits.append(int(exit))
- self.rooms[room].exits = exits
- def going_places(self, user_input):
- if "north" in user_input:
- if self.player.location.north != 0:
- for room in self.rooms.keys():
- if self.player.location.north == self.rooms[room].rnum:
- self.player.location = self.rooms[room]
- if "south" in user_input:
- if self.player.location.south != 0:
- for room in self.rooms.keys():
- if self.player.location.south == self.rooms[room].rnum:
- self.player.location = self.rooms[room]
- if "east" in user_input:
- if self.player.location.east != 0:
- for room in self.rooms.keys():
- if self.player.location.east == self.rooms[room].rnum:
- self.player.location = self.rooms[room]
- if "west" in user_input:
- if self.player.location.west != 0:
- for room in self.rooms.keys():
- if self.player.location.west == self.rooms[room].rnum:
- self.player.location = self.rooms[room]
- # lets see if the user entered a room name instead of north south east or west
- if self.player.location.north != 0:
- for room in self.rooms.keys():
- if self.player.location.north == self.rooms[room].rnum:
- room_name = self.rooms[room].rname.lower().split()
- x = 0
- for word in user_input:
- for name in room_name:
- if word == name:
- x += 1
- if x == len(room_name):
- self.player.location = self.rooms[room]
- if self.player.location.south != 0:
- for room in self.rooms.keys():
- if self.player.location.south == self.rooms[room].rnum:
- room_name = self.rooms[room].rname.lower().split()
- x = 0
- for word in user_input:
- for name in room_name:
- if word == name:
- x += 1
- if x == len(room_name):
- self.player.location = self.rooms[room]
- if self.player.location.east != 0:
- for room in self.rooms.keys():
- if self.player.location.east == self.rooms[room].rnum:
- room_name = self.rooms[room].rname.lower().split()
- x = 0
- for word in user_input:
- for name in room_name:
- if word == name:
- x += 1
- if x == len(room_name):
- self.player.location = self.rooms[room]
- if self.player.location.west != 0:
- for room in self.rooms.keys():
- if self.player.location.west == self.rooms[room].rnum:
- room_name = self.rooms[room].rname.lower().split()
- x = 0
- for word in user_input:
- for name in room_name:
- if word == name:
- x += 1
- if x == len(room_name):
- self.player.location = self.rooms[room]
- # lets assume the user wanted to enter an exit location
- if self.player.location.exits[0] != 0:
- for exit in self.player.location.exits:
- for room in self.rooms.keys():
- if exit == self.rooms[room].rnum:
- exit_name = self.rooms[room].rname.lower().split()
- x = 0
- for word in user_input:
- for name in exit_name:
- if word == name:
- x += 1
- if x == len(exit_name):
- self.player.location = self.rooms[room]
- general_name = self.rooms[room].general_location.lower().split()
- z = 0
- for word in user_input:
- for name in general_name:
- if word == name:
- z += 1
- if z == len(general_name):
- self.player.location = self.rooms[room]
- def get_player_room(self):
- for room in self.rooms.keys():
- if self.player.location == self.rooms[room].rnum:
- self.player.location = self.rooms[room]
- def take_input(self):
- user_input = input("\nCommand: ").lower().split()
- self.going_places(user_input)
- def do_look(self):
- # printing out the current room
- print("\n[Location]")
- print("Room name: %s" % (self.player.location.rname))
- print("Room description: %s" % (self.player.location.rdesc))
- # print the exits of leaving general location
- if self.player.location.exits[0] != 0:
- print("\n[Exits]")
- for room in self.rooms.keys():
- for exit in self.player.location.exits:
- if exit == self.rooms[room].rnum:
- print("%s: %s" % (self.rooms[room].general_location, self.rooms[room].rname))
- # print the room exits
- print("\n[Room Exits]")
- if self.player.location.north != 0:
- for room in self.rooms.keys():
- if self.player.location.north == self.rooms[room].rnum:
- print("North: %s" % (self.rooms[room].rname))
- if self.player.location.south != 0:
- for room in self.rooms.keys():
- if self.player.location.south == self.rooms[room].rnum:
- print("South: %s" % (self.rooms[room].rname))
- if self.player.location.east != 0:
- for room in self.rooms.keys():
- if self.player.location.east == self.rooms[room].rnum:
- print("East: %s" % (self.rooms[room].rname))
- if self.player.location.west != 0:
- for room in self.rooms.keys():
- if self.player.location.west == self.rooms[room].rnum:
- print("West: %s" % (self.rooms[room].rname))
- def import_rooms(self):
- f = open("rooms.txt", "r")
- lines = f.readlines()
- loop = 0
- while loop <= len(lines) - 1:
- self.rooms[lines[loop+1]] = Room(rnum=int(lines[loop]), rname=lines[loop+1].strip("\n"), rdesc=lines[loop+2].strip("\n"), general_location=lines[loop+3].strip("\n"), nsew=lines[loop+4].split(), exits=lines[loop+5].split())
- loop += 6
- class Commands:
- def __init__(self, name, function, type, description):
- self.name = name
- self.function = function
- self.type = type
- self.description = description
- class Player:
- def __init__(self, name, health, damage, location, work_experiance):
- self.name = name
- self.health = health
- self.damage = damage
- self.location = location
- self.work_experiance = work_experiance
- class Room:
- def __init__(self, rnum=None, rname=None, rdesc=None, general_location=None, nsew=None, north=None, south=None, east=None, west=None, exits=None):
- self.rnum = rnum
- self.rname = rname
- self.rdesc = rdesc
- self.general_location = general_location
- self.nsew = nsew
- self.north = north
- self.south = south
- self.east = east
- self.west = west
- self.exits = exits
- gamestate = Gamestate()
- def main():
- gamestate.player.name = input("\nWhats your name?\nName: ")
- print("\nThank you for playing my game %s" % (gamestate.player.name))
- while True:
- gamestate.do_look()
- gamestate.take_input()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement