|
| 1 | +import pygame |
| 2 | +from support import import_sprite |
| 3 | + |
| 4 | +class Player(pygame.sprite.Sprite): |
| 5 | + def __init__(self, pos): |
| 6 | + super().__init__() |
| 7 | + self._import_character_assets() |
| 8 | + self.frame_index = 0 |
| 9 | + self.animation_speed = 0.15 |
| 10 | + self.image = self.animations["idle"][self.frame_index] |
| 11 | + self.rect = self.image.get_rect(topleft = pos) |
| 12 | + self.mask = pygame.mask.from_surface(self.image) |
| 13 | + |
| 14 | + # player movement |
| 15 | + self.direction = pygame.math.Vector2(0, 0) |
| 16 | + self.speed = 5 |
| 17 | + self.jump_move = -16 |
| 18 | + |
| 19 | + # player status |
| 20 | + self.life = 5 |
| 21 | + self.game_over = False |
| 22 | + self.win = False |
| 23 | + self.status = "idle" |
| 24 | + self.facing_right = True |
| 25 | + self.on_ground = False |
| 26 | + self.on_ceiling = False |
| 27 | + self.on_left = False |
| 28 | + self.on_right = False |
| 29 | + |
| 30 | + # gets all the image needed for animating specific player action |
| 31 | + def _import_character_assets(self): |
| 32 | + character_path = "assets/player/" |
| 33 | + self.animations = { |
| 34 | + "idle": [], |
| 35 | + "walk": [], |
| 36 | + "jump": [], |
| 37 | + "fall": [], |
| 38 | + "lose": [], |
| 39 | + "win": [] |
| 40 | + } |
| 41 | + for animation in self.animations.keys(): |
| 42 | + full_path = character_path + animation |
| 43 | + self.animations[animation] = import_sprite(full_path) |
| 44 | + |
| 45 | + # animates the player actions |
| 46 | + def _animate(self): |
| 47 | + animation = self.animations[self.status] |
| 48 | + |
| 49 | + # loop over frame index |
| 50 | + self.frame_index += self.animation_speed |
| 51 | + if self.frame_index >= len(animation): |
| 52 | + self.frame_index = 0 |
| 53 | + image = animation[int(self.frame_index)] |
| 54 | + image = pygame.transform.scale(image, (35, 50)) |
| 55 | + if self.facing_right: |
| 56 | + self.image = image |
| 57 | + else: |
| 58 | + flipped_image = pygame.transform.flip(image, True, False) |
| 59 | + self.image = flipped_image |
| 60 | + |
| 61 | + # set the rect |
| 62 | + if self.on_ground and self.on_right: |
| 63 | + self.rect = self.image.get_rect(bottomright = self.rect.bottomright) |
| 64 | + elif self.on_ground and self.on_left: |
| 65 | + self.rect = self.image.get_rect(bottomleft = self.rect.bottomleft) |
| 66 | + elif self.on_ground: |
| 67 | + self.rect = self.image.get_rect(midbottom = self.rect.midbottom) |
| 68 | + elif self.on_ceiling and self.on_right: |
| 69 | + self.rect = self.image.get_rect(topright = self.rect.topright) |
| 70 | + elif self.on_ceiling and self.on_left: |
| 71 | + self.rect = self.image.get_rect(bottomleft = self.rect.topleft) |
| 72 | + elif self.on_ceiling: |
| 73 | + self.rect = self.image.get_rect(midtop = self.rect.midtop) |
| 74 | + |
| 75 | + # checks if the player is moving towards left or right or not moving |
| 76 | + def _get_input(self, player_event): |
| 77 | + if player_event != False: |
| 78 | + if player_event == "right": |
| 79 | + self.direction.x = 1 |
| 80 | + self.facing_right = True |
| 81 | + elif player_event == "left": |
| 82 | + self.direction.x = -1 |
| 83 | + self.facing_right = False |
| 84 | + else: |
| 85 | + self.direction.x = 0 |
| 86 | + |
| 87 | + def _jump(self): |
| 88 | + self.direction.y = self.jump_move |
| 89 | + |
| 90 | + # identifies player action |
| 91 | + def _get_status(self): |
| 92 | + if self.direction.y < 0: |
| 93 | + self.status = "jump" |
| 94 | + elif self.direction.y > 1: |
| 95 | + self.status = "fall" |
| 96 | + elif self.direction.x != 0: |
| 97 | + self.status = "walk" |
| 98 | + else: |
| 99 | + self.status = "idle" |
| 100 | + |
| 101 | + # update the player's state |
| 102 | + def update(self, player_event): |
| 103 | + self._get_status() |
| 104 | + if self.life > 0 and not self.game_over: |
| 105 | + if player_event == "space" and self.on_ground: |
| 106 | + self._jump() |
| 107 | + else: |
| 108 | + self._get_input(player_event) |
| 109 | + elif self.game_over and self.win: |
| 110 | + self.direction.x = 0 |
| 111 | + self.status = "win" |
| 112 | + else: |
| 113 | + self.direction.x = 0 |
| 114 | + self.status = "lose" |
| 115 | + self._animate() |
0 commit comments