-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
361 lines (316 loc) · 12.5 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import yaml, time, sys
from collections import deque
class Game(object):
"""
This is the main class. A Game instance holds all data needed to play the game.
Therefore the Game-YAML is parsed and all the Items / Places / Actions get initialized.
This class also executes the operations.
To write a own game a own yaml-file has to be configured.
"""
def __init__(self, loadfile = None):
super(Game, self).__init__()
stream = open("batzgame.yaml", 'r')
gamedata = yaml.load(stream)
self.objects = dict()
#allow multiple names
for o in gamedata['objects']:
Game.addOrAppendToDict(self.objects, o.name, o)
self.title = gamedata['title']
self.introduction = gamedata['introduction']
self.places = dict([ (p.name , p) for p in gamedata['places'] ])
self.startplace = self.places[gamedata['startplace']]
self.actions = dict()
for action in gamedata['actions']:
self.addAction(action)
State.inventory = gamedata['startinventory']
State.actionsleft = gamedata['startactions']
State.money = gamedata['startmoney']
State.drawpile = deque(gamedata['startdrawpile'])
State.sandrapile = deque(gamedata['startsandrapile'])
State.sandrahand = gamedata['startsandrahand']
State.startactions = gamedata['startactions']
State.startmoney = gamedata['startmoney']
State.startdrawpile = gamedata['startdrawpile']
State.startsandrapile = gamedata['startsandrapile']
State.startsandrahand = gamedata['startsandrahand']
if not loadfile:
self.initNewGame()
else:
self.loadGame(loadfile)
def loadGame(self, loadfile):
stream = open(loadfile+".sav", 'r')
gamedata = yaml.load(stream)
#Itemsveränderungen übernehmen
for o_name in gamedata['objects']:
for o in gamedata['objects'][o_name]:
for current_obj in self.objects[o_name]:
if current_obj.getID() == o.getID():
current_obj.copyFrom(o)
State.inventory = gamedata['inventory']
State.actionsleft = gamedata['actionsleft']
State.money = gamedata['money']
State.drawpile = deque(gamedata['drawpile'])
State.sandrahand = gamedata['sandrahand']
State.sandrapile = deque(gamedata['sandrapile'])
self.changePlace(self.places[gamedata['currentplace'].name])
def initNewGame(self):
Game.animatedprint(self.introduction)
#time.sleep(3)
self.changePlace(self.startplace)
def addAction(self, action):
if hasattr(action, 'target2'):
Game.addOrAppendToDict(self.actions, action.operation + action.target + ' ' + action.target2, action)
Game.addOrAppendToDict(self.actions, action.operation + action.target2 + ' ' + action.target, action)
else:
if hasattr(action, 'target'):
Game.addOrAppendToDict(self.actions, action.operation + action.target, action)
else:
Game.addOrAppendToDict(self.actions, action.operation, action)
def changePlace(self, where):
State.currentplace = where
Game.animatedprint(where.description)
def examine(self, what):
if what in self.objects:
items = self.objects[what]
for item in items:
if item.isActive() and (item.isInInventory() or item.isAt(State.currentplace.name)):
Game.animatedprint(item.description)
return
Game.animatedprint("Ich kann "+what+" nicht finden.\n")
def use(self, what):
if not self.executeOperation(what, "benutze"):
print("Nichts besonderes passiert.")
def take(self, what):
if what in self.objects:
for take_obj in self.objects[what]:
if take_obj.isActive() and take_obj.isNotInInventory() and take_obj.isAt(State.currentplace.name):
if self.executeOperation(what, "nehme"):
if not take_obj.isDestroyed():
take_obj.toInventory()
return
print("Das kann ich nicht tun.")
def go(self, where):
if not self.executeOperation(where, "gehe"):
print("Das kann ich nicht tun")
def thumbup(self):
if not self.executeOperation(None, "daumenraus"):
print("Ich halte den Daumen raus... nichts geschieht.")
def checkInventory(self):
for item in State.inventory:
print(item)
def umsehen(self):
print(State.currentplace.description)
def executeOperation(self, what, operation):
if what:
actioncode = operation+what
else:
actioncode = operation
if actioncode in self.actions:
actions = self.actions[actioncode]
for action in actions:
conditions_met = True
if hasattr(action, 'conditions'):
for condition in action.conditions:
if 'obj' in condition:
obj_name = condition['obj']
if 'id' in condition:
obj_id = condition['id']
else:
obj_id = 0
for condition_object in self.objects[obj_name]:
if condition_object.getID() == obj_id:
checker = getattr(condition_object, condition['check'])
if 'param' in condition:
param = condition['param']
if not checker(param):
conditions_met = False
break
else:
if not checker():
conditions_met = False
break
else:
condition_place = condition['place']
if State.currentplace.name != condition_place:
conditions_met = False
if(conditions_met):
Game.animatedprint(action.text)
if hasattr(action, 'changes'):
for change in action.changes:
if 'obj' in change:
obj_name = change['obj']
if 'id' in change:
obj_id = change['id']
else:
obj_id = 0
for change_object in self.objects[obj_name]:
if change_object.getID() == obj_id:
apply_change = getattr(change_object, change['change'])
param = None
if 'param' in change:
param = change['param']
if param:
apply_change(param)
else:
apply_change()
else:
if 'place' in change:
change_place = self.places[change['place']]
self.changePlace(change_place)
else:
if 'drawCard' in change:
amount = change['drawCard']
self.drawCard(amount)
else:
if 'actionDiff' in change:
diff = change['actionDiff']
self.adjustActions(diff)
else:
if 'specialAction' in change:
self.specialAction(change['specialAction'])
else:
print("unknownchange")
return True
return False
def drawCard(self, amount):
Game.animatedprint("Du ziehst " + str(amount) + " Karten von deinem Nazistapel: \n")
for _ in range(amount):
card = self.objects[State.drawpile.popleft()][0]
card.activate()
card.toInventory()
Game.animatedprint(card.name + "\n")
def adjustActions(self, diff):
if (diff == 1):
Game.animatedprint("+ " + str(diff) + " Aktion. \n")
else:
if (diff > 1):
Game.animatedprint("+ " + str(diff) + " Aktionen. \n")
State.actionsleft += (diff - 1)
Game.animatedprint("Übrige Aktionen: " + str(State.actionsleft) + "\n")
def specialAction(self, cardname):
switcher = {
'maskerade': self.maskerade,
'miliz': self.miliz,
'raeuber': self.raeuber,
'dieb': self.dieb
}
func = switcher.get(cardname)
func()
self.checkDominion()
def save(self, name):
stream = open(name+'.sav', 'w')
data = dict()
data['objects'] = self.objects
data['inventory'] = State.inventory
data['currentplace'] = State.currentplace
data['actionsleft'] = State.actionsleft
data['money'] = State.money
data['drawpile'] = State.drawpile
data['sandrapile'] = State.sandrapile
data['sandrahand'] = State.sandrahand
yaml.dump(data, stream)
def resetDominion(self, message):
Game.animatedprint(message + '\n')
self.objects['laboratorium'][0].destroy()
self.objects['laboratorium'][0].toInventory()
self.objects['dorf'][0].destroy()
self.objects['maskerade'][0].destroy()
self.objects['grenzdorf'][0].destroy()
self.objects['miliz'][0].destroy()
self.objects['dieb'][0].destroy()
self.objects['räuber'][0].destroy()
self.objects['anwesen'][0].destroy()
State.drawpile = deque(State.startdrawpile)
State.sandrahand = list(State.startsandrahand)
State.actionsleft = State.startactions
State.money = State.startmoney
State.sandrapile = deque(State.startsandrapile)
def checkDominion(self):
if State.actionsleft == 0 and State.money < 150:
self.resetDominion("Ich habe keine Aktionen mehr und nicht genug Geld geklaut.. Gut, dass ich mir die gesamte Combo nur im Kopf vorgestellt habe.")
if State.actionsleft == 0 and State.money == 150:
self.changePlace(self.places["fahrt.kielpb.ende"])
self.changePlace(self.places["paderborn.waldrand"])
@staticmethod
def animatedprint(string):
for c in string:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.002)
@staticmethod
def addOrAppendToDict(d, key, newvalue):
if key in d:
oldvalue = d[key]
oldvalue.append(newvalue)
d[key] = oldvalue
else:
d[key] = list([newvalue])
### DOMINION ACTIONS!
def maskerade(self):
sandracard = self.objects[State.sandrahand.pop()][0]
mycard = self.objects['anwesen'][0]
if not mycard.isInInventory():
self.resetDominion('Ich habe keinen Schrott, den ich Sandra geben könnte.. gut, dass ich mir die gesamte Combo nur im Kopf vorgestellt habe..\n')
else:
State.sandrahand.append(mycard.name)
if sandracard.name == 'fuffi':
State.money += 50
Game.animatedprint("Ich gebe Sandra ein Anwesen und bekomme von ihr einen Fuffi. Ich habe jetzt " + str(State.money) + " Euro geklaut. \n")
mycard.destroy()
def miliz(self):
for _ in range(2):
sandracard = State.sandrahand.pop()
State.sandrapile.append(sandracard)
Game.animatedprint("Sandra legt zwei Karten ab. \n")
def dieb(self):
stolen = False
for _ in range(2):
if (len(State.sandrapile) > 0):
sandracard = State.sandrapile[0]
if 'fuffi' == sandracard:
if not stolen:
stolen = True
State.money += 50
State.sandrapile.remove(sandracard)
Game.animatedprint('Sandra deckt einen Fuffi von ihrem Ablagestapen auf. Sie zerreißt ihn vor meinen Augen und in meiner Hosentasche erscheint ein Fuffi auf magische Art und Weise. Ich habe jetzt ' + str(State.money) + " Euro geklaut.\n")
else:
State.sandrapile.rotate(-1)
Game.animatedprint('Sandra deckt einen weiteren Fuffi auf und legt ihn ab.\n')
else:
State.sandrapile.rotate(-1)
Game.animatedprint('Sandra deckt die Karte ' + sandracard + ' auf und legt sie ab.\n')
def raeuber(self):
stolen = False
for _ in range(2):
if (len(State.sandrapile) > 0):
sandracard = State.sandrapile[0]
if 'fuffi' == sandracard:
if not stolen:
stolen = True
State.money += 50
State.sandrapile.remove(sandracard)
Game.animatedprint('Sandra deckt einen Fuffi von ihrem Ablagestapen auf. Sie zerreißt ihn vor meinen Augen und in meiner Hosentasche erscheint ein Fuffi auf magische Art und Weise. Ich habe jetzt ' + str(State.money) + " Euro geklaut.\n")
else:
State.sandrapile.rotate(-1)
Game.animatedprint('Sandra deckt einen weiteren Fuffi auf und legt ihn ab.\n')
else:
State.sandrapile.rotate(-1)
Game.animatedprint('Sandra deckt die Karte ' + sandracard + ' auf und legt sie ab.\n')
if not stolen:
Game.animatedprint('Sandra muss sich ein 5 Cent Stück nehmen, weil ich kein Geld geklaut habe.\n')
class State(object):
"""docstring for State"""
objects = dict()
inventory = list()
currentplace = None
"Dominion stuff"
actionsleft = 1
money = 0
drawpile = list()
sandrapile = list()
sandrahand = list()
startactions = 1
startmoney = 0
startdrawpile = list()
startsandrapile = list()
startsandrahand = list()