def update_bullets(ai_settings: Settings, stats: GameStats,
                   screen: pygame.Surface, sb: ScoreBoard, ship: Ship,
                   aliens: pygame.sprite.Group,
                   bullets: pygame.sprite.Group) -> NoReturn:
    """更新子弹的位置,并删除已消失的子弹"""
    # 更新子弹的位置
    bullets.update()

    # 删除已消失的子弹
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

    check_bullet_alien_collisions(ai_settings, stats, sb, aliens, bullets,
                                  screen, ship)
 def update(self, gold: pygame.sprite.Group) -> None:
     """Update the spinning animation and possibly jump."""
     # Update the disappear timer and remove from group if time.
     self._disappear_timer += 1
     if self._disappear_timer % self._disappear_time == 0:
         gold.remove(self)
     else:
         # Update the spinning animation.
         self._rotation_timer += 1
         if self._rotation_timer % 3 == 0:
             self._current_image += 1
             self.image = self._images[self._current_image % len(self._images)]
             loc: tuple = self.rect.topleft
             self.rect = self.image.get_rect()
             self.rect.topleft = loc
 def update(self, asteroids: pygame.sprite.Group, player: rocket.rocket,
            dt: float, bullets: list):
     if self.status == ASTEROID_STATUS.ALIVE:
         """
         Calculates acceleration
         """
         inp = [None] * self.network_inp_count
         # inp[0] = self.velocity[0]
         # inp[1] = self.velocity[1]
         inp[0] = player.position[0] - self.position[0]
         inp[1] = player.position[1] - self.position[1]
         out = self.network.calculate(np.array(inp))
         """
         Updates the velocity and position
         """
         dx = player.position[0] - self.position[0]
         dy = player.position[1] - self.position[1]
         dist_sq = dx**2 + dy**2
         self.velocity[0] += out[0] + 0.001 * dx * asteroid_accel_coeff * dt
         self.velocity[1] += out[1] + 0.001 * dy * asteroid_accel_coeff * dt
         self.position[0] += self.velocity[0] * asteroid_vel_coeff * dt
         self.position[1] += self.velocity[1] * asteroid_vel_coeff * dt
         self.rect.center = (int(self.position[0]), int(self.position[1]))
         """
         Calculates the average square root of 
         the distance between asteroid and the player
         """
         if dist_sq < self.least_dist_sq or self.least_dist_sq < 0:
             self.least_dist_sq = dist_sq
         """
         Checks if asteroid has collided with another
         asteroid or bullet
         """
         asteroids.remove(self)
         for asteroid in asteroids:
             dx = asteroid.position[0] - self.position[0]
             dy = asteroid.position[1] - self.position[1]
             if dx**2 + dy**2 <= 4 * (asteroid.radius + self.radius)**2:
                 self.die()
                 asteroid.die()
                 asteroids.add(self)
                 return
         asteroids.add(self)
         for bult in bullets:
             if self.rect.collidepoint(bult.position[0], bult.position[1]):
                 bullets.remove(bult)
                 self.destroyed_by_player = True
                 self.die()
                 return
         if (pygame.time.get_ticks() - self.last_avg_dist_sample_tick) /\
            1000 >= 0.01:
             dx = self.position[0] - target_position[0]
             dy = self.position[1] - target_position[1]
             if self.avg_dist_sqrt < 0:
                 self.avg_dist_sqrt
             total = self.avg_dist_sqrt * \
                 self.avg_dist_samples + (dx**2 + dy**2)**0.5
             self.avg_dist_samples += 1
             self.avg_dist_sqrt = total / self.avg_dist_samples
             self.last_avg_dist_sample_tick = pygame.time.get_ticks()
         if self.position[0] < 0-2*self.radius or\
            self.position[0] > window_width+2*self.radius or\
            self.position[1] < 0-2*self.radius or\
            self.position[1] > window_height+2*self.radius:
             self.die()
             return
Beispiel #4
0
 def _invisible_sprites(self, group: pygame.sprite.Group,
                        sprites: List[Sprite]):
     """groupからspritesを削除
     """
     group.remove(sprites)