def update(self): if self.velocity.x != 0: self.set_animation("run") self.flipped = self.velocity.x < 0 else: self.set_animation("idle") NetEntity.update(self)
def set_animation(self, animation): """Override method to set the animation with armed/unarmed variants. :param animation: string animation name """ if self.is_armed: NetEntity.set_animation(self, animation + "_armed") else: NetEntity.set_animation(self, animation + "_unarmed")
def __init__(self, name, position=(0, 0)): """ :param name: string name of character :param position: tuple(2) starting position """ NetEntity.__init__(self, position) # set the texture folder self.texture_folder = "player\\{}\\".format(name) # Add all animations # Last animation loaded will be used self.add_animation("jump_armed", (0, 0, 16, 16), frames=2, fpi=6) # self.add_animation("run_armed", (0, 0, 16, 32), frames=15, fpi=4) self.add_animation("idle_armed", (0, 0, 16, 16), frames=2, fpi=30) self.add_animation("jump_unarmed", (0, 0, 16, 16), frames=2, fpi=15) self.add_animation("run_unarmed", (0, 0, 16, 16), frames=14, fpi=8) self.add_animation("idle_unarmed", (0, 0, 16, 16), frames=2, fpi=60) # whether or not the character sprite is # facing right (False) or left (True) self.flipped = False # set the examine to the name of the # character. # will most likely be changed to player's # username, and name replaced with 'skin' self.examine = [name.title(), "Lvl 1 scout", "Unarmed"] # if the player has at least one gun self.is_armed = False # player's two guns # primary is always the one shown # on the character self.primary = None self.secondary = None # amount of jumps performed by player self.jumps = 0 # used to prevent instant double jumps # since a keypress may last more than # one frame self.is_jumping = False # inventory of perks # amount of perks are stored in the perks # themselves self.perks = [] # angle the player is aiming # measured in radians from horizontal self.aim = 0
def draw(self, screen): """Draw the player to the screen. Also draws the primary weapon in a suitable position, manually moving to the player's animation. Key pixels for weapon positions may be necessary. :param screen: pygame.Surface to draw to """ # get next frame self.next_frame() # flip sprite if self.flipped: self.image = pygame.transform.flip(self.image, True, False) NetEntity.draw(self, screen) # overlay perk effects on player [item.draw_on_player(self, screen) for item in self.perks] if self.primary is not None: x_tr = 16 y_tr = -2 if self.examine == "Kyle": # kyle is short y_tr = -1 # manual tracking of gun onto player animation # TODO!! if self.get_animation() == "idle" and self.strips.i in [2, 3]: y_tr += 1 elif self.get_animation() == "run" and self.strips.i in [0, 1, 2, 7, 8, 9]: y_tr -= 1 # add recoil to the gun (if it is recoiling) # TODO: currently broken x_tr += self.primary.recoil * (1 if self.flipped else -1) # flip gun to match player if self.primary.flipped is not self.flipped: self.primary.flipped = self.flipped self.primary.image = pygame.transform.flip(self.primary.image, True, False) grip_w = self.primary.grip_point # move the gun rect left or right depending # on which way its facing if self.primary.flipped: self.primary.rect.center = self.rect.move((-x_tr + grip_w, y_tr)).center else: self.primary.rect.center = self.rect.move((x_tr - grip_w, y_tr)).center self.primary.draw(screen) # recover the gun from recoil # should be moved if self.primary.recoil > 0: self.primary.recover_recoil()
def draw(self, screen): """Draw the player to the screen. Also draws the primary weapon in a suitable position, manually moving to the player's animation. Key pixels for weapon positions may be necessary. :param screen: pygame.Surface to draw to """ # get next frame self.next_frame() # flip sprite if self.flipped: self.image = pygame.transform.flip(self.image, True, False) NetEntity.draw(self, screen) # overlay perk effects on player [item.draw_on_player(self, screen) for item in self.perks] if self.primary is not None: x_tr = 16 y_tr = -2 if self.examine == "Kyle": # kyle is short y_tr = -1 # manual tracking of gun onto player animation # TODO!! if self.get_animation() == "idle" and self.strips.i in [2, 3]: y_tr += 1 elif self.get_animation() == "run" and self.strips.i in [ 0, 1, 2, 7, 8, 9 ]: y_tr -= 1 # add recoil to the gun (if it is recoiling) # TODO: currently broken x_tr += self.primary.recoil * (1 if self.flipped else -1) # flip gun to match player if self.primary.flipped is not self.flipped: self.primary.flipped = self.flipped self.primary.image = pygame.transform.flip( self.primary.image, True, False) grip_w = self.primary.grip_point # move the gun rect left or right depending # on which way its facing if self.primary.flipped: self.primary.rect.center = self.rect.move( (-x_tr + grip_w, y_tr)).center else: self.primary.rect.center = self.rect.move( (x_tr - grip_w, y_tr)).center self.primary.draw(screen) # recover the gun from recoil # should be moved if self.primary.recoil > 0: self.primary.recover_recoil()