forked from MasterCash/donkey-kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luigi.py
247 lines (203 loc) · 8.24 KB
/
luigi.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
"""
Class to control luigi
"""
from spriteManager import SpriteManager
from framework import PlayableWithLives, Clock, SpriteSheet, Keys, Sound, Music
from inputManager import InputManager
from enum import Enum
from collisionDetector import CollisionTypes, CollisionDirection
class PlayerState(Enum):
IDLE = 0
LADDER_DOWN = 1
MOVELEFT = 2
MOVERIGHT = 3
DEAD = 5
ERROR = 6
LADDER_IDLE = 7
LADDER_UP = 8
class PlayerSubState(Enum):
NONE = 0
ON_GROUND = 10
ON_GOO = 1
JUMPING = 2
goo_speed = 50
player_speed = 100.0
jump_height = 15
jump_speed = 300 # Actually 100
class Luigi(PlayableWithLives):
def __init__(self):
super().__init__()
self._sheet = SpriteSheet('luigi')
self._speed = player_speed
self._jumpCount = jump_height
self._sprites = {
'stand_left': self._sheet.sprite(0, 20, 24, 32),
'stand_right': self._sheet.sprite(0, 20, 24, 32).flip(),
'run_left1': self._sheet.sprite(45, 20, 31, 32),
'run_left2': self._sheet.sprite(94, 21, 30, 32),
'run_right1': self._sheet.sprite(45, 20, 31, 32).flip(),
'run_right2': self._sheet.sprite(94, 21, 30, 32).flip(),
'ladder_up1': self._sheet.sprite(142, 20, 28, 32),
'ladder_up2': self._sheet.sprite(142, 20, 28, 32).flip(),
'death1': self._sheet.sprite(716, 20, 32, 32),
'death2': self._sheet.sprite(764, 20, 32, 32),
'death3': self._sheet.sprite(764, 20, 32, 32).rotate(90),
'death4': self._sheet.sprite(764, 20, 32, 32).flip(),
'death5': self._sheet.sprite(764, 20, 32, 32).rotate(-90),
'death6': self._sheet.sprite(812, 20, 32, 32),
'life': self._sheet.sprite(1629, 31, 14, 16).flip()
}
self.spriteManager = SpriteManager(self._sprites)
self.spriteManager.useSprites([
'stand_right'
], 10)
self.ticks = 0
InputManager.subscribe(
[Keys.A, Keys.D, Keys.S, Keys.W, Keys.Q, Keys.E],
self._luigiKeyPress
)
self._walkingSound = Sound('15_SFX_Walking')
def update(self):
""" Method used for updating state of a sprite/object """
if self._isAtLadder != True and self.subState != PlayerSubState.JUMPING:
self.y = self.y + (self._speed * 2) * Clock.timeDelta # Gravity
self._isAtLadder = False
if self.subState == PlayerSubState.ON_GOO:
self._speed = goo_speed
self.subState = PlayerSubState.NONE
else:
self._speed = player_speed
if self.subState == PlayerSubState.JUMPING:
if self._jumpCount >= -jump_height:
neg = 1
if self._jumpCount < 0:
neg = -1
self.y = self.y - (self._jumpCount ** 2) * 0.025 * neg
self._jumpCount = self._jumpCount - 1
else:
self.subState = PlayerSubState.NONE
self._jumpCount = jump_height
self._isOnGround = False
else:
self._jumpCount = jump_height
if self.state == PlayerState.MOVELEFT:
self.x -= self._speed * Clock.timeDelta
self.state = PlayerState.IDLE
self.spriteManager.useSprites([
'run_left1',
'stand_left',
'run_left2'
], 8)
self._walkingSound.stop()
self._walkingSound.play()
elif self.state == PlayerState.MOVERIGHT:
self.x += self._speed * Clock.timeDelta
self.state = PlayerState.IDLE
self.spriteManager.useSprites([
'run_right1',
'stand_right',
'run_right2'
], 8)
self._walkingSound.stop()
self._walkingSound.play()
elif self.state == PlayerState.LADDER_DOWN:
self.y += self._speed * Clock.timeDelta
self.state = PlayerState.LADDER_IDLE
self.spriteManager.useSprites([
'ladder_up1',
'ladder_up2'
], 10)
elif self.state == PlayerState.LADDER_UP:
self.y -= self._speed * Clock.timeDelta
self.state = PlayerState.LADDER_IDLE
self.spriteManager.useSprites([
'ladder_up1',
'ladder_up2'
], 10)
elif self.state == PlayerState.LADDER_IDLE:
self.spriteManager.useSprites([
'ladder_up1'
], 10)
else:
self.state = PlayerState.IDLE
if 'stand_left' in self.spriteManager.currentAnimation:
self.spriteManager.useSprites(['stand_left'], 10)
else:
self.spriteManager.useSprites(['stand_right'], 10)
self._walkingSound.stop()
def collision(self, collisionType, direction, obj):
""" Luigi collided with something """
if collisionType == CollisionTypes.Enemy:
print("You killed Luigi!!!!!")
self.die()
elif collisionType == CollisionTypes.Ladder:
if not obj.isBroken:
self._isAtLadder = True
elif collisionType == CollisionTypes.Platform:
if self._isAtLadder == False and self.subState != PlayerSubState.JUMPING:
self._isOnGround = True
self.bottom = obj.top + 1
elif collisionType == CollisionTypes.Immovable:
self.state = PlayerState.IDLE
if not obj.isTopOfLadder and not obj.isBroken:
self.bottom = obj.top
elif collisionType == CollisionTypes.Wall:
self.state = PlayerState.IDLE
if obj.isLeftWall:
self.left = obj.right
else:
self.right = obj.left
def collectedItem(self, collectible, collectionType):
""" Luigi collecting something """
if collectible.name == 'Goo' and self.subState != PlayerSubState.JUMPING:
self.subState = PlayerSubState.ON_GOO
def _luigiKeyPress(self, key):
def __str__(self):
return "LuigiKeyPress"
if key == Keys.E:
self.die()
if key == Keys.A and self.state not in (PlayerState.LADDER_IDLE, PlayerState.LADDER_DOWN, PlayerState.LADDER_UP):
self.state = PlayerState.MOVELEFT
elif key == Keys.D and self.state != PlayerState.LADDER_IDLE:
self.state = PlayerState.MOVERIGHT
elif key == Keys.S:
if self._isAtLadder:
self.state = PlayerState.LADDER_DOWN
elif key == Keys.W:
if self._isAtLadder and self.subState != PlayerSubState.JUMPING:
self.state = PlayerState.LADDER_UP
elif key is Keys.Q and self.state not in (PlayerState.LADDER_IDLE, PlayerState.LADDER_DOWN, PlayerState.LADDER_UP):
if self.subState not in (PlayerSubState.JUMPING, PlayerSubState.ON_GOO) and self._isOnGround:
self.subState = PlayerSubState.JUMPING
self._walkingSound.stop()
Music.playEffect("16_SFX_Jump")
def getSprite(self):
""" Returns the current sprite for the game object """
if self.isDying:
self.ticks = self.ticks + 1
if self.ticks == 30:
self.spriteManager.useSprites(['death2', 'death3', 'death4', 'death5'], 10)
elif self.ticks == 100:
self.spriteManager.useSprites(['death6'])
elif self.ticks == 220:
Music.playBackground()
self.respawnIfPossible()
return self.spriteManager.animate()
def onDeath(self):
""" Play the death animation """
Music.playOnTop("20_SFX_Miss")
self.state = PlayerState.DEAD
self.spriteManager.useSprites(['death1'], 10)
self.ticks = 0
self._walkingSound.stop()
def drawExtra(self, screen):
""" Draw the number of lives remaining """
for i in range(self.lives):
screen.draw(self._sprites['life'], 10 + (i * 20), 30)
def onSpawn(self):
self.state = PlayerState.IDLE
self.subState = PlayerSubState.NONE
self._isAtLadder = False
self._isOnGround = True
self._jumpCount = jump_height
self.ticks = 0