-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
254 lines (223 loc) · 9.98 KB
/
player.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
from cmu_112_graphics import *
from helpers import *
from sound import *
# getYs and cutSpritesheet from helpers.py
# https://www.cs.cmu.edu/~112/notes/notes-oop-part1.html#oopExample
# Citation ^ 9. Example: Animation with OOP
class Player:
def __init__(self, app):
self.hp = 100
self.maxHP = 100
self.actions = {}
self.state = 'walk'
self.startWalkIn = True
self.walkCycles = 0
self.animationCounter = 0
self.timeAfterDeath = 0
self.walkX = 0
self.combatTuple = (0, 0, 'vulnerable')
self.callGameOver = False
spritesheet = app.loadImage('./assets/playerSheet.png')
self.icon = app.loadImage('./assets/fire_knight.png')
# Load sounds
self.downAudio = Sound('./assets/audio/downAudio.mp3')
self.sideAudio = Sound('./assets/audio/slashAudio.mp3')
self.blockAudio = Sound('./assets/audio/blockAudio.mp3')
# https://chierit.itch.io/elementals-fire-knight
xWidth = 288
startX = 0
# Citation for all loads & redraw...
# https://www.cs.cmu.edu/~112/notes/notes-animations-part4.html#
# spritesheetsWithCropping
# Load idle
self.idle = []
for i in range(8):
(startY, endY) = getYs(0)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'vulnerable')
self.idle.append((img, dmg, blockEff, vulnerability))
# Load walk
self.walk = []
for i in range(8):
(startY, endY) = getYs(1)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'vulnerable')
self.walk.append((img, dmg, blockEff, vulnerability))
# Load side
self.side = []
for i in [0, 14, 15, 0]:
(startY, endY) = getYs(8)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
if i == 0:
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'vulnerable')
else:
(img, dmg, blockEff, vulnerability) = (animation, 10, 0,
'vulnerable')
self.side.append((img, dmg, blockEff, vulnerability))
# Load down
self.down = []
for i in range(11):
(startY, endY) = getYs(7)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
if i == 4:
(img, dmg, blockEff, vulnerability) = (animation, 25, 0,
'vulnerable')
elif i == 5:
(img, dmg, blockEff, vulnerability) = (animation, 5, 0,
'vulnerable')
else:
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'vulnerable')
self.down.append((img, dmg, blockEff, vulnerability))
# Load block
self.block = []
for i in [0, 1, 2, 2, 2, 2, 8, 9]:
(startY, endY) = getYs(11)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
if i == 2:
(img, dmg, blockEff, vulnerability) = (animation, 0, 1,
'invulnerable')
else:
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'vulnerable')
self.block.append((img, dmg, blockEff, vulnerability))
# Load hit
self.hit = []
for i in range(6):
(startY, endY) = getYs(12)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
(img, dmg, blockEff, vulnerability) = (animation, 0, 1,
'invulnerable')
self.hit.append((img, dmg, blockEff, vulnerability))
# Load death
self.death = []
for i in range(12):
(startY, endY) = getYs(13)
animation = cutSpritesheet(startX, xWidth*i, xWidth, startY, endY,
spritesheet, app)
(img, dmg, blockEff, vulnerability) = (animation, 0, 0,
'invulnerable')
self.death.append((img, dmg, blockEff, vulnerability))
def drawIcon(self, app, canvas):
canvas.create_image(app.width//50, app.height//50, image=ImageTk.PhotoImage(self.icon), anchor = 'nw')
def drawMaxHP(self, app, canvas):
self.maxHPBarLength = (app.width//25)*10 - (app.width//25)*2
canvas.create_rectangle((app.width//25)*2, app.width//50,
(app.width//25)*10, (app.width//50)*3, width = 5)
def drawCurrentHP(self, app, canvas):
hpSliceLength = self.hp/self.maxHP * self.maxHPBarLength
if self.hp > 0:
canvas.create_rectangle((app.width//25)*2, app.width//50,
hpSliceLength + (app.width//25)*2, (app.width//50)*3, fill = 'blue')
def redraw(self, app, canvas):
self.drawIcon(app, canvas)
self.drawMaxHP(app, canvas)
self.drawCurrentHP(app, canvas)
# Change animation set (self.___) depending on state
if self.state == 'down':
animation = self.down[self.animationCounter][0]
elif self.state == 'walk':
animation = self.walk[self.animationCounter][0]
elif self.state == 'side':
animation = self.side[self.animationCounter][0]
elif self.state == 'hit':
animation = self.hit[self.animationCounter][0]
elif self.state == 'death':
animation = self.death[self.animationCounter][0]
elif self.state == 'block':
animation = self.block[self.animationCounter][0]
else:
animation = self.idle[self.animationCounter][0]
if self.state == 'walk' and self.startWalkIn:
canvas.create_image(self.walkX, app.height//2
- app.height//5, image=ImageTk.PhotoImage(animation))
else:
canvas.create_image(app.width//2 - app.width//9, app.height//2
- app.height//5, image=ImageTk.PhotoImage(animation))
def timerFired(self, app):
if self.hp <= 0:
if self.state != 'death':
self.animationCounter = 0
self.state = 'death'
# Define behaviors depending on state
if self.state == 'down':
self.combatTuple = createCombatTuple(self.down[self.animationCounter])
self.animationCounter = (1 + self.animationCounter)
if self.animationCounter >= len(self.down):
self.animationCounter = 0
self.state = 'idle'
elif self.state == 'side':
self.combatTuple = createCombatTuple(self.side[self.animationCounter])
self.animationCounter = (1 + self.animationCounter)
if self.animationCounter >= len(self.side):
self.animationCounter = 0
self.state = 'idle'
elif self.state == 'walk' and self.startWalkIn:
self.combatTuple = createCombatTuple(self.walk[self.animationCounter])
self.animationCounter = ((1 + self.animationCounter) %
len(self.walk))
self.walkX += 10
if self.walkX >= app.width//2 - app.width//9 - 20:
self.startWalkIn = False
self.animationCounter = 0
self.state = 'idle'
elif self.state == 'walk' and self.startWalkIn != True:
self.combatTuple = createCombatTuple(self.walk[self.animationCounter])
if self.animationCounter == len(self.walk) - 1:
self.walkCycles += 1
self.animationCounter = ((1 + self.animationCounter) %
len(self.walk))
if self.walkCycles >= 5:
self.walkCycles = 0
self.state = 'idle'
elif self.state == 'hit':
self.combatTuple = createCombatTuple(self.hit[self.animationCounter])
self.animationCounter = (1 + self.animationCounter)
if self.animationCounter >= len(self.hit):
self.animationCounter = 0
self.state = 'idle'
elif self.state == 'block':
self.combatTuple = createCombatTuple(self.block[self.animationCounter])
self.animationCounter = (1 + self.animationCounter)
if self.animationCounter >= len(self.block):
self.animationCounter = 0
self.state = 'idle'
elif self.state == 'death':
self.combatTuple = createCombatTuple(self.death[self.animationCounter])
if self.animationCounter < len(self.death) - 1:
self.animationCounter += 1
self.timeAfterDeath += 1
else:
self.timeAfterDeath += 1
if self.timeAfterDeath > 30:
self.callGameOver = True
else: # idle animation
self.combatTuple = createCombatTuple(self.idle[self.animationCounter])
self.animationCounter = ((1 + self.animationCounter) %
len(self.idle))
# https://www.cs.cmu.edu/~112/notes/notes-animations-part4.html#events
# Part 3. Events
def keyPressed(self, app, event):
if event.key == 's' and self.state == 'idle':
self.downAudio.start(1)
self.state = 'down'
self.animationCounter = 0
elif event.key == 'd' and self.state == 'idle':
self.sideAudio.start(1)
self.state = 'side'
self.animationCounter = 0
elif (event.key == 'a' and self.state == 'idle'):
self.blockAudio.start(1)
self.state = 'block'
self.animationCounter = 0
# Block auto releases after key pressed on Macbook Pro 16 (M1), but works on
# Macbook Pro 16 non M1