def move_ai(self, player): self.state.move_ai(self, player) # Comprobamos que el enemigo esté golpeando al jugador if pygame.sprite.collide_mask(player, self): angle = math.radians(360-EnemyRange.get_angle(self.movement)) impulse = Force(angle, self.stats["backward"]) player.receive_damage(self.stats["atk"], impulse)
def update(self, player, time, stage): # Si ha pasado el tiempo suficiente y estamos intentando atacar if (self.elapsedTime >= self.delayTime) and self.attacking: #Se llama al channel_effect self.channel_effect.soundUpdate(time) self.drawAnimation = True # Y reiniciar el contador self.elapsedTime = 0 # Si tenemos nivel suficiente if (self.level > 2 and random.random() <= self.probLvl3): thunder = Thunder(self.characterPos, self.rotation, self.radius + 30, self.enemies) self.thunders.add(thunder) else: self.elapsedTime += time Attack.update(self, time) if self.drawAnimation: if self.rotation >= 90: self.image = pygame.transform.rotate(self.origImage, 180 - self.rotation) self.image = pygame.transform.flip(self.image, True, False) elif self.rotation <= -90: self.image = pygame.transform.rotate(self.origImage, -180 - self.rotation) self.image = pygame.transform.flip(self.image, True, False) else: self.image = pygame.transform.rotate(self.origImage, self.rotation) # Colisiones for enemy in self.enemies: (atkX, atkY) = self.position (enemyX, enemyY) = enemy.position # atkY -= self.image.get_height() enemyY -= enemy.image.get_height() offset = (int(enemyX - atkX), int(enemyY - atkY)) self.mask = pygame.mask.from_surface(self.image) collision = self.mask.overlap(enemy.mask, offset) if collision is not None: # Comprobamos si aun no hemos dañado al enemigo value = self.attackDict.get(id(enemy)) if (value is None or value != self.id): self.attackDict[id(enemy)] = self.id enemyPos = enemy.rect.center impulse = Force(self.rotation, player.stats["backward"]) enemy.receive_damage('physic', player.stats["atk"], impulse) if (self.level > 1 and enemy.justDied and random.random() <= self.probLvl2): explosion = Explosion(enemyPos, self.enemies) self.explosions.add(explosion) del self.attackDict[id(enemy)] self.thunders.update(player, time, stage) self.explosions.update(player, time, stage)
def update(self, boss, time, stage): # Actualizamos el ataque Attack.update(self, time) if not self.castingAnim.animationFinish: self.castingAnim.update(time) else: # Si ha pasado el tiempo suficiente y estamos intentando atacar if (self.elapsedTime > self.delayTime) and self.attacking: #Se llama a la funcion sound_update del channel_effect self.channel_effect.soundUpdate(time) # Se crea una bala y se guarda en el grupo de balas bullet = Bullet(self.bulletPos, self.rotation, self.radius, self.bulletImage, 0.15) self.bullets.add(bullet) # Y reiniciar el contador self.elapsedTime = 0 else: self.elapsedTime += time # Actualizamos las balas self.bullets.update(time, stage, self.bulletImage) # Comprobamos que enemigos colisionan con que grupos collides = pygame.sprite.groupcollide(self.bullets, self.enemies, True, False) # Si hay una colisión, hacemos daño al enemigo y matamos la bala for bullet in collides: enemies = collides[bullet] # Cogemos el primero en hacer la colisión para que reciba daño enemy = enemies[0] enemyPos = enemy.position impulse = Force(bullet.rotation, boss.stats["backward"]) enemy.receive_damage(boss.stats["atk"], impulse) if not self.attacking and len(self.bullets.sprites()) == 0: boss.attack = None self.kill()
def update(self, boss, time, stage): # Actualizamos el ataque Attack.update(self, time) # Colisiones for enemy in iter(self.enemies): (atkX, atkY) = self.rect.topleft (enemyX, enemyY) = enemy.position # print(self.rect.topleft, enemy.position) # atkY -= self.image.get_height() enemyY -= enemy.image.get_height() offset = (int(enemyX - atkX), int(enemyY - atkY)) if self.looking == E: image = pygame.transform.flip(self.image, 1, 0) else: image = self.image self.mask = pygame.mask.from_surface(image) collision = self.mask.overlap(enemy.mask, offset) if collision is not None: impulse = Force(self.angle, boss.stats["backward"]) enemy.receive_damage(boss.stats["atk"], impulse) # # Comprobamos que enemigos colisionan con que grupos # collides = pygame.sprite.groupcollide(self.bullets, self.enemies, True, False) # # Si hay una colisión, hacemos daño al enemigo y matamos la bala # for bullet in collides: # enemies = collides[bullet] # # Cogemos el primero en hacer la colisión para que reciba daño # enemy = enemies[0] # enemyPos = enemy.position # impulse = Force(bullet.rotation, boss.stats["backward"]) # enemy.receive_damage(boss.stats["atk"], impulse) # if not self.attacking and len(self.bullets.sprites())==0: # print("killing attck") # boss.attack = None # self.kill() if not self.attacking: boss.attack = None self.kill() # TODO: creo que no hace falta
def update(self, player, time, stage): self.image = pygame.transform.scale( self.origImage, (int(self.rect.width * 2), int(self.rect.height * 2))) Attack.update(self, time) # Colisiones if self.animationFrame > 3: self.channel_effect.soundUpdate(time) for enemy in self.enemies: if self.rect.colliderect(enemy.rect): value = self.attackDict.get(id(enemy)) if (value is None or value != self.id): self.attackDict[id(enemy)] = self.id angle = random.uniform(0, 2 * math.pi) impulse = Force(angle, player.stats["backward"]) enemy.receive_damage('physic', player.stats["atk"], impulse) if (enemy.killed): del self.attackDict[id(enemy)] if not self.drawAnimation: self.kill()
def update(self, player, time, stage): Attack.update(self, time) self.image = pygame.transform.rotate(self.origImage, 90) # Colisiones if self.animationFrame > 3: self.channel_effect.soundUpdate(time) blastRect = Rect(0, 0, self.rect.height - self.shrink * 2, self.rect.height - self.shrink * 2) blastRect.left = self.rect.left + self.shrink blastRect.bottom = self.rect.bottom + self.rect.width - self.rect.height - self.shrink # self.blastRect = blastRect for enemy in self.enemies: if blastRect.colliderect(enemy.rect): value = self.attackDict.get(id(enemy)) if (value is None or value != self.id): self.attackDict[id(enemy)] = self.id angle = random.uniform(0, 2 * math.pi) impulse = Force(angle, player.stats["backward"]) enemy.receive_damage('magic', player.stats["atk"], impulse) if (enemy.killed): del self.attackDict[id(enemy)] if not self.drawAnimation: self.kill()
def update(self, player, time, stage): # Actualizamos el ataque Attack.update(self, time) # Si ha pasado el tiempo suficiente y estamos intentando atacar if (self.elapsedTime >= self.delayTime) and self.attacking: #Se llama a la funcion sound_update del channel_effect self.channel_effect.soundUpdate(time) # Se crea una bala y se guarda en el grupo de balas bullet = Bullet(self.characterPos, self.rotation, self.radius, self.image) self.bullets.add(bullet) # Si tenemos nivel suficiente, se pueden lanzar dos extra if (self.level > 2 and random.random() <= self.probLvl3): rot2 = normalize(self.rotation + 15, -180, 180) rot3 = normalize(self.rotation - 15, -180, 180) bullet2 = Bullet(self.characterPos, rot2, self.radius, self.image) bullet3 = Bullet(self.characterPos, rot3, self.radius, self.image) self.bullets.add(bullet2) self.bullets.add(bullet3) # Y reiniciar el contador self.elapsedTime = 0 else: self.elapsedTime += time # Actualizamos las balas self.bullets.update(time, stage, self.image) # Comprobamos que enemigos colisionan con que grupos # collides = pygame.sprite.groupcollide(self.bullets, self.enemies, True, False) # # Si hay una colisión, hacemos daño al enemigo y matamos la bala # for bullet in collides: # enemies = collides[bullet] # # Cogemos el primero en hacer la colisión para que reciba daño # enemy = enemies[0] # enemyPos = enemy.position # impulse = Force(bullet.rotation, player.stats["backward"]) # enemy.receive_damage('magic', player.stats["atk"], impulse) # if (self.level>1 and enemy.justDied and random.random()<=self.probLvl2): # # enemy.kill() # angle = random.uniform(-180,180) # bulletExtra = Bullet(enemyPos, angle, self.radius, self.image) # self.bullets.add(bulletExtra) for bullet in iter(self.bullets): for enemy in iter(self.enemies): (atkX, atkY) = bullet.position (enemyX, enemyY) = enemy.position # atkY -= self.image.get_height() enemyY -= enemy.image.get_height() / 2 offset = (int(enemyX - atkX), int(enemyY - atkY)) self.mask = pygame.mask.from_surface(self.image) collision = self.mask.overlap(enemy.mask, offset) if collision is not None: impulse = Force(bullet.rotation, player.stats["backward"]) enemy.receive_damage('magic', player.stats["atk"], impulse) bullet.kill() if (self.level > 1 and enemy.justDied and random.random() <= self.probLvl2): # enemy.kill() angle = random.uniform(-180, 180) bulletExtra = Bullet(enemy.position, angle, self.radius, self.image) self.bullets.add(bulletExtra)