def create_planet(self, pos, radius): new_planet = Planet(self.evm, pos, radius) for planet in self.planets: if collide_circle(planet, new_planet): return self.planets.append(new_planet) self.evm.post(ev.PlanetCreated(new_planet))
def do_damage(self, monsters): for monster in monsters: if sprite.collide_circle(self, monster): dmg_result = monster.damage(self.damage) if dmg_result[0] is not None: self.tower.kills += 1 self.tower.turn_yield += dmg_result[0] self.tower.damage_done += dmg_result[1]
def update(self, player: PlayerSprite, enemies: Group, screen: Surface): if (self._data.health <= 0): self.kill() enemies.remove(self) try: for enemy in enemies: if sprite.collide_circle(self, enemy) and enemy != self: distance = math.hypot((enemy.rect.x - self.rect.x), (enemy.rect.y - self.rect.y)) if (distance < 400): target_direction = Vector2( (self.rect.x - enemy.rect.x), (self.rect.y - enemy.rect.y)) target_direction.scale_to_length(self._data.vel * 0.001) self.rect.x += target_direction.x self.rect.y += target_direction.y # Delete enemy when it comes into contact with player if sprite.collide_mask( player, self) is not None and not player.invulnerable: player.take_damage(1) self.kill() enemies.remove(self) # Type 2 enemy specification # Auto fire towards player at a given rate n = time.get_ticks() if (self._charging) <= 1000: self._charging = n - self._start self._pstart = time.get_ticks() target_direction = Vector2(-self.rect.x + player.rect.x, -self.rect.y + player.rect.y) target_direction.scale_to_length(self._data.vel * 2) self.rect.x += target_direction.x self.rect.y += target_direction.y elif (self._charging > 1000): self._pausing = time.get_ticks() - self._pstart if (self._pausing) > 550: self._charging = 0 self._pausing = 0 self._start = time.get_ticks() screen_rect = screen.get_rect() self.rect.clamp_ip(screen_rect) self._data.pos = Vector2(self.rect.center) self._calc_rotation(player) except ValueError: return
def _avoid_player(self, player: PlayerSprite, target_direction: Vector2): # Stop moving towards player at a certain distance if sprite.collide_circle(self, player): distance = self._data.pos.distance_to(player._data.pos) # Move back if in danger zone if distance < self.radius - 100: return self._data.pos - player._data.pos else: return Vector2(0, 0) else: return target_direction
def update(self, monsters, screen, screen_rect): # If there is no target if self.target is None: for monster in monsters: if monster.can_move: if sprite.collide_circle(monster, self): self.target = monster break # Shoot at the target if self.target is not None: # If the target has gone out of range if not sprite.collide_circle(self.target, self): self.target = None else: self.shoot() if self.target.health <= 0: self.target = None self.projectiles.update(monsters, screen_rect) self.projectiles.draw(screen)
def update(self, monsters, screen, screen_rect): self.target = [] # Gets nearby targets for monster in monsters: if len(self.target) == 5: break elif sprite.collide_circle(monster, self): self.target.append(monster) if len(self.target) > 0: self.shoot() self.projectiles.update(monsters, screen_rect) self.projectiles.draw(screen)
def update(self, player: PlayerSprite, enemies: Group, screen: Surface): if (self._data.health <= 0): self.kill() enemies.remove(self) rand_pos_x = random.randint(40, WINDOW_WIDTH / 2) rand_pos_y = random.randint(40, WINDOW_HEIGHT / 2) for enemy in enemies: if sprite.collide_circle(self, enemy) and enemy != self: distance = math.hypot((enemy.rect.x - self.rect.x), (enemy.rect.y - self.rect.y)) if (distance < 400): target_direction = Vector2((self.rect.x - enemy.rect.x), (self.rect.y - enemy.rect.y)) target_direction.scale_to_length(self._data.vel * 0.001) self.rect.x += target_direction.x self.rect.y += target_direction.y # Delete enemy when it comes into contact with player if sprite.collide_mask(player, self) is not None and not player.invulnerable: player.take_damage(1) self.kill() enemies.remove(self) n = time.get_ticks() if (n - self.weapon.prev_shot) > 2000 and not self._moved: self.rect.x = rand_pos_x self.rect.y = rand_pos_y self._moved = True elif (n - self.weapon.prev_shot) > 3000 and self._moved: if self._weapon.fire(player, self._data.attack_speed, 15, self.rect): self._moved = False else: self.rect.x = self.rect.x self.rect.y = self.rect.y screen_rect = screen.get_rect() self.rect.clamp_ip(screen_rect) self._data.pos = Vector2(self.rect.center) self._calc_rotation(player) self._weapon.update()
def update(self, player: PlayerSprite, enemies: Group, screen: Surface): if(self._data.health <= 0): self.kill() enemies.remove(self) for enemy in enemies: # Check for nearby enemies to avoid collision if sprite.collide_circle(self, enemy) and enemy != self: distance = math.hypot( (enemy.rect.x - self.rect.x), (enemy.rect.y - self.rect.y)) if (distance < 400): target_direction = Vector2( (self.rect.x - enemy.rect.x), (self.rect.y - enemy.rect.y)) target_direction.scale_to_length( self._data.vel * 0.001) self.rect.x += target_direction.x self.rect.y += target_direction.y target_direction = Vector2( - self.rect.x + player.rect.x + random.randrange(0, 30), - self.rect.y + player.rect.y + random.randrange(0, 30)) target_direction.scale_to_length(self._data.vel * 0.7) # Delete enemy when it comes into contact with player if sprite.collide_mask(player, self) is not None and not player.invulnerable: player.take_damage(1) self.kill() enemies.remove(self) self.rect.x += target_direction.x self.rect.y += target_direction.y screen_rect = screen.get_rect() self.rect.clamp_ip(screen_rect) self._data.pos = Vector2(self.rect.center) self._calc_rotation(player)
def render(self): # Видимость объекта xx, yy = PickUp.camera.get_pos(self.x, self.y, True) if (-self.w * 1.5 < xx < SIZE[0]) and (-self.h * 1.5 < yy < SIZE[1]): self.Visible = True else: self.Visible = False if self.Visible: # Отображение PickUp.screen.blit(self.image, (xx, yy)) # Столкновение if collide_circle(self, PickUp.hero): if self.type == 'heal': PickUp.hero.hp += 15 elif self.type == 'explode': PickUp.explode.append(Explode(self.x, self.y)) elif self.type == 'fire_bullet': if (PickUp.hero.weapon != 'rifle') and (PickUp.hero.weapon != 'flamethrower'): PickUp.hero.fire_bullet = True elif self.type == 'armor': PickUp.hero.armor = 85 elif self.type == 'holder': PickUp.hero.bullet_count0 = PickUp.hero.reload0 = 0 elif self.type == 'rapidfire': PickUp.hero.rapidfire0 += 1 else: if PickUp.hero.weapon != 'launcher': weapons[PickUp.hero.weapon]['sound_r'].stop() PickUp.hero.bullet_count0 = PickUp.hero.reload0 = 0 PickUp.hero.Max_Speed = 2 PickUp.hero.weapon = self.type if self.type == 'm249': PickUp.hero.Max_Speed = 1.5 PickUp.pickup.remove(self)
def update(self): for player in self.players: player.update() for proj in self.projectiles: proj.update() #Bounce off the walls if proj.pos[0] < 0: proj.dx *= -1 proj.ax *= -1 elif proj.pos[0] > self.field_rect.width: proj.dx *= -1 proj.ax *= -1 if proj.pos[1] < 0: proj.dy *= -1 proj.ay *= -1 elif proj.pos[1] > self.field_rect.height: proj.dy *= -1 proj.ay *= -1 '''if self.out_of_bounds(proj): angle = math.atan2((proj.rect.centery- self.field_rect.centery), (proj.rect.centerx- self.field_rect.centerx)) proj.ax -= math.cos(-angle) * 0.002 proj.ay += math.sin(-angle) * 0.002''' for planet in self.planets: #Neutral planets don't gain value, so we don't call update on them if planet.owner: planet.update() ai = planet.owner if planet.owner.is_ai and ai.wait_counter > ai.wait_cycle: ai.process(planet, self.planets) if planet.is_dragged: #Don't allow the player to select planets that aren't his if planet.owner == self.active_player: #Set the planet's text to yellow when it's selected planet.value_color = (255, 255, 0) else: planet.clear_clicked() for proj in self.projectiles: #Ignore projectle if this planet is where the it was shot from. if planet == proj.parent_planet: continue dist = find_distance(planet.rect.center, proj.rect.center) #Ignore projectile if it is too far away if dist > 40000: continue #If dist is 0, set it to something to avoid making a big mistake elif dist == 0: dist = 0.001 m1 = planet.radius m2 = proj.radius force = (m1*1000) / (dist**2) angle = math.atan2((proj.rect.centery - planet.rect.centery), (proj.rect.centerx - planet.rect.centerx)) proj.ax -= math.cos(-angle) * force * 3 proj.ay += math.sin(-angle) * force * 3 if collide_circle(proj, planet): planet.on_projectile_collided(proj) self.remove_projectile(proj) #Right now the view does not pick up this event, so we don't #need to send it '''self.evm.post(ev.ProjectileCollided(proj, planet))'''
def collide_rect_circ(left, right): return 1 if collide_rect(left, right) and collide_circle(left, right) else 0
def update(self, player: PlayerSprite, enemies: Group, screen: Surface): if(self._data.health <= 0): self.kill() enemies.remove(self) # Check for nearby enemies, only move in certain case for enemy in enemies: if sprite.collide_circle(self, enemy) and enemy != self: distance = math.hypot( (enemy.rect.x - self.rect.x), (enemy.rect.y - self.rect.y)) if (distance < 400): target_direction = Vector2( (self.rect.x - enemy.rect.x), (self.rect.y - enemy.rect.y)) target_direction.scale_to_length( self._data.vel * 0.001) self.rect.x += target_direction.x self.rect.y += target_direction.y # Type 2 enemy backs away from player distance = math.hypot( (player.rect.x - self.rect.x), (player.rect.y - self.rect.y)) if (distance > 550): self._data._stopMoving = False # Enemy moves toward player given that they are either type 1 or sufficiently far enough from player if not self._data._stopMoving: target_direction = Vector2( - self.rect.x + player.rect.x + random.randrange(0, 30), - self.rect.y + player.rect.y + random.randrange(0, 30)) target_direction.scale_to_length(self._data.vel * 0.9) # Delete enemy when it comes into contact with player if sprite.collide_mask(player, self) is not None and not player.invulnerable: player.take_damage(1) self.kill() enemies.remove(self) # Stop moving towards player at a certain distance if sprite.collide_circle(self, player): self._data._stopMoving = True distance = math.hypot( (player.rect.x-self.rect.x), (player.rect.y - self.rect.y)) # Move back if in danger zone if(distance < 550): target_direction = Vector2( (self.rect.x - player.rect.x), (self.rect.y - player.rect.y)) target_direction.scale_to_length(self._data.vel * 1.01) self.rect.x += target_direction.x self.rect.y += target_direction.y # All other cases are given movement data here if self._data._stopMoving is False: self.rect.x += target_direction.x self.rect.y += target_direction.y screen_rect = screen.get_rect() self.rect.clamp_ip(screen_rect) self._data.pos = Vector2(self.rect.center) self._calc_rotation(player) self._weapon.fire(player, self._data.attack_speed, 15, self.rect, 550) self._weapon.update(player, screen)
def effect(self, target, monsters): for monster in monsters: if sprite.collide_circle(target, monster): monster.damage(self.damage) self.kill()
def fish_collision(sprite1, sprite2): """Algorithm for determining if there is a collision between the sprites.""" if sprite1 == sprite2: return False else: return collide_circle(sprite1, sprite2)
def update(self, monsters, screen, screen_rect): for monster in monsters: if sprite.collide_circle(self, monster): monster.speed_mod = self.speed_mod
def find_collision(animal, animal_group): """Return the first collision found between animal and the group""" for other_animal in animal_group: if collide_circle(animal, other_animal): collision = Collision(animal, other_animal) return collision