Removed unused file
Showing
1 changed file
with
0 additions
and
226 deletions
simplemud.py
deleted
100644 → 0
1 | #!/usr/bin/env python | ||
2 | |||
3 | """A simple Multi-User Dungeon (MUD) game. Players can talk to each | ||
4 | other, examine their surroundings and move between rooms. | ||
5 | |||
6 | Some ideas for things to try adding: | ||
7 | * More rooms to explore | ||
8 | * An 'emote' command e.g. 'emote laughs out loud' -> 'Mark laughs | ||
9 | out loud' | ||
10 | * A 'whisper' command for talking to individual players | ||
11 | * A 'shout' command for yelling to players in all rooms | ||
12 | * Items to look at in rooms e.g. 'look fireplace' -> 'You see a | ||
13 | roaring, glowing fire' | ||
14 | * Items to pick up e.g. 'take rock' -> 'You pick up the rock' | ||
15 | * Monsters to fight | ||
16 | * Loot to collect | ||
17 | * Saving players accounts between sessions | ||
18 | * A password login | ||
19 | * A shop from which to buy items | ||
20 | |||
21 | author: Mark Frimston - mfrimston@gmail.com | ||
22 | """ | ||
23 | |||
24 | import time | ||
25 | |||
26 | # import the MUD server class | ||
27 | from mudserver import MudServer | ||
28 | |||
29 | |||
30 | # structure defining the rooms in the game. Try adding more rooms to the game! | ||
31 | rooms = { | ||
32 | "Tavern": { | ||
33 | "description": "You're in a cozy tavern warmed by an open fire.", | ||
34 | "exits": {"outside": "Outside"}, | ||
35 | }, | ||
36 | "Outside": { | ||
37 | "description": "You're standing outside a tavern. It's raining.", | ||
38 | "exits": {"inside": "Tavern"}, | ||
39 | } | ||
40 | } | ||
41 | |||
42 | # stores the players in the game | ||
43 | players = {} | ||
44 | |||
45 | # start the server | ||
46 | mud = MudServer() | ||
47 | |||
48 | # main game loop. We loop forever (i.e. until the program is terminated) | ||
49 | while True: | ||
50 | |||
51 | # pause for 1/5 of a second on each loop, so that we don't constantly | ||
52 | # use 100% CPU time | ||
53 | time.sleep(0.2) | ||
54 | |||
55 | # 'update' must be called in the loop to keep the game running and give | ||
56 | # us up-to-date information | ||
57 | mud.update() | ||
58 | |||
59 | # go through any newly connected players | ||
60 | for id in mud.get_new_players(): | ||
61 | |||
62 | # add the new player to the dictionary, noting that they've not been | ||
63 | # named yet. | ||
64 | # The dictionary key is the player's id number. We set their room to | ||
65 | # None initially until they have entered a name | ||
66 | # Try adding more player stats - level, gold, inventory, etc | ||
67 | players[id] = { | ||
68 | "name": None, | ||
69 | "room": None, | ||
70 | } | ||
71 | |||
72 | # send the new player a prompt for their name | ||
73 | mud.send_message(id, "What is your name?") | ||
74 | |||
75 | # go through any recently disconnected players | ||
76 | for id in mud.get_disconnected_players(): | ||
77 | |||
78 | # if for any reason the player isn't in the player map, skip them and | ||
79 | # move on to the next one | ||
80 | if id not in players: | ||
81 | continue | ||
82 | |||
83 | # go through all the players in the game | ||
84 | for pid, pl in players.items(): | ||
85 | # send each player a message to tell them about the diconnected | ||
86 | # player | ||
87 | mud.send_message(pid, "{} quit the game".format( | ||
88 | players[id]["name"])) | ||
89 | |||
90 | # remove the player's entry in the player dictionary | ||
91 | del(players[id]) | ||
92 | |||
93 | # go through any new commands sent from players | ||
94 | for id, command, params in mud.get_commands(): | ||
95 | |||
96 | # if for any reason the player isn't in the player map, skip them and | ||
97 | # move on to the next one | ||
98 | if id not in players: | ||
99 | continue | ||
100 | |||
101 | # if the player hasn't given their name yet, use this first command as | ||
102 | # their name and move them to the starting room. | ||
103 | if players[id]["name"] is None: | ||
104 | |||
105 | players[id]["name"] = command | ||
106 | players[id]["room"] = "Tavern" | ||
107 | |||
108 | # go through all the players in the game | ||
109 | for pid, pl in players.items(): | ||
110 | # send each player a message to tell them about the new player | ||
111 | mud.send_message(pid, "{} entered the game".format( | ||
112 | players[id]["name"])) | ||
113 | |||
114 | # send the new player a welcome message | ||
115 | mud.send_message(id, "Welcome to the game, {}. ".format( | ||
116 | players[id]["name"]) | ||
117 | + "Type 'help' for a list of commands. Have fun!") | ||
118 | |||
119 | # send the new player the description of their current room | ||
120 | mud.send_message(id, rooms[players[id]["room"]]["description"]) | ||
121 | |||
122 | # each of the possible commands is handled below. Try adding new | ||
123 | # commands to the game! | ||
124 | |||
125 | # 'help' command | ||
126 | elif command == "help": | ||
127 | |||
128 | # send the player back the list of possible commands | ||
129 | mud.send_message(id, "Commands:") | ||
130 | mud.send_message(id, " say <message> - Says something out loud, " | ||
131 | + "e.g. 'say Hello'") | ||
132 | mud.send_message(id, " look - Examines the " | ||
133 | + "surroundings, e.g. 'look'") | ||
134 | mud.send_message(id, " go <exit> - Moves through the exit " | ||
135 | + "specified, e.g. 'go outside'") | ||
136 | |||
137 | # 'say' command | ||
138 | elif command == "say": | ||
139 | |||
140 | # go through every player in the game | ||
141 | for pid, pl in players.items(): | ||
142 | # if they're in the same room as the player | ||
143 | if players[pid]["room"] == players[id]["room"]: | ||
144 | # send them a message telling them what the player said | ||
145 | mud.send_message(pid, "{} says: {}".format( | ||
146 | players[id]["name"], params)) | ||
147 | |||
148 | # 'look' command | ||
149 | elif command == "look": | ||
150 | |||
151 | # store the player's current room | ||
152 | rm = rooms[players[id]["room"]] | ||
153 | |||
154 | # send the player back the description of their current room | ||
155 | mud.send_message(id, rm["description"]) | ||
156 | |||
157 | playershere = [] | ||
158 | # go through every player in the game | ||
159 | for pid, pl in players.items(): | ||
160 | # if they're in the same room as the player | ||
161 | if players[pid]["room"] == players[id]["room"]: | ||
162 | # ... and they have a name to be shown | ||
163 | if players[pid]["name"] is not None: | ||
164 | # add their name to the list | ||
165 | playershere.append(players[pid]["name"]) | ||
166 | |||
167 | # send player a message containing the list of players in the room | ||
168 | mud.send_message(id, "Players here: {}".format( | ||
169 | ", ".join(playershere))) | ||
170 | |||
171 | # send player a message containing the list of exits from this room | ||
172 | mud.send_message(id, "Exits are: {}".format( | ||
173 | ", ".join(rm["exits"]))) | ||
174 | |||
175 | # 'go' command | ||
176 | elif command == "go": | ||
177 | |||
178 | # store the exit name | ||
179 | ex = params.lower() | ||
180 | |||
181 | # store the player's current room | ||
182 | rm = rooms[players[id]["room"]] | ||
183 | |||
184 | # if the specified exit is found in the room's exits list | ||
185 | if ex in rm["exits"]: | ||
186 | |||
187 | # go through all the players in the game | ||
188 | for pid, pl in players.items(): | ||
189 | # if player is in the same room and isn't the player | ||
190 | # sending the command | ||
191 | if players[pid]["room"] == players[id]["room"] \ | ||
192 | and pid != id: | ||
193 | # send them a message telling them that the player | ||
194 | # left the room | ||
195 | mud.send_message(pid, "{} left via exit '{}'".format( | ||
196 | players[id]["name"], ex)) | ||
197 | |||
198 | # update the player's current room to the one the exit leads to | ||
199 | players[id]["room"] = rm["exits"][ex] | ||
200 | rm = rooms[players[id]["room"]] | ||
201 | |||
202 | # go through all the players in the game | ||
203 | for pid, pl in players.items(): | ||
204 | # if player is in the same (new) room and isn't the player | ||
205 | # sending the command | ||
206 | if players[pid]["room"] == players[id]["room"] \ | ||
207 | and pid != id: | ||
208 | # send them a message telling them that the player | ||
209 | # entered the room | ||
210 | mud.send_message(pid, | ||
211 | "{} arrived via exit '{}'".format( | ||
212 | players[id]["name"], ex)) | ||
213 | |||
214 | # send the player a message telling them where they are now | ||
215 | mud.send_message(id, "You arrive at '{}'".format( | ||
216 | players[id]["room"])) | ||
217 | |||
218 | # the specified exit wasn't found in the current room | ||
219 | else: | ||
220 | # send back an 'unknown exit' message | ||
221 | mud.send_message(id, "Unknown exit '{}'".format(ex)) | ||
222 | |||
223 | # some other, unrecognised command | ||
224 | else: | ||
225 | # send back an 'unknown command' message | ||
226 | mud.send_message(id, "Unknown command '{}'".format(command)) |
-
Please register or sign in to post a comment