-
Notifications
You must be signed in to change notification settings - Fork 0
/
chameleon.py
194 lines (172 loc) · 7.64 KB
/
chameleon.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
import pygame
from animation import Animation
from entity import Entity
from player import Player
from staticenemy import DynamicEnemy, DoctorMan, StaticEnemy
class Chameleon(Entity):
TONGUE_TIME = 1500
TONGUE_DELAY = 500
TONGUE_DURATION = 1000
LASER_TIME = 0
LASER_DELAY = 500
LASER2_DELAY = 100
LASER_DURATION = 1000
STATE_IDLE = 0
STATE_TONGUE = 1
STATE_LASERS = 2
OFFSET_TONGUE = pygame.math.Vector2(4.5 * 26, 0)
OFFSET_LASER = pygame.math.Vector2(5 * 26, 0)
OFFSET_DOCTOR = pygame.math.Vector2(1 * 26, -1.5 * 26)
ANIM_TONGUE = Animation([
pygame.image.load("mock/nanomeleon-tongue-1.png"),
pygame.image.load("mock/nanomeleon-tongue-2.png"),
pygame.image.load("mock/nanomeleon-tongue-2.png"),
pygame.image.load("mock/nanomeleon-tongue-2.png"),
pygame.image.load("mock/nanomeleon-tongue-3.png"),
],
(TONGUE_DELAY + TONGUE_DURATION) / 5)
ANIM_LASER = Animation([
pygame.image.load("mock/nanomeleon-laser-1.png"),
pygame.image.load("mock/nanomeleon-laser-2.png"),
pygame.image.load("mock/nanomeleon-laser-2.png"),
pygame.image.load("mock/nanomeleon-laser-2.png"),
pygame.image.load("mock/nanomeleon-laser-3.png"),
],
(LASER_DELAY + LASER2_DELAY + LASER_DURATION) / 5)
ANIM_IDLE = Animation([
pygame.image.load("mock/nanomeleon-walk-1.png"),
pygame.image.load("mock/nanomeleon-walk-2.png"),
pygame.image.load("mock/nanomeleon-walk-3.png"),
pygame.image.load("mock/nanomeleon-walk-4.png"),
pygame.image.load("mock/nanomeleon-walk-5.png"),
pygame.image.load("mock/nanomeleon-walk-6.png"),
pygame.image.load("mock/nanomeleon-walk-7.png"),
],
100)
ANIM_OBJ_TONGUE = Animation([
pygame.image.load("mock/tongue-1.png"),
pygame.image.load("mock/tongue-2.png"),
pygame.image.load("mock/tongue-3.png"),
pygame.image.load("mock/tongue-4.png"),
pygame.image.load("mock/tongue-4.png"),
pygame.image.load("mock/tongue-4.png"),
pygame.image.load("mock/tongue-4.png"),
pygame.image.load("mock/tongue-4.png"),
],
TONGUE_DURATION / 8)
VEL_LEFTRIGHT = 100
DIST_LEFTRIGHT = 26 * 3
def __init__(self, image_path, position):
self.type = Entity.TYPE_CHAMELEON
self.animation = Animation([
pygame.image.load(image_path),
],
0)
self.position = position
self.start = pygame.math.Vector2(position.x, position.y)
self.velocity = pygame.math.Vector2(Chameleon.VEL_LEFTRIGHT, 0)
self.damage = 1
self.reset_collider()
self.tongue = None
self.laser = None
self.laser2 = None
self.jumped = False
self.dead = False
self.state = Chameleon.STATE_IDLE
self.state_time = 0
self.doctor = DoctorMan(self.position + Chameleon.OFFSET_DOCTOR)
def collide(self, other, axis, dt):
if other.type == Entity.TYPE_PLAYER or other.type == Entity.TYPE_OBJECT:
other.take_damage(self.damage, self)
self.resolve_collision(other, axis, dt)
def update_logic(self, input_m, entities, dt):
if self.doctor.dead:
self.velocity = pygame.math.Vector2()
self.animation = Chameleon.ANIM_IDLE
return
if not self.doctor in entities:
entities.insert(0, self.doctor)
self.state_time += dt
if self.state == Chameleon.STATE_IDLE:
self.update_idle(dt)
elif self.state == Chameleon.STATE_TONGUE:
self.update_tongue(dt, entities)
else:
self.update_laser(dt, entities)
self.update_doctor()
def update_idle(self, dt):
if self.position.x - self.start.x > Chameleon.DIST_LEFTRIGHT:
self.velocity.x = -Chameleon.VEL_LEFTRIGHT
elif self.position.x - self.start.x < -Chameleon.DIST_LEFTRIGHT:
self.velocity.x = Chameleon.VEL_LEFTRIGHT
if self.animation != Chameleon.ANIM_IDLE:
self.animation = Chameleon.ANIM_IDLE
self.animation.frame_number = 0
self.animation.current_time = 0
if self.state_time > Chameleon.TONGUE_TIME:
self.state = Chameleon.STATE_TONGUE
self.state_time = 0
def update_tongue(self, dt, entities):
if self.animation != Chameleon.ANIM_TONGUE:
self.animation = Chameleon.ANIM_TONGUE
self.animation.frame_number = 0
self.animation.current_time = 0
if self.tongue:
self.tongue.position = self.position + Chameleon.OFFSET_TONGUE
if self.state_time > Chameleon.TONGUE_DELAY and not self.tongue:
Chameleon.ANIM_OBJ_TONGUE.frame_number = 0
Chameleon.ANIM_OBJ_TONGUE.current_time = 0
self.tongue = DynamicEnemy("mock/chameleon-tongue.png",
self.position + Chameleon.OFFSET_TONGUE,
pygame.math.Vector2(),
3,
Chameleon.ANIM_OBJ_TONGUE)
print("tongue is a " + str(type(self.tongue)) + " with damage " + str(self.tongue.damage))
entities.append(self.tongue)
elif self.state_time > Chameleon.TONGUE_DELAY + Chameleon.TONGUE_DURATION:
self.state_time = 0
self.state = Chameleon.STATE_LASERS
if self.tongue in entities:
entities.remove(self.tongue)
self.tongue = None
def update_laser(self, dt, entities):
if self.animation != Chameleon.ANIM_LASER:
self.animation = Chameleon.ANIM_LASER
self.animation.frame_number = 0
self.animation.current_time = 0
if self.state_time > Chameleon.LASER_DELAY and not self.jumped:
self.jump()
self.jumped = True
if self.state_time > Chameleon.LASER_DELAY and not self.laser:
self.laser = DynamicEnemy("mock/chameleon-laser.png",
self.position + Chameleon.OFFSET_LASER,
pygame.math.Vector2(Player.VEL_BULLET, 0),
3)
entities.append(self.laser)
if self.state_time > Chameleon.LASER_DELAY + Chameleon.LASER2_DELAY \
and not self.laser2:
self.laser2 = DynamicEnemy("mock/chameleon-laser.png",
self.position + Chameleon.OFFSET_LASER,
pygame.math.Vector2(Player.VEL_BULLET, 0),
3)
entities.append(self.laser2)
elif self.state_time > Chameleon.LASER_DELAY \
+ Chameleon.LASER_DURATION \
+ Chameleon.LASER2_DELAY:
if self.laser in entities:
entities.remove(self.laser)
if self.laser2 in entities:
entities.remove(self.laser2)
self.laser = None
self.laser2 = None
self.jumped = False
self.state_time = 0
self.state = Chameleon.STATE_IDLE
def jump(self):
self.velocity = pygame.math.Vector2(0, -Player.VEL_JUMP)
def update_doctor(self):
self.doctor.position = self.position + Chameleon.OFFSET_DOCTOR
print("chameleon now at (" + str(self.position.x) + ", "
+ str(self.position.y) + ")")
print("doctor now at (" + str(self.doctor.position.x)
+ ", " + str(self.doctor.position.y) + ")")