def shoot(self): """Shoots bullets at the player""" # do nothing if there is less than 60 frames left if self.frames_left() < 60: return to_player = self.to_player_vector() # Shoot a 60px bullet at the player every 4 frames if self.frame % 4 == 0: LinearBullet(containers=self.containers, image=self.enemy.bullet_image_60px, start=self.enemy.vec, damage=50, velocity=to_player * self.BULLET_SPEED) # Shoot two 30px bullets near the player every 2 frames if self.frame % 2 == 0: max_rot = np.pi / 2 rand_theta = random.random() * 2 * max_rot - max_rot to_player_rand = rot_matrix(rand_theta) @ to_player LinearBullet(containers=self.containers, image=self.enemy.bullet_image_30px, start=self.enemy.vec, damage=40, velocity=to_player_rand * self.BULLET_SPEED) max_rot = np.pi / 4 rand_theta = random.random() * 2 * max_rot - max_rot to_player_rand = rot_matrix(rand_theta) @ to_player LinearBullet(containers=self.containers, image=self.enemy.bullet_image_30px, start=self.enemy.vec, damage=40, velocity=to_player_rand * self.BULLET_SPEED)
def shoot(self): """Shoots bombs towards the bottom of the screen""" if self.frame < 120 and self.frame % 6 == 0: # should fire 20 bombs in 2 seconds, then do nothing down = np.array([0, 1]) max_rot = np.pi / 6 rand_theta = random.random() * 2 * max_rot - max_rot direction_rand = rot_matrix(rand_theta) @ down BombBullet(containers=self.containers, image=self.enemy.bomb_image_120px, start=self.enemy.vec, damage=50, velocity=direction_rand * self.BULLET_SPEED, on_explode=self.bomb_explode)
def bomb_explode(self, start): """The function to be called when the bomb explodes""" down = np.array([0, 1]) max_rot = np.pi rand_theta = random.random() * 2 * max_rot - max_rot num_bullets = 12 for i in range(num_bullets): direction = rot_matrix(rand_theta + 2 * np.pi / num_bullets * i) @ down LinearBullet(containers=self.containers, image=self.enemy.bullet_image_60px, start=start + direction * 30, damage=50, velocity=direction * self.BULLET_SPEED)
def shoot(self): """Shoots the bouncy bullets""" if self.frame < 60 and self.frame % 4 == 0: # Should shoot 15 bouncy bullets in 1 second, then stop doing anything down = np.array([0, 1]) max_rot = np.pi rand_theta = random.random() * 2 * max_rot - max_rot direction_rand = rot_matrix(rand_theta) @ down BouncingBullet(containers=self.containers, image=self.enemy.bullet_image_120px, start=self.enemy.vec, damage=70, velocity=direction_rand * self.BULLET_SPEED, max_bounce=self.total_frames - 120)
def test_rot_matrix(self): rot = rot_matrix(np.pi / 2) self.assertAlmostEqual(0, rot[0, 0]) self.assertAlmostEqual(-1, rot[0, 1]) self.assertAlmostEqual(1, rot[1, 0]) self.assertAlmostEqual(0, rot[1, 1])
def test_rot_matrix_identity(self): self.assertEqual(np.identity(2).tolist(), rot_matrix(0).tolist())