def __init__(self, name, bullet1_cls, bullet2_cls, ship_type=None): self.ship_type = ship_type self.tilt_angle = 0 self.weight = 1 self.health = 100 self.immortality = int(3 / app.constants.FRAME_INTERVAL) self.engine = Engine() self.damage = 0 self.kills = 0 self.name = name[:15] or 'Anonymous' self._acc = Vector2D(x=0, y=0) self.dead_step = -1 # dead_step # -1 : life # 0 : in dive # >0 impolisum and fire # 0..5 implosiom # >80 close socket self.pk = None self.candidat_position = Vector2D(x=0, y=0) self.current_speed = Vector2D(x=0, y=0) self.current_route = _pi_2 self.control = _Control() self.a_sum = Vector2D(x=0, y=0) self.demensions_info = [(p.length, p.angle() - _pi_2) for p in self.demensions] self.demensions_max_radius = max(d.y for d in self.demensions) while True: self.candidat_position = Vector2D(x=random.random() * MAP_SIZE[0], y=random.random() * MAP_SIZE[1]) if not get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.current_position = self.candidat_position self.candidat_position = Vector2D(x=0, y=0) break self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon) self.gun1 = Gun(**bullet1_cls(self).gun_kwargs) self.bullet1_cls = bullet1_cls self.gun2 = Gun(**bullet2_cls(self).gun_kwargs) self.bullet2_cls = bullet2_cls from app.castomization import SmokeBullet self.smoke_cls = SmokeBullet self.smoke_interval = _smoke_interval self.lifetime = None self.starttime = None self.dead_reason = None
def _calculate_polygon_cell(): for x in range(MAP_SIZE_APPROX[0] + 1): polygon_cell.append([]) class _FakeObject2D: def __init__(self, x, y): self.candidat_position = Vector2D(x=x, y=y) for y in range(MAP_SIZE_APPROX[1] + 1): cell = [ _FakeObject2D(x=x * CELL_STEP, y=y * CELL_STEP), _FakeObject2D(x=x * CELL_STEP, y=(y + 1) * CELL_STEP), _FakeObject2D(x=(x + 1) * CELL_STEP, y=y * CELL_STEP), _FakeObject2D(x=(x + 1) * CELL_STEP, y=(y + 1) * CELL_STEP), ] for cell_point in cell: if get_polygon_idx_collision(cell_point.candidat_position.x, cell_point.candidat_position.y): polygon_cell[x].append(True) break else: polygon_cell[x].append(False) polygon_cell[x] = bitarray(polygon_cell[x])
def _dive(self): if self.dead_step > 0: self.dead_step += 20 * app.constants.FRAME_INTERVAL return a_friction = self.current_speed * (-MU) / 2 self.current_route -= 4 * app.constants.FRAME_INTERVAL * self.control.vector self.a_sum = (gravity_dive + a_friction) self.current_speed += self.a_sum * app.constants.FRAME_INTERVAL sum_vector = self.current_speed * app.constants.FRAME_INTERVAL + ( self.a_sum / 2) * app.constants.FRAME_INTERVAL**2 self.candidat_position.x = self.current_position.x + sum_vector.x self.candidat_position.y = self.current_position.y - sum_vector.y polygon_idx = get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y) if polygon_idx: polygon = all_polygons[polygon_idx] else: polygon = None if polygon: if self.dead_step == 0: self.mark_as_dead(to_dive=False) return self.current_position.reinit( x=self.candidat_position.x, y=self.candidat_position.y, ) for idx, (p_length, p_angle) in enumerate(self.demensions_info): self.current_polygon[idx].reinit( length=p_length, angle=p_angle - self.current_route, ) self.current_polygon[idx] += self.current_position self.current_polygon_info = get_polygon_info(self.current_polygon)
def calculate_position(self): FI = app.constants.FRAME_INTERVAL self.life_limit -= FI if self.life_limit < 0: self.life_limit = -1000 # selfdestroy return if not self.current_speed: return a_sum = self.current_speed * (-MU) self.current_speed += a_sum * FI sum_vector = self.current_speed * FI + (a_sum / 2) * FI**2 self.candidat_position.reinit(self.current_position.x + sum_vector.x, self.current_position.y - sum_vector.y) if get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.life_limit = -1 return self.current_position.reinit( x=self.candidat_position.x, y=self.candidat_position.y, ) if self.current_speed.length < 2: self.current_speed = None self.current_polygon = [ Vector2D( length=p_length, angle=p_angle, ) + self.current_position for p_length, p_angle in self.demensions_info ]
def calculate_position(self): if self.immortality: self.immortality -= 1 if self.health <= 0: if self.dead_step == -1: self.mark_as_dead(to_dive=True) return self._dive() a_friction = self.current_speed * (-MU) tilt_angle_speed = 7 * self.rotation_speed * app.constants.FRAME_INTERVAL if self.control.vector: if self.control.vector > 0 and self.tilt_angle < 9: self.tilt_angle += tilt_angle_speed elif self.control.vector < 0 and self.tilt_angle > -9: self.tilt_angle -= tilt_angle_speed else: if self.tilt_angle > 0: self.tilt_angle -= tilt_angle_speed elif self.tilt_angle < 0: self.tilt_angle += tilt_angle_speed self.current_route -= self.rotation_speed * app.constants.FRAME_INTERVAL * self.control.vector if self.control.accelerator: power = self.engine.power if self.engine.is_ready else 200 self._acc.reinit( angle=self.current_route, length=power / self.weight, ) self.engine.acceleration() self.a_sum.reinit( x=self._acc.x + gravity.x + a_friction.x, y=self._acc.y + gravity.y + a_friction.y, ) else: self.a_sum.reinit( x=gravity.x + a_friction.x, y=gravity.y + a_friction.y, ) self.a_sum *= app.constants.FRAME_INTERVAL self.current_speed += self.a_sum sum_vector = self.current_speed * app.constants.FRAME_INTERVAL + ( self.a_sum / 2) * app.constants.FRAME_INTERVAL**2 self.candidat_position.x = self.current_position.x + sum_vector.x self.candidat_position.y = self.current_position.y - sum_vector.y round_x = floor(self.candidat_position.x / CELL_STEP) round_y = floor(self.candidat_position.y / CELL_STEP) try: has_world_collision = polygon_cell[round_x][round_y] except: has_world_collision = True if has_world_collision: polygon_idx = get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y) if polygon_idx: polygon = all_polygons[polygon_idx] else: polygon = None else: polygon = None if polygon: angle = get_angle_collision(self, polygon) or _pi_2 self.current_speed.reinit(length=self.current_speed.length * 0.3, angle=angle * 2 + self.current_speed.angle()) self.health -= 100 * app.constants.FRAME_INTERVAL # polygon dead if self.health <= 0: self.mark_as_dead(DeadReason.WORLD_COLLISION) self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon) return self.current_position.reinit( x=self.candidat_position.x, y=self.candidat_position.y, ) self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon)
def __init__( self, emitter=None, health=1, fuel=0, bullet1=0, bullet2=0, dispersion=True, start_position=None, life_limit=30, ): self.type = 0 self.damage = 0 # interface consist self.kills = 0 # interface consist self.current_route = 0 # interface consist self.pk = random.randint(1, 30000) self.ship_emitter = emitter if dispersion: bonus_route = random.random() * 2 * math.pi else: bonus_route = 0 self._demensions = [ Vector2D(x=-15, y=15), Vector2D(x=15, y=15), Vector2D(x=15, y=-15), Vector2D(x=-15, y=-15), ] self.demensions_info = [(p.length, p.angle() - math.pi / 2) for p in self._demensions] self.demensions_max_radius = max(d.y for d in self._demensions) if start_position: self.current_position = Vector2D( x=start_position.x, y=start_position.y, ) self.candidat_position = Vector2D(x=0, y=0) else: while True: self.candidat_position = Vector2D( x=random.random() * MAP_SIZE[0], y=random.random() * MAP_SIZE[1], ) if not get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.current_position = self.candidat_position break self.current_polygon = [ Vector2D( length=p_length, angle=p_angle, ) + self.current_position for p_length, p_angle in self.demensions_info ] if dispersion: self.current_speed = Vector2D(length=60, angle=bonus_route) else: self.current_speed = None self.life_limit = life_limit self.health = health self.fuel = fuel self.bullet1 = bullet1 self.bullet2 = bullet2
import xlimb_helper if __debug__ is not True: raise ("Check __debug__ option") assert xlimb_helper.distance((1, 1), (2, 2)) == 1.4142135623730951 print('OK distance') assert xlimb_helper.resolve_line((1, 1), (100, 100)) == (1, 0) print('OK resolve_line') assert xlimb_helper.resolve_line((1, 1), (1, 100)) == (None, None) print('OK resolve_line') assert xlimb_helper.get_polygon_idx_collision(100.0, 1500.0) == 13 print('OK get_polygon_idx_collision') class Vector(): def __init__(self): self.x = 100 self.y = 200 class A(object): def __init__(self, x): self.life_limit = 100 self.able_to_make_tracing = 2 self.current_speed = Vector() self.current_position = Vector() self.approx_x = 60 self.approx_y = 60 self.ricochet = 1