|
| 1 | +import pygame |
| 2 | +from Config import * |
| 3 | +from ClassSpriteSheet import SpriteSheet |
| 4 | + |
| 5 | + |
| 6 | +runSprites = [ |
| 7 | + (24, 16, 40, 52), |
| 8 | + (104, 16, 40, 52), |
| 9 | + (184, 16, 40, 52), |
| 10 | + (264, 16, 40, 52), |
| 11 | + (344, 16, 40, 52), |
| 12 | + (424, 16, 40, 52), |
| 13 | + (504, 16, 40, 52), |
| 14 | + (584, 16, 40, 52) |
| 15 | +] |
| 16 | + |
| 17 | +idleSprites = [ |
| 18 | + (12, 12, 44, 52), |
| 19 | + (76, 12, 44, 52), |
| 20 | + (140, 12, 44, 52), |
| 21 | + (204, 12, 44, 52) |
| 22 | +] |
| 23 | + |
| 24 | +attackSprites = [ |
| 25 | + (4, 0, 92, 80), |
| 26 | + (100, 0, 92, 80), |
| 27 | + (196, 0, 92, 80), |
| 28 | + (294, 0, 92, 80), |
| 29 | + (388, 0, 92, 80), |
| 30 | + (484, 0, 92, 80), |
| 31 | + (580, 0, 92, 80), |
| 32 | + (676, 0, 92, 80) |
| 33 | +] |
| 34 | + |
| 35 | +deathSprites = [ |
| 36 | + (0, 0, 64, 56), |
| 37 | + (80, 0, 64, 56), |
| 38 | + (160, 0, 64, 56), |
| 39 | + (240, 0, 64, 56), |
| 40 | + (320, 0, 64, 56), |
| 41 | + (400, 0, 64, 56), |
| 42 | + (480, 0, 64, 56), |
| 43 | + (560, 0, 64, 56) |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +class Hero(pygame.sprite.Sprite): |
| 49 | + |
| 50 | + def __init__(self, position, faceRight): |
| 51 | + super().__init__() |
| 52 | + |
| 53 | + # Load spritesheets |
| 54 | + idleSpriteSheet = SpriteSheet(SPRITESHEET_PATH + "Character/Idle/Idle-Sheet.png", idleSprites) |
| 55 | + runSpriteSheet = SpriteSheet(SPRITESHEET_PATH + "Character/Run/Run-Sheet.png", runSprites) |
| 56 | + attackSpriteSheet = SpriteSheet(SPRITESHEET_PATH + "Character/Attack-01/Attack-01-Sheet.png", attackSprites) |
| 57 | + deathSpriteSheet = SpriteSheet(SPRITESHEET_PATH + "Character/Dead/Dead-Sheet.png", deathSprites) |
| 58 | + |
| 59 | + self.spriteSheets = { |
| 60 | + 'IDLE' : idleSpriteSheet, |
| 61 | + 'RUN' : runSpriteSheet, |
| 62 | + 'ATTACK' : attackSpriteSheet, |
| 63 | + 'DIE' : deathSpriteSheet |
| 64 | + } |
| 65 | + |
| 66 | + self.animationIndex = 0 |
| 67 | + self.facingRight = faceRight |
| 68 | + self.currentState = 'IDLE' |
| 69 | + self.xDir = 0 |
| 70 | + self.speed = SPEED_HERO |
| 71 | + self.xPos = position[0] |
| 72 | + self.yPos = position[1] |
| 73 | + |
| 74 | + |
| 75 | + def update(self, level): |
| 76 | + self.previousState = self.currentState |
| 77 | + self.xDir = 0 |
| 78 | + |
| 79 | + # get key status |
| 80 | + if self.currentState != 'ATTACK' and self.currentState != 'DIE': |
| 81 | + keys = pygame.key.get_pressed() |
| 82 | + if keys[pygame.K_SPACE]: |
| 83 | + self.currentState = 'ATTACK' |
| 84 | + elif keys[pygame.K_LEFT]: |
| 85 | + self.xDir = -1 |
| 86 | + self.facingRight = False |
| 87 | + self.currentState = 'RUN' |
| 88 | + elif keys[pygame.K_RIGHT]: |
| 89 | + self.xDir = 1 |
| 90 | + self.facingRight = True |
| 91 | + self.currentState = 'RUN' |
| 92 | + else: |
| 93 | + self.currentState = 'IDLE' |
| 94 | + |
| 95 | + # Select animation for current player action (idle, run, jump, fall, etc.) |
| 96 | + self.selectAnimation() |
| 97 | + |
| 98 | + # Start from beginning of a new animation |
| 99 | + if self.previousState != self.currentState: |
| 100 | + self.animationIndex = 0 |
| 101 | + |
| 102 | + # Select the image |
| 103 | + self.image = self.currentAnimation[int(self.animationIndex)] |
| 104 | + |
| 105 | + # Select a rect size depending on the current animation |
| 106 | + # (xPos, yPos) = bottom-center position of the sprite |
| 107 | + if self.currentState == 'IDLE': |
| 108 | + self.rect = pygame.Rect(self.xPos - 22, self.yPos - 52, 44, 52) |
| 109 | + elif self.currentState == 'RUN': |
| 110 | + self.rect = pygame.Rect(self.xPos - 20, self.yPos - 48, 40, 48) |
| 111 | + elif self.currentState == 'ATTACK': |
| 112 | + self.rect = pygame.Rect(self.xPos - 44, self.yPos - 64, 88, 64) |
| 113 | + elif self.currentState == 'DIE': |
| 114 | + self.rect = pygame.Rect(self.xPos - 32, self.yPos - 48, 64, 48) |
| 115 | + |
| 116 | + # Play animation until end of current animation is reached |
| 117 | + self.animationIndex += self.animationSpeed |
| 118 | + if self.animationIndex >= len(self.currentAnimation): |
| 119 | + if self.currentState == 'DIE': |
| 120 | + self.animationIndex = len(self.currentAnimation) - 1 |
| 121 | + else: |
| 122 | + self.animationIndex = 0 |
| 123 | + self.currentState = 'IDLE' |
| 124 | + |
| 125 | + self.moveHorizontal(level) |
| 126 | + |
| 127 | + # Handle collisions with enemies |
| 128 | + self.checkEnemyCollisions(level.bees) |
| 129 | + |
| 130 | + |
| 131 | + def selectAnimation(self): |
| 132 | + self.animationSpeed = ANIMSPEED_HERO_DEFAULT |
| 133 | + if self.currentState == 'IDLE': |
| 134 | + self.animationSpeed = ANIMSPEED_HERO_IDLE |
| 135 | + |
| 136 | + spriteSheet = self.spriteSheets[self.currentState] |
| 137 | + self.currentAnimation = spriteSheet.getSprites(flipped = not self.facingRight) |
| 138 | + |
| 139 | + |
| 140 | + def moveHorizontal(self, level): |
| 141 | + self.rect.centerx += self.xDir * self.speed |
| 142 | + |
| 143 | + # Do not walk outside level |
| 144 | + if self.rect.left < 0: |
| 145 | + self.rect.left = 0 |
| 146 | + elif self.rect.right > WINDOW_WIDTH: |
| 147 | + self.rect.right = WINDOW_WIDTH |
| 148 | + |
| 149 | + self.xPos = self.rect.centerx |
| 150 | + |
| 151 | + |
| 152 | + def die(self): |
| 153 | + if self.currentState != 'DIE': |
| 154 | + self.currentState = 'DIE' |
| 155 | + self.animationIndex = 0 |
| 156 | + |
| 157 | + |
| 158 | + def checkEnemyCollisions(self, enemies): |
| 159 | + # Check for collisions between hero and all enemies in the spritegroup |
| 160 | + collidedSprites = pygame.sprite.spritecollide(self, enemies, False) |
| 161 | + for enemy in collidedSprites: |
| 162 | + if self.currentState == 'ATTACK': |
| 163 | + if self.facingRight == True: |
| 164 | + if enemy.rect.left < self.rect.right - 30: |
| 165 | + enemy.die() |
| 166 | + else: |
| 167 | + if enemy.rect.right > self.rect.left + 30: |
| 168 | + enemy.die() |
| 169 | + else: |
| 170 | + if enemy.currentState != 'DYING': |
| 171 | + if self.rect.left < enemy.rect.left: # Collision on right side |
| 172 | + if self.rect.right > enemy.rect.left + 16: |
| 173 | + self.die() |
| 174 | + elif self.rect.right > enemy.rect.right: # Collision on left side |
| 175 | + if self.rect.left < enemy.rect.right - 16: |
| 176 | + self.die() |
0 commit comments