def absorb(self, index, player_list, wb_list): #absorb whiteball for i in range(len(wb_list) - 1, -1, -1): wb = wb_list[i] if (wb.pos - self.pos ).length_squared() < modelConst.explosive_radius**2: wb_list.append(White_Ball(Vec(wb.pos), True, index)) wb_list.pop(i) #absorb competitor's ball for other in player_list: if other.index == index: continue tag = 0 for i in range(len(other.body_list) - 1, 0, -1): cb = other.body_list[i] if (cb.pos - self.pos ).length_squared() < modelConst.explosive_radius**2: tag = i if tag == 0: continue for i in range(len(other.body_list) - 1, 0, -1): cb = other.body_list[i] if (cb.pos - self.pos ).length_squared() < modelConst.explosive_radius**2: wb_list.append(White_Ball(Vec(cb.pos), True, index)) other.body_list.pop(i) elif i > tag: wb_list.append( White_Ball(Vec(cb.pos), True, other.index, other.index)) other.body_list.pop(i)
def move(self): self.acc = Vec(0, 0) # Apply gravity. self.acc = Vec( 0, PLAYER_MOVEMENT["jump"]["gravity"] * self.gravity_orientation) # Get key presses for movement. self.apply_keys() # Forward/backwards movement. # Apply friction. self.acc += self.vel * PLAYER_MOVEMENT["jump"]["friction"] # New velocity after. # vf = vi + at self.vel = self.vel + self.acc * self.game.dt # Displacement. # d = vit + 1/2at^2 self.displacement = self.vel * self.game.dt + 0.5 * self.acc * \ self.game.dt ** 2 self.pos += self.displacement # Wrap around the screen. # screen_wrap(self) self.collide_walls() self.collide_items()
def update(self): if self.radius < modelconst.body_radius: self.radius += 1 self.pos = Vec(self.pre.pos_log[0]) self.pos_log.append(Vec(self.pos)) if len(self.pos_log) > modelconst.pos_log_max: self.pos_log.pop(0)
def collisionOnRoute(self, pos1, radius1, _dir, pos2, radius2): Pos1 = Vec(pos1) Pos2 = Vec(pos2) Dir = Vec(_dir) inner_product = (Pos2 - Pos1).dot(Dir) outer_product = abs((Pos2 - Pos1).cross(Dir)) return inner_product > 0 and outer_product > 0 and outer_product + modelConst.eps <= radius1 + radius2
def __init__(self, player, entities, clickables, xpos, ypos): super().__init__(entities, clickables, xpos, ypos, interactible_size, interactible_size, interactible_color) self.v = Vec(0, 0) self.a = Vec(0, 0) self.mass = interactible_mass self.is_solid = True
def attack(self, carry, my_pos): if self.helper.get_player_item_is_active( ) and self.helper.get_player_item_name() == 'Invincible': return 0, 0, 0 players_value = self.helper.get_players_value() players_speed = self.helper.get_players_speed() players_position = self.helper.get_players_position() my_speed = self.helper.get_player_speed() maximum = 0 target = -1 for i in range(4): if self.helper.player_id == i: continue #bug speed_relative = abs(players_speed[i] - my_speed) if abs(players_speed[i] - my_speed) != 0 else 0.1 length_relative = (Vec(my_pos) - Vec(players_position[i])).length( ) if (Vec(my_pos) - Vec(players_position[i])).length() != 0 else 10000 cp = 0.9e-3 * (players_value[i] - self.helper.get_player_insurance_value(i) - carry) / (length_relative / speed_relative) # print("{}th cp is {}".format(i, cp)) if maximum <= cp and players_speed[i] < my_speed: maximum = cp target = i #print(maximum, players_position[target], target) return maximum, players_position[target], target
def __init__(self, screen, all_sprites, rotation=0, speed=None): super().__init__(screen, Config, 0, 0, all_sprites) self.size = "medium" self.image = self.get_random_asteroid() self.rect = self.image.get_rect() self.rect.bottom = self.screen_rect.top self.rect.centerx = random.choice(range(0, self.screen_rect.right)) self.pos = Vec(self.rect.center) if rotation == 0: rotation_reaches = eval("Config." + self.size + "_rot_reach") self.rotation = random.choice( range(rotation_reaches)) * random.choice([-1, 1]) else: self.rotation = rotation if not speed: x_speed_reaches = eval("Config.common_horizontal_speed_reach") x_speed = random.choice( range(x_speed_reaches[0], x_speed_reaches[1])) # will drift right if occupies left side of the screen, otherwise left if self.rect.centerx > self.screen_rect.centerx: x_speed *= -1 y_speed_reaches = eval("Config." + self.size + "_vertical_speed_reach") y_speed = random.choice( range(y_speed_reaches[0], y_speed_reaches[1])) self.speed = Vec(x_speed, y_speed) else: self.speed = speed
def __init__(self, game, x, y, image_string): self._layer = PLAYER_LAYER # Pygame sprite creation with groups. self.groups = [game.all_sprites, game.visible_sprites, game.players] pg.sprite.Sprite.__init__(self, self.groups) # Save the game object to access data later. self.game = game # Position. self.pos = Vec(x, y) self.displacement = Vec(0, 0) self.vel = Vec(0, 0) self.acc = Vec(0, 0) # Jumping. self.on_ground = False self.jumping = False self.gravity_orientation = 1 # Sprite image. self.image_string = image_string self.image = game.player_imgs[image_string] self.rect = self.image.get_rect() self.rect.center = (x, y) self.hit_rect = pg.Rect(self.rect.x, self.rect.y, PLAYER_HIT_RECT_WIDTH, PLAYER_HIT_RECT_HEIGHT) self.hit_rect.center = self.rect.center self.color = CYAN self.moving_obstacle = None
def __init__(self, name, color, size = None): self._size: Vec = Vec(5, 5) if size is None else Vec(size) if name == 'home': self._size = Vec(10, 10) self._color = color self._icon = self._make_icon() self._name = name self.pos = (0, 0)
def get_direction(self, vector_to_go): move_dir = 0 #default vec_dot = 0 for dir_vec in model_const.dir_mapping: if Vec(dir_vec).dot(Vec(vector_to_go)) > vec_dot: vec_dot = Vec(dir_vec).dot(Vec(vector_to_go)) move_dir = model_const.dir_mapping.index(dir_vec) return move_dir
def __init__(self, game): self.game = game sprite_mine.__init__(self) self.image = pygame.Surface((50, 40)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.rect.midbottom = Vec(0, 0) self.pos = Vec(30, HEIGHT - 55)
def get_nearest_oil(self, player_id=None): if player_id == None: player_id = self.player_id my_pos = self.get_player_position(player_id) oils = self.get_oils() if len(oils) == 0: return None else: return min(oils, key=lambda oil: (Vec(oil) - Vec(my_pos)).length())
def getNearestPosToCenter(self): if not self.checkMeInGrav(): return None gPos, gRadius = self.getMyGrav() gPos = Vec(gPos) hPos = Vec(self.getMyHeadPos()) hDir = Vec(self.getMyDir()) inner_product = (gPos - hPos).dot(hDir) return tuple(hPos + inner_product * hDir)
def apply_pos(self, pos): """ calculate the position of the given coordinates relative to the point of view :param pos: coordinates to calculate the relative position :type pos: tuple[float, float] or pygame.math.Vector2 :return: the relative position :rtype: pygame.math.Vector2 """ return Vec(pos) - Vec(self.rel_rect.topleft)
def __init__(self, friction: int = None): super().__init__() self.friction = 1 if friction is None else friction # Note that without friction (1), any object will move perpetually. self.acc = Vec(0, 0) self.vel = Vec(0, 0) self.pos = Vec() self.max_speed = MAX_SPEED
def get_dir(self, dest, my_pos): new = Vec(dest) - Vec(my_pos) maximum = 0 record = 0 for i in range(9): if maximum < (new).dot(Vec(direct[i])): maximum = (new).dot(Vec(direct[i])) record = i return record
def headOnRoute(self): hPos = Vec(self.getMyHeadPos()) hDir = Vec(self.getMyDir()) pos_list = [] for player in self.model.player_list: if player.index == self.index or (not player.is_alive): continue if self.collisionOnRoute(hPos, modelConst.head_radius, hDir, player.pos, modelConst.head_radius): pos_list.append(tuple(player.pos)) return pos_list
def canGetOnRoute(self): hPos = Vec(self.getMyHeadPos()) hDir = Vec(self.getMyDir()) count = 0 for wb in self.model.wb_list: if wb.target != -1: continue if self.collisionOnRoute(hPos, modelConst.head_radius, hDir, wb.pos, modelConst.wb_radius): count += 1 return count
def distance_sorter(polygon): p = Vec() for v in polygon: p.x += v[0] p.y += v[1] p.z += v[2] p.x /= 4 p.y /= 4 p.z /= 4 return p.distance_to(Vec(300, 300, -50))
def get_best_oil_position(self): my_pos = self.helper.get_player_position() oil_poses = self.helper.get_oils() best_pos = None best_cp = -1 for oil_pos in oil_poses: cp = (400 - self.helper.get_distance_to_center(oil_pos)) / (( (Vec(my_pos) - Vec(oil_pos)).length())**2) if cp > best_cp: best_cp = cp best_pos = oil_pos return best_pos, my_pos, best_cp
def project(pos, pitch, yaw, cam): global e x = pos[0] - cam.x y = pos[1] - cam.y z = pos[2] - cam.z d = Vec(x, y, z) d = d.rotate(-pitch, Vec(1, 0, 0).rotate(yaw, Vec(0, 1, 0))) d = d.rotate(-yaw, Vec(0, 1, 0)) if d.z > 0.1: return (e / d.z * d.x + width / 2, e / d.z * d.y + height / 2) else: return 0
def __init__(self, player, base, rank): self.player = player self.base = base self.rank = rank self.position = Vec(model_const.score_position[rank]) self.target = Vec(model_const.score_position[rank]) self.velocity = Vec((0, 0)) self.acceleration = Vec((0, 0)) self.p_value = player.value self.b_value = base.value_sum self.timer = 0
def click(self, bullet_list, wb_list): if not self.is_alive: return if self.init_timer != -1: return if not self.is_dash: if self.is_circling or self.is_ingrav: self.is_circling = (not self.is_circling) if self.is_circling: self.circling_radius = (self.pos - self.grav_center).length() ori = self.direction.cross(self.pos - self.grav_center) if ori > 0: #counterclockwise self.theta = atan2( self.pos.y - self.grav_center.y, -self.pos.x + self.grav_center.x) - pi / 2 self.direction = Vec(cos(self.theta), -sin(self.theta)) self.ori = 1 else: self.theta = atan2( self.pos.y - self.grav_center.y, -self.pos.x + self.grav_center.x) + pi / 2 self.direction = Vec(cos(self.theta), -sin(self.theta)) self.ori = -1 else: self.circling_radius = 0 elif self.dash_cool == 0: self.is_dash = True self.dash_timer = modelconst.max_dash_time * modelconst.dash_speed_multiplier self.dash_cool = self.dash_timer + modelconst.dash_cool #self.speed = modelconst.dash_speed if len(self.body_list) > 1: self.body_list.pop(-1) if self.always_multibullet or self.have_multibullet: bullet_list.append( Bullet(self.pos, self.direction, self.index)) bullet_list.append( Bullet(self.pos, self.direction.rotate(30), self.index)) bullet_list.append( Bullet(self.pos, self.direction.rotate(-30), self.index)) self.have_multibullet = False elif self.always_bigbullet or self.have_bigbullet: bullet_list.append( Bullet(self.pos, self.direction, self.index, modelconst.bigbullet_r)) self.have_bigbullet = False else: bullet_list.append( Bullet(self.pos, self.direction, self.index))
def __init__(self, player, entities, xpos, ypos): super().__init__(entities, xpos, ypos, 2 * missile_prox, 2 * missile_prox) self.target = player self.a = Vec(0, 0) self.v = Vec(0, 0) self.prox = missile_prox self.state_counter = 0 self.explosion_radius = missile_explosion_radius self.max_speed = missile_max_speed self.sfd = False # Drawing surface and state counter self.set_state(MissileState.WAITING)
def get_nearest_player(self, player_id=None): if player_id == None: player_id = self.player_id my_pos = self.get_player_position(player_id) players = self.get_players_position() min_distance = 800 id = None for i in range(len(players)): if i == player_id: continue if (Vec(players[i]) - Vec(my_pos)).length() < min_distance: min_distance = (Vec(players[i]) - Vec(my_pos)).length() id = i return id
def __init__(self, pre, following = False): self.pre = pre self.index = pre.index self.color = pre.color if following: self.radius = modelconst.body_radius else: self.radius = 0 self.pos = Vec(pre.pos_log[0]) #try: # self.pos = pre.pos - pre.direction * (pre.radius + modelconst.body_radius + modelconst.body_gap) #except: # self.pos = pre.pos - (pre.pre.pos - pre.pos).normalize() * (pre.radius + modelconst.body_radius + modelconst.body_gap) self.pos_log = [Vec(self.pos)]
def create_bullet(self): if self.ticks < modelConst.suddendeath_ticks: return if self.ticks == modelConst.suddendeath_ticks: self.evManager.Post(Event_SuddenDeath()) if random.randint( 0, modelConst.freq * int(viewConst.FramePerSec**2 / (self.ticks - modelConst.suddendeath_ticks + 1))) == 0: screen_mid = Vec(viewConst.ScreenSize[1] / 2, viewConst.ScreenSize[1] / 2) rndtheta = random.random() * 2 * pi self.bullet_list.append( Bullet(screen_mid, Vec(cos(rndtheta),sin(rndtheta)), -1, modelConst.bullet_radius,\ modelConst.suddendeath_speed , 0 ) )
def pick_item(self, dest, my_pos, carry): if self.helper.get_player_item_name( ) or self.helper.get_player_item_is_active(): return dest item = self.helper.get_market() if ((item[0] == 'RadiationOil' and self.helper.get_timer() <= 85 * 60) or item[0] in self.want_to_buy and self.want_to_buy[item[0]] > 0) \ and carry > item[1] \ and not self.helper.get_player_item_name(): if (Vec(my_pos) - Vec(self.helper.get_market_center()) ).length() < self.helper.market_radius: if item[0] in self.want_to_buy: self.want_to_buy[item[0]] -= 1 return PICK return self.helper.get_market_center() return dest
def __init__(self, entities, xpos, ypos, xsize=default_size, ysize=default_size): self.pos = Vec(xpos, ypos) self.xsize = xsize self.ysize = ysize self.size_v = Vec(self.xsize / 2, self.ysize / 2) self.center = (int(self.xsize / 2), int(self.ysize / 2)) entities.append(self) self._surface = None self.sfd = False self.is_solid = False
def __init__(self, pos, size, parent_surface, steps=10): super().__init__(pos, size, parent_surface) size = Vec(size) width = size.x - size.y * 2 full_step_width = width // steps full_step_width -= 0.5 * full_step_width // steps margin = 0.25 * full_step_width step_width = full_step_width - margin self._style.update({ 'step_width': step_width, 'margin': margin, 'active_step_color': (255, 255, 255), 'inactive_step_color': (180, 180, 180), 'steps': steps, 'bg_color': (0, 0, 0) }) self._surfaces = self._build_surfaces() self._rects = self._build_rects() self._state.update({ 'left_button_hover': False, 'right_button_hover': False, 'value': 5 }) self._draw_button('left') self._draw_button('right') self._update_surface()