def update(self): self.is_possible_direction() actual_position = helpers.scenario_to_screen(self.position_on_scenario, self.background) self.position_on_scenario += self.velocity * self.final_direction.normalize() next_position = helpers.scenario_to_screen(self.position_on_scenario, self.background) _, angle = (next_position-actual_position).as_polar() self.image = pg.transform.rotozoom(self.original_image, -angle, 1) self.rect = self.image.get_rect() self.rect.center = helpers.scenario_to_screen(self.position_on_scenario, self.background, False) self.distance += self.velocity
def check_collision_player_powerups_multiplayer(self): # check if someone picked up any powerup if self.is_host: for powerup in self.powerups_list: for player_name in self.player_group: player_rect = pg.Rect(0, 0, 20, 20) # carteação total scenario_position = self.player_group[player_name][ 'position_on_scenario'] player_rect.center = helpers.scenario_to_screen( scenario_position, self.background, False) if player_rect.colliderect(powerup.rect): if player_name == self.player.name: self.player.gets_powerup(powerup.powerup_type) else: self.server_client.push_powerups_state( player_name, powerup.powerup_type, True) self.server_client.pull_powerups_state() self.powerups_list.remove(powerup) else: self.server_client.push_powerups_state("null", "null", False) self.server_client.pull_powerups_state() player_list_status = self.server_client.powerups_clients_state for player_name in player_list_status: if player_name == self.player.name and player_list_status[ player_name] != "None": self.player.gets_powerup(player_list_status[player_name]) self.server_client.push_powerups_state( player_name, "None", True) self.server_client.pull_powerups_state()
def rotate(self): player_position = pg.math.Vector2( self.nearest_player['position_on_screen']) bot_position = helpers.scenario_to_screen(self.position_on_scenario, self.background) _, self.angle = (player_position - bot_position).as_polar() self.angle = self.angle # gira todas as imagens # self.image = pg.transform.rotozoom(self.original_image, -self.angle, 1) self.image = pg.transform.rotate(self.original_image, -self.angle) # gira em torno do centro real # encontra a nova posição do centro do rect self.rotated_center = self.delta_center_position.rotate(+self.angle) self.new_rect_center = self.rotated_center + helpers.scenario_to_screen( self.position_on_scenario, self.background) # atualiza o rect da imagem com o novo centro correto self.rect = self.image.get_rect(center=self.new_rect_center)
def draw_multiplayer(cls, screen, background, server_info): position_on_screen = helpers.scenario_to_screen( server_info['position_on_scenario'], background, False) try: if server_info['type'] == 'life': image = cls.life_image rect = image.get_rect(center=position_on_screen) screen.blit(image, rect) except: pass
def __init__(self, background, powerup_type): super().__init__() self.powerup_type = powerup_type if powerup_type == 'life': type(self).life_image = pygame.image.load( "Assets/Images/powerups/life_up.png").convert_alpha() self.image = type(self).life_image elif powerup_type == 'rifle': type(self).rifle_image = pygame.image.load( "Assets/Images/ak47.png").convert_alpha() self.image = type(self).rifle_image elif powerup_type == 'shotgun': type(self).shotgun_image = pygame.image.load( "Assets/Images/shotgun.png").convert_alpha() self.image = type(self).shotgun_image elif powerup_type == 'rifle_ammo': type(self).rifle_ammo_image = pygame.image.load( "Assets/Images/rifle_ammo.png").convert_alpha() self.image = type(self).rifle_ammo_image elif powerup_type == 'shotgun_ammo': type(self).shotgun_ammo_image = pygame.image.load( "Assets/Images/shotgun_ammo.png").convert_alpha() self.image = type(self).shotgun_ammo_image elif powerup_type == 'handgun_ammo': type(self).handgun_ammo_image = pygame.image.load( "Assets/Images/handgun_ammo.png").convert_alpha() self.image = type(self).handgun_ammo_image self.background = background self.position_on_scenario = None self.get_free_position() self.center = helpers.scenario_to_screen(self.position_on_scenario, self.background, False) self.rect = self.image.get_rect(center=self.center) self.life_current_quantity = 0 self.life_maximum_quantity = 5 self.player_name = None
def draw_multiplayer(self, screen, server_info): position_on_screen = helpers.scenario_to_screen( server_info['position_on_scenario'], self.background, False) _, angle = ( pg.math.Vector2(server_info['mouse_position']) - pg.math.Vector2(server_info['position_on_scenario'])).as_polar() angle -= self.background.angle # [TODO] os nomes das animações estavam chegando com nonetype nessa parte e dava problema try: # body animation = getattr(self.animation, server_info['animation_body']) original_image = animation[server_info['animation_body_index']] except: animation = getattr(self.animation, 'rifle_idle') original_image = animation[0] if server_info['weapon_type'] == 'rifle' or server_info[ 'weapon_type'] == 'shotgun': delta_center_position = pygame.math.Vector2((+56 / 2.7, -19 / 2.7)) else: delta_center_position = pygame.math.Vector2((0, 0)) [imageMultiplayer, rect_multiplayer ] = helpers.rotate_fake_center(original_image, angle, delta_center_position, position_on_screen) # feet try: animation_feet = getattr(self.animation, server_info['animation_feet']) original_image = animation_feet[ server_info['animation_feet_index']] except: animation_feet = getattr(self.animation, 'feet_idle') original_image = animation_feet[0] [imageFeet, rect_feet] = helpers.rotate_fake_center(original_image, angle, pg.math.Vector2((0, 0)), position_on_screen) # draw images screen.blit(imageFeet, rect_feet) screen.blit(imageMultiplayer, rect_multiplayer)
def draw_multiplayer(self, screen, server_info, player_group): self.player_group = player_group position_on_screen = helpers.scenario_to_screen( server_info['position_on_scenario'], self.background, False) # body animation = getattr(self.animation, server_info['animation_name']) original_image = animation[server_info['animation_index']] _, angle = ( pg.math.Vector2(server_info['player_position']) - pg.math.Vector2(server_info['position_on_scenario'])).as_polar() angle -= self.background.angle [imageMultiplayer, rect_multiplayer ] = helpers.rotate_fake_center(original_image, angle, self.delta_center_position, position_on_screen) if helpers.is_visible_area(position_on_screen): # draw images screen.blit(imageMultiplayer, rect_multiplayer)
def check_collision_player_bullet(self): class player_ghost_class(pg.sprite.Sprite): def __init__(self): super().__init__() # is needed only in multiplayer mode if self.multiplayer_on: player_ghost = player_ghost_class() # create rects for each player for player_name in self.player_group: player_rect = pg.Rect(0, 0, 20, 20) # carteação total scenario_position = self.player_group[player_name][ 'position_on_scenario'] player_rect.center = helpers.scenario_to_screen( scenario_position, self.background, False) player_ghost.rect = player_rect bullet_collision = pg.sprite.spritecollideany( player_ghost, self.bullet_list) if bullet_collision: if player_name == self.player.name: self.player.gets_hit_by_weapon()
def __init__(self, origin_scenario, image, background, destination_scenario, shooter_name, weapon_type): pg.sprite.Sprite.__init__(self) self.weapon_type = weapon_type self.shooter_name = shooter_name self.velocity = 20 self.background = background self.mask = pg.mask.from_surface(image) self.player_position = pg.math.Vector2(origin_scenario) # vector between player head and rifle self.vector_offset = pg.math.Vector2((172.5/2.7, 32/2.7)) #Angle correction self.first_direction = destination_scenario - self.player_position _, angle = self.first_direction.as_polar() try: hypotenuse = self.first_direction.length() add_angle = math.degrees(math.asin(32/(2.7*hypotenuse))) angle -= add_angle except: pass correction_angle = math.degrees(math.atan(32/172)) self.final_direction = self.vector_offset.rotate(angle-correction_angle) #Start position correction self.new_vector = self.vector_offset.rotate(angle) self.position_on_scenario = self.player_position + self.new_vector self.position_on_screen = helpers.scenario_to_screen(self.position_on_scenario, self.background) self.image = image self.original_image = image self.image = pg.transform.rotozoom(self.image, -angle, 1) self.rect = self.image.get_rect(center=self.position_on_screen) self.distance = 0 self.is_colliding = False
def __init__(self, location_on_scenario, surface, background, player_group, animation): super().__init__() self.velocity = random.randint(3, 6) # load all objects necessary for bot interaction self.player_is_dead = True self.player_group = self.mount_player_group(player_group) self.player_is_dead = False self.multiplayer_on = False # this variable change at self.mount_player_group self.background = background # original_image will be used in rotate self.original_image = None # surface to draw self.surface = surface # represent the position in relation to map # when drawing, this coordinate should be converted to position in screen self.position_on_scenario = pg.math.Vector2(location_on_scenario) # image and rect will be defined according with the animation self.image = animation.idle[0] self.center = helpers.scenario_to_screen(self.position_on_scenario, self.background, False) self.rect = self.image.get_rect(center=self.center) # add collider to bot self.collider = Collider.Collider(pg.Rect(0, 0, 40, 40), self.background.rect, None) # the center of sprite isnt the center of the own image file # so, this is needed to find the real center self.delta_center_position = pg.math.Vector2((+35.5 / 2.7, -8 / 2.7)) Sound.Bot.load() self.grunt = Sound.Bot.grunt # flag self.is_grunting = False self.is_moving = False self.is_attacking = False # load animation self.animation = animation # index for animations self.index_animation_idle = 0 self.index_animation_move = 0 self.index_animation_attack = 0 self.float_index = 0 #multiplayer animation self.animation_name = None self.animation_index = None # life inicial status self.life = 3 self.is_dead = False self.victim_name = None # group with all other zombies self.bot_group = None
def update(self): self.rect.center = helpers.scenario_to_screen( self.position_on_scenario, self.background)