-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemies.py
174 lines (140 loc) · 5.19 KB
/
enemies.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
import pygame as pg
from .. import setup
from .. import constants as c
class Enemy(pg.sprite.Sprite):
"""Base class for all enemies (Goombas, Koopas, etc.)"""
def __init__(self):
pg.sprite.Sprite.__init__(self)
def setup_enemy(self, x, y, direction, name, setup_frames):
"""Sets up various values for enemy"""
self.sprite_sheet = setup.GFX['smb_enemies_sheet']
self.frames = []
self.frame_index = 0
self.animate_timer = 0
self.death_timer = 0
self.gravity = 1.5
self.state = c.WALK
self.name = name
self.direction = direction
setup_frames()
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.bottom = y
self.set_velocity()
def set_velocity(self):
"""Sets velocity vector based on direction"""
if self.direction == c.LEFT:
self.x_vel = -2
else:
self.x_vel = 2
self.y_vel = 0
def get_image(self, x, y, width, height):
"""Get the image frames from the sprite sheet"""
image = pg.Surface([width, height]).convert()
rect = image.get_rect()
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
image.set_colorkey(c.BLACK)
image = pg.transform.scale(image,
(int(rect.width * c.SIZE_MULTIPLIER),
int(rect.height * c.SIZE_MULTIPLIER)))
return image
def handle_state(self):
"""Enemy behavior based on state"""
if self.state == c.WALK:
self.walking()
elif self.state == c.FALL:
self.falling()
elif self.state == c.JUMPED_ON:
self.jumped_on()
elif self.state == c.SHELL_SLIDE:
self.shell_sliding()
elif self.state == c.DEATH_JUMP:
self.death_jumping()
def walking(self):
"""Default state of moving sideways"""
if (self.current_time - self.animate_timer) > 125:
if self.frame_index == 0:
self.frame_index += 1
elif self.frame_index == 1:
self.frame_index = 0
self.animate_timer = self.current_time
def falling(self):
"""For when it falls off a ledge"""
if self.y_vel < 10:
self.y_vel += self.gravity
def jumped_on(self):
"""Placeholder for when the enemy is stomped on"""
pass
def death_jumping(self):
"""Death animation"""
self.rect.y += self.y_vel
self.rect.x += self.x_vel
self.y_vel += self.gravity
if self.rect.y > 600:
self.kill()
def start_death_jump(self, direction):
"""Transitions enemy into a DEATH JUMP state"""
self.y_vel = -8
if direction == c.RIGHT:
self.x_vel = 2
else:
self.x_vel = -2
self.gravity = .5
self.frame_index = 3
self.image = self.frames[self.frame_index]
self.state = c.DEATH_JUMP
def animation(self):
"""Basic animation, switching between two frames"""
self.image = self.frames[self.frame_index]
def update(self, game_info, *args):
"""Updates enemy behavior"""
self.current_time = game_info[c.CURRENT_TIME]
self.handle_state()
self.animation()
class Goomba(Enemy):
def __init__(self, y=c.GROUND_HEIGHT, x=0, direction=c.LEFT, name='goomba'):
Enemy.__init__(self)
self.setup_enemy(x, y, direction, name, self.setup_frames)
def setup_frames(self):
"""Put the image frames in a list to be animated"""
self.frames.append(
self.get_image(0, 4, 16, 16))
self.frames.append(
self.get_image(30, 4, 16, 16))
self.frames.append(
self.get_image(61, 0, 16, 16))
self.frames.append(pg.transform.flip(self.frames[1], False, True))
def jumped_on(self):
"""When Mario squishes him"""
self.frame_index = 2
if (self.current_time - self.death_timer) > 500:
self.kill()
class Koopa(Enemy):
def __init__(self, y=c.GROUND_HEIGHT, x=0, direction=c.LEFT, name='koopa'):
Enemy.__init__(self)
self.setup_enemy(x, y, direction, name, self.setup_frames)
def setup_frames(self):
"""Sets frame list"""
self.frames.append(
self.get_image(150, 0, 16, 24))
self.frames.append(
self.get_image(180, 0, 16, 24))
self.frames.append(
self.get_image(360, 5, 16, 15))
self.frames.append(pg.transform.flip(self.frames[2], False, True))
def jumped_on(self):
"""When Mario jumps on the Koopa and puts him in his shell"""
self.x_vel = 0
self.frame_index = 2
shell_y = self.rect.bottom
shell_x = self.rect.x
self.rect = self.frames[self.frame_index].get_rect()
self.rect.x = shell_x
self.rect.bottom = shell_y
def shell_sliding(self):
"""When the koopa is sliding along the ground in his shell"""
if self.direction == c.RIGHT:
self.x_vel = 10
elif self.direction == c.LEFT:
self.x_vel = -10