| 2 | |
| 3 | |
| 4 | class Fighter: |
| 5 | def __init__(self, player, x, y, flip, data, sprite_sheet, animation_steps, sound): |
| 6 | self.player = player |
| 7 | self.size = data[0] |
| 8 | self.image_scale = data[1] |
| 9 | self.offset = data[2] |
| 10 | self.flip = flip |
| 11 | self.animation_list = self.load_images(sprite_sheet, animation_steps) |
| 12 | self.action = 0 # 0:idle #1:run #2:jump #3:attack1 #4: attack2 #5:hit #6:death |
| 13 | self.frame_index = 0 |
| 14 | self.image = self.animation_list[self.action][self.frame_index] |
| 15 | self.update_time = pygame.time.get_ticks() |
| 16 | self.rect = pygame.Rect((x, y, 80, 180)) |
| 17 | self.vel_y = 0 |
| 18 | self.running = False |
| 19 | self.jump = False |
| 20 | self.attacking = False |
| 21 | self.attack_type = 0 |
| 22 | self.attack_cooldown = 0 |
| 23 | self.attack_sound = sound |
| 24 | self.hit = False |
| 25 | self.health = 100 |
| 26 | self.alive = True |
| 27 | |
| 28 | def load_images(self, sprite_sheet, animation_steps): |
| 29 | # extract images from spritesheet |
| 30 | animation_list = [] |
| 31 | for y, animation in enumerate(animation_steps): |
| 32 | temp_img_list = [] |
| 33 | for x in range(animation): |
| 34 | temp_img = sprite_sheet.subsurface( |
| 35 | x * self.size, y * self.size, self.size, self.size |
| 36 | ) |
| 37 | temp_img_list.append( |
| 38 | pygame.transform.scale( |
| 39 | temp_img, |
| 40 | (self.size * self.image_scale, self.size * self.image_scale), |
| 41 | ) |
| 42 | ) |
| 43 | animation_list.append(temp_img_list) |
| 44 | return animation_list |
| 45 | |
| 46 | def move(self, screen_width, screen_height, target, round_over): |
| 47 | SPEED = 10 |
| 48 | GRAVITY = 2 |
| 49 | dx = 0 |
| 50 | dy = 0 |
| 51 | self.running = False |
| 52 | self.attack_type = 0 |
| 53 | |
| 54 | # get keypresses |
| 55 | key = pygame.key.get_pressed() |
| 56 | |
| 57 | # can only perform other actions if not currently attacking |
| 58 | if self.attacking == False and self.alive == True and round_over == False: |
| 59 | # check player 1 controls |
| 60 | if self.player == 1: |
| 61 | # movement |