-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattleframe.py
264 lines (220 loc) · 8.96 KB
/
battleframe.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
import boardframe
import collections
import creature
import dialogueframe
import graphics
import inventoryframe
import menuframe
import stateframe
from boardframe import MakeCreatureMenu
from collections import OrderedDict
from creature import Creature
from dialogueframe import DialogueFrame
from graphics import DisplayTextBox, Sprite, SpriteError
from inventoryframe import InventoryFrame
from menuframe import MenuFrame
from stateframe import StateFrame
class BattleEndError(Exception):
pass
class BattleFrame(StateFrame):
def __init__(self, player, npc, winText, loseText, winFunction, loseFunction):
super(BattleFrame, self).__init__()
assert len(player.creatures) > 0
assert len(npc.creatures) > 0
self._player = player
self._npc = npc
self._winFunction = winFunction
self._loseFunction = loseFunction
self._winText = winText
self._loseText = loseText
self._playerCreature = self._player.creatures[0]
self._npcCreature = self._npc.creatures[0]
self._runner = self._RunGame()
def Update(self):
try:
self._runner.next()
except StopIteration:
if self.visible:
self.KillSelf()
def Render(self, ctx, size):
self._DrawCreature(ctx, 10, self._playerCreature)
self._DrawCreature(ctx, 420, self._npcCreature)
def _CheckCreatures(self):
for _ in self._CheckDead():
yield
if self._playerCreature.IsDead():
MenuFrame.Show(self._CreateSwitchMenu(), 'Next Victim', (20, 400), 16, 16)
yield
if self._npcCreature.IsDead() or not self._npcCreature in self._npc.creatures:
# select a live npc creature
for c in self._npc.creatures:
if not c.IsDead():
self._npcCreature = c
DialogueFrame('The enemy switches in {0}!!!'.format(c.name)).Show()
yield
def _CheckDead(self):
if not self._npc.HasLiveCreature():
DialogueFrame(self._winText).Show()
yield
self.KillSelf()
self._winFunction()
raise BattleEndError()
if not self._player.HasLiveCreature():
DialogueFrame(self._loseText).Show()
yield
self.KillSelf()
self._loseFunction()
while len(stateframe.stack) > 1:
del stateframe.stack[-1] # back to main menu
raise BattleEndError()
def _CreateSwitchMenu(self):
def DoSwitch(idx, c):
self._playerCreature = c
if c.name == 'Dog':
DialogueFrame('Go, Dog. Go!').Show()
else:
DialogueFrame('Go {0}!!!'.format(c.name)).Show()
return MakeCreatureMenu(self._player, DoSwitch,
lambda c: not c.IsDead() and c != self._playerCreature)
def _CreateMenu(self):
def DoPlayerAttack(attack):
self._menuResult = self._playerCreature.AttackGenerator(attack, self._npcCreature, True)
return False
attacks = OrderedDict()
for attack in self._playerCreature.attacks:
attacks[attack.name] = lambda attack=attack : DoPlayerAttack(attack)
def TryRun():
DialogueFrame('You think about running, but decide that you are too lazy.').Show()
return False
def DoNothing():
DialogueFrame('You quack some and try to fly.').Show()
return False
playerOptions = OrderedDict()
playerOptions['Attack'] = attacks
playerOptions['Use Item'] = InventoryFrame(self._player, npc=self._npc,
enemy=self._npcCreature)
playerOptions['Switch Creatures'] = self._CreateSwitchMenu()
playerOptions['Run Like a MOFO'] = TryRun
playerOptions['Sit Like a Duck'] = DoNothing
return playerOptions
def _DrawCreature(self, ctx, startx, creature):
ctx.save()
HEALTH_WIDTH = 350
HEALTH_HEIGHT = 34
ctx.translate(startx, 10)
ctx.set_source_rgb(.5, .5, .5)
ctx.rectangle(0, 0, HEALTH_WIDTH, HEALTH_HEIGHT)
ctx.fill()
if not creature.IsDead():
ctx.set_source_rgb(1, 0, 0)
ctx.rectangle(0, 0, (creature.health / creature.maxHealth) * HEALTH_WIDTH, HEALTH_HEIGHT)
ctx.fill()
topText = '%s: lvl %s' % (creature.name, creature.level)
DisplayTextBox(ctx, topText, boxSize=(HEALTH_WIDTH, HEALTH_HEIGHT), DRAW_BACKGROUND=False)
ctx.translate(0, HEALTH_HEIGHT + 50)
sprite = Sprite(creature.name)
spriteScale = (HEALTH_WIDTH - 10) / sprite.width
ctx.translate(HEALTH_WIDTH / 2 - sprite.width * spriteScale / 2, 0)
ctx.scale(spriteScale, spriteScale)
sprite.Render(ctx)
ctx.restore()
def _NPCTurn(self):
first = True
while first or self._npcCreature.IsDead():
first = False
for _ in self._CheckCreatures():
yield
for msg in self._npcCreature.UpdateGenerator():
DialogueFrame(msg).Show()
yield
move = self._npc.GetNextMove(self._npcCreature, self._playerCreature)
story = list()
if move[0] == 'attack':
attack = move[1]
for msg in self._npcCreature.AttackGenerator(attack, self._playerCreature):
DialogueFrame(msg).Show()
yield
elif move[0] == 'item':
DialogueFrame('Items do not work.').Show()
yield
elif move[0] == 'switch':
self._npcIndex = self._npc.creatures.index(move[1])
DialogueFrame('The enemy switches creatures.').Show()
yield
else:
DialogueFrame('The game is FULL of bugs. The NPC does nothing in protest.').Show()
yield
def _PlayerTurn(self):
first = True
while first or self._playerCreature.IsDead():
first = False
for _ in self._CheckCreatures():
yield
for msg in self._playerCreature.UpdateGenerator():
DialogueFrame(msg).Show()
yield
self._menuResult = None
MenuFrame.Show(self._CreateMenu(), 'What Now?', (20, 400), 16, 16)
yield
if self._menuResult is not None:
for msg in self._menuResult:
DialogueFrame(msg).Show()
yield
def _RunGame(self):
npcName = 'A random ' + self._npc._sprite.name
DialogueFrame([
'{0} wants to fight!'.format(npcName),
'You use {0}'.format(self._playerCreature.name),
'{0} uses {1}'.format(npcName, self._npcCreature.name)
]).Show()
yield
if not (self._npc.HasLiveCreature() or self._player.HasLiveCreature()):
DialogueFrame('BUG: Someone has no live creature').Show()
yield
exit(1)
roundCount = 1
try:
while True:
# each round has two turns, player and npc
DialogueFrame('Round {0}!'.format(roundCount)).Show()
yield
if self._playerCreature.speed >= self._npcCreature.speed:
DialogueFrame('You go first.').Show()
yield
toRun = [self._PlayerTurn(), self._NPCTurn()]
else:
DialogueFrame('Your opponent goes first.').Show()
yield
toRun = [self._NPCTurn(), self._PlayerTurn()]
for gen in toRun:
for _ in gen:
yield
for _ in self._CheckCreatures():
yield
roundCount += 1
except BattleEndError:
pass
def StartFight(player, npc):
stateframe.stack.append(BattleFrame(player, npc, npc.winText, npc.loseText, npc.winFunction, npc.loseFunction))
if __name__ == '__main__':
import board
import entity
import main
from stateframe import FrameUpdate
board = board.Board('test')
player = entity.Player('hobo', (0,0), board)
player.AddCreature(Creature('Dog'))
player.AddCreature(Creature('Rat'))
player.AddCreature(Creature('Programmer'))
player.AddItem(entity.POSSIBLE_INVENTORY_ITEMS['spiked drink'][0])
player.AddItem(entity.POSSIBLE_INVENTORY_ITEMS['thunderbird'][0])
player.AddItem(entity.POSSIBLE_INVENTORY_ITEMS['thunderbird'][0])
player.AddItem(entity.POSSIBLE_INVENTORY_ITEMS['speed'][0])
npc = entity.NPC('hobo', (1,0), board)
npc.AddCreature(Creature('Rat'))
npc.AddCreature(Creature('Dog'))
npc.AddCreature(Creature('Programmer'))
npc.AddFightInfo('test win', 'test lose')
StartFight(player, npc)
win = main.Window('BattleFrame test')
win.run(FrameUpdate)