Skip to content

Commit 6ba7dcc

Browse files
committed
to infinity and beyond
1 parent 2021901 commit 6ba7dcc

File tree

7 files changed

+184
-52
lines changed

7 files changed

+184
-52
lines changed

command.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
inspect
3+
get
4+
put
5+
use
6+
enter
7+
talk
8+
"""

content.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from room import *
2+
3+
class RoomEntrance(Room):
4+
def on_enter(self):
5+
self.game.output_line("Aloha")

game.py

+83-29
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,100 @@
11
import sys
22
from world import *
3+
from content import *
34

45
class Game (object):
56

67
def __init__(self):
7-
self.world = World();
8+
self.__game_running = True
9+
self.world = World(self);
810
self.world.build();
9-
1011
self.player = self.world.player
1112

1213
def input(self):
1314
return sys.stdin.readline()
1415

16+
def output_line(self, text):
17+
sys.stdout.write(text + "\n")
18+
1519
def output(self, text):
16-
print text
20+
sys.stdout.write(text)
21+
22+
def __game_init(self):
23+
self.output("Pleas enter your name: ")
24+
self.player.name = self.input()
25+
self.player.location = self.world.get_room_by_id("entrance")
26+
27+
def __show_menu(self):
28+
rooms = self.player.location.get_connected_rooms()
29+
for index, room in enumerate(rooms):
30+
self.output_line("[%d]\t%s" % (index + 1, room.name))
31+
for item in self.player.location.inventory.get_items():
32+
self.output_line("%s (%s)" % (item.name, item.id))
33+
34+
def __process_input(self, text):
35+
args = text.lower().strip().split(' ')
36+
cmd = args[0]
37+
args = args[1:]
1738

39+
cmds = {
40+
'go' : self.__command_go,
41+
'get' : self.__command_get,
42+
'put' : self.__command_put,
43+
'use' : self.__command_use,
44+
'talk' : self.__command_talk,
45+
'inventory' : self.__command_inventory,
46+
'exit' : self.__command_exit,
47+
}
48+
49+
try:
50+
cmds[cmd](args)
51+
except KeyError:
52+
self.output("Unknown command. Available: ")
53+
for key in cmds.keys():
54+
self.output(key + " ")
55+
self.output_line("")
56+
57+
def __enter_room(self, num):
58+
rooms = self.player.location.get_connected_rooms()
59+
if num >= 1 and num <= len(rooms):
60+
self.player.location = rooms[num - 1]
61+
else:
62+
self.output_line("Unknown room!")
63+
64+
def __command_go(self, args):
65+
try:
66+
num = int(args[0])
67+
except IndexError, ValueError:
68+
num = 1000
69+
self.__enter_room(num)
70+
71+
def __command_get(self, args):
72+
pass
73+
74+
def __command_put(self, args):
75+
pass
76+
77+
def __command_use(self, args):
78+
pass
79+
80+
def __command_talk(self, args):
81+
pass
82+
83+
def __command_inventory(self, args):
84+
for item in self.player.inventory.get_items():
85+
self.output_line("%s (%s)" % (item.name, item.id))
86+
87+
def __command_exit(self, args):
88+
self.__game_running = False
89+
1890
def run(self):
1991

20-
self.output("Pleas enter your name: ")
21-
self.player.name = self.input()
92+
self.__game_init()
93+
94+
self.__game_running = True
95+
while (self.__game_running):
96+
self.__show_menu()
97+
self.output(": ")
98+
self.__process_input(self.input())
2299

23-
exit_key = 0
24-
while (1):
25-
26-
self.output("You:\t%s" % (self.player.location.name))
27-
28-
rooms = self.player.location.get_connected_rooms()
29-
for index, room in enumerate(rooms):
30-
self.output("[%d]\t%s" % (index + 1, room.name))
31-
exit_key = index + 2
32-
self.output("[%d]\tExit" % (exit_key))
33-
34-
self.output("Go to: ")
35-
input = self.input()
36-
try:
37-
num = int(input)
38-
except ValueError:
39-
num = 1000
40-
41-
if num >= 1 and num <= len(rooms):
42-
self.player.location = rooms[num - 1]
43-
elif num == exit_key:
44-
break
45-
else:
46-
self.output("Unknown key!")
100+
self.output("Good bye %s" % (self.player.name))

item.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Item(object):
22
""" This class represents a item. """
33

4-
def __init__(self, id, name, location):
4+
def __init__(self, game, id, name):
5+
self.game = game
56
self.id = id
67
self.name = name
78
self.description = ""
89
self.fixed = True
9-
self.location = location
10-
self.inventory = None
10+
self.__inventory = None
1111

1212
def __str__(self):
1313
return self.id
@@ -26,3 +26,24 @@ def set_description(self, description):
2626

2727
def get_description(self, description):
2828
return self.description
29+
30+
def __get_inventory(self):
31+
return self.__inventory
32+
33+
def __set_inventory(self, inventory):
34+
self.__inventory = inventory
35+
36+
inventory = property(__get_inventory, __set_inventory)
37+
38+
class Inventory(object):
39+
40+
def __init__(self):
41+
self.__items = []
42+
43+
def add_item(self, item):
44+
if item.inventory:
45+
item.inventory.__items.remove(item)
46+
self.__items.append(item)
47+
48+
def get_items(self):
49+
return self.__items

player.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
class Player(object):
55

6-
def __init__(self, location):
6+
def __init__(self, game, location):
7+
self.game = game
78
self.__name = "Nobody"
89
self.__location = location
9-
10-
def __del__(self):
11-
print "Good bye %s" % (self.name)
12-
10+
self.inventory = Inventory()
11+
1312
def __get_name(self):
1413
return self.__name
1514

@@ -20,7 +19,13 @@ def __get_location(self):
2019
return self.__location
2120

2221
def __set_location(self, location):
22+
# leave current room
23+
if self.__location:
24+
self.__location.leave()
25+
# set new location
2326
self.__location = location
27+
# enter new location
28+
self.__location.enter()
2429

2530
name = property(__get_name, __set_name)
2631
location = property(__get_location, __set_location)

room.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
class Room(object):
44
""" This class represents a room. """
55

6-
def __init__(self, id, name):
6+
def __init__(self, game, id, name):
77
self.id = id
88
self.name = name;
99
self.description = ""
1010
self.connections = []
11+
self.game = game
12+
self.__first_enter = True
13+
self.inventory = Inventory()
1114

1215
def __str__(self):
1316
return self.id
@@ -39,4 +42,25 @@ def get_items(self):
3942
pass
4043

4144
def get_connected_rooms(self):
42-
return self.connections
45+
return self.connections
46+
47+
def enter(self):
48+
if self.__first_enter:
49+
self.__first_enter = False
50+
self.on_first_enter()
51+
self.on_enter()
52+
53+
def leave(self):
54+
self.on_leave()
55+
56+
57+
58+
def on_first_enter(self):
59+
self.game.output_line("You first entered %s" % (self.name))
60+
61+
def on_enter(self):
62+
self.game.output_line("You enter %s" % (self.name))
63+
64+
def on_leave(self):
65+
self.game.output_line("You leave %s" % (self.name))
66+

world.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
from room import *
22
from player import *
3+
from content import *
34

45
class World(object):
56

6-
def __init__(self):
7+
def __init__(self, game):
8+
self.game = game
79
self.__rooms = []
8-
self.rooms_by_id = {}
9-
self.__player = Player(None)
10-
10+
self.__rooms_by_id = {}
11+
self.__items = []
12+
self.__items_by_id = {}
13+
self.__player = Player(game, None)
1114

1215
def build(self):
13-
room = self.__add_room(Room("living_room", "Living Room"))
16+
room = self.__add_room(Room(self.game, "living_room", "Living Room"))
1417
room.set_description("This is where you live.")
18+
self.__add_item(Item(self.game, "table", "A Table"), room.inventory)
1519

16-
room = self.__add_room(Room("bedroom", "Bedroom"))
20+
room = self.__add_room(Room(self.game, "bedroom", "Bedroom"))
1721
room.set_description("This is where you sleep.")
1822

19-
room = self.__add_room(Room("toilet", "Toilet"))
23+
room = self.__add_room(Room(self.game, "toilet", "Toilet"))
2024
room.set_description("This is where you do your private stuff.")
2125

22-
room = self.__add_room(Room("dining_room", "Dining Room"))
26+
room = self.__add_room(Room(self.game, "dining_room", "Dining Room"))
2327
room.set_description("This is where you eat your Meal.")
2428

25-
room = self.__add_room(Room("balcony", "Balcony"))
29+
room = self.__add_room(Room(self.game, "balcony", "Balcony"))
2630
room.set_description("This is where you Chill.")
2731

28-
room = self.__add_room(Room("entrance", "Entrance"))
32+
room = self.__add_room(RoomEntrance(self.game, "entrance", "Entrance"))
2933
room.set_description("This is where you enter the Flat.")
34+
self.__add_item(Item(self.game, "gun_rack", "A Gun Rack"), room.inventory)
3035

3136
self.__connect_rooms("living_room", "bedroom")
3237
self.__connect_rooms("living_room", "dining_room")
@@ -38,10 +43,12 @@ def build(self):
3843

3944
self.__connect_rooms("bedroom", "toilet")
4045

41-
self.__player.location = self.get_room_by_id("entrance")
46+
self.__add_item(Item(self.game, "pen", "A Pen"), self.player.inventory)
47+
self.__add_item(Item(self.game, "paper", "A piece of Paper"), self.player.inventory)
48+
4249

4350
def get_room_by_id(self, id):
44-
return self.rooms_by_id[id]
51+
return self.__rooms_by_id[id]
4552

4653
def __connect_rooms(self, a, b):
4754
room_a = self.get_room_by_id(a)
@@ -50,9 +57,17 @@ def __connect_rooms(self, a, b):
5057

5158
def __add_room(self, room):
5259
self.__rooms.append(room)
53-
self.rooms_by_id[room.get_id()] = room
60+
self.__rooms_by_id[room.get_id()] = room
5461
return room
5562

63+
def __add_item(self, item, inventory):
64+
self.__rooms.append(item)
65+
self.__rooms_by_id[item.get_id()] = item
66+
inventory.add_item(item)
67+
68+
def get_item_by_id(self, id):
69+
return self.__items_by_id[id]
70+
5671
def __get_rooms(self):
5772
return self.__rooms
5873

0 commit comments

Comments
 (0)