Advertisement
Maluia

playerclass

Sep 7th, 2024 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | Gaming | 0 0
  1. import pygame
  2. from healthbar import HealthBar
  3.  
  4. from functions import load_sprite_sheets
  5.  
  6.  
  7. class Player(pygame.sprite.Sprite):
  8.     COLOR = (255, 0, 0)
  9.     GRAVITY = 1
  10.     SPRITES = load_sprite_sheets("MainCharacters", "MaskDude", 32, 32, True)    #load spritesheet
  11.     ANIMATION_DELAY = 3
  12.  
  13.     def __init__(self, x, y, width, height):
  14.         super().__init__()
  15.         self.rect = pygame.Rect(x, y, width, height)
  16.         self.x_vel = 0
  17.         self.y_vel = 0
  18.         self.mask = None
  19.         self.direction = "right"
  20.         self.animation_count = 0
  21.         self.fall_count = 60
  22.         self.jump_count = 0
  23.         self.hit = False
  24.         self.hit_count = 0
  25.         self.finished = False
  26.        
  27.         self.healthbar = HealthBar (10,10)
  28.         self.hit_cooldown = False
  29.         self.hit_cooldown_event = pygame.USEREVENT + 1
  30.  
  31.     def jump(self):
  32.         self.y_vel = -self.GRAVITY * 8
  33.         self.animation_count = 0
  34.         self.jump_count += 1
  35.         if self.jump_count == 1:
  36.             self.fall_count = 0
  37.  
  38.     def move(self, dx, dy):
  39.         self.rect.x += dx
  40.         self.rect.y += dy
  41.  
  42.     def make_hit(self):
  43.         self.hit = True
  44.         self.hit_count = 0
  45.  
  46.     def move_left(self, vel):
  47.         self.x_vel = -vel
  48.         if self.direction != "left":
  49.             self.direction = "left"
  50.             self.animation_count = 0
  51.  
  52.     def move_right(self, vel):
  53.         self.x_vel = vel
  54.         if self.direction != "right":
  55.             self.direction = "right"
  56.             self.animation_count = 0
  57.  
  58.     def loop(self, fps):
  59.         self.y_vel += min(1, (self.fall_count / fps) * self.GRAVITY)
  60.         if self.y_vel and abs(self.y_vel) < 0.1:
  61.             self.y_vel = 0
  62.  
  63.         self.move(self.x_vel, self.y_vel)
  64.  
  65.         if self.hit:
  66.             self.hit_count += 1
  67.         if self.hit_count > fps * 2:    #2 seconds
  68.             self.hit = False
  69.             self.hit_count = 0
  70.  
  71.  
  72.         self.fall_count += 1
  73.        
  74.         self.update_sprite()
  75.  
  76.     def landed(self):
  77.         self.fall_count = 0
  78.         self.y_vel = 0
  79.         self.jump_count = 0
  80.  
  81.     def hit_head(self):
  82.         self.count = 0
  83.         self.y_vel *= -1    #move downwards
  84.  
  85.     def player_hit(self, damage):
  86.         if self.hit_cooldown == False:
  87.             self.hit_cooldown = True
  88.             self.healthbar.takeDamage(damage)
  89.             pygame.time.set_timer(self.hit_cooldown_event, 2000)        #damage cooldown = 2 seconds (animation length)
  90.  
  91.     def player_heal(self, heal):
  92.         if self.hit_cooldown == False:
  93.             self.hit_cooldown = True
  94.             self.healthbar.heal(heal)
  95.             pygame.time.set_timer(self.hit_cooldown_event, 500)        #heal cooldown = 0.5 seconds
  96.  
  97.     def update_sprite(self):
  98.         sprite_sheet = "idle"
  99.         if self.hit:
  100.             sprite_sheet = "hit"
  101.         elif self.y_vel < 0:
  102.             if self.jump_count == 1:
  103.                 sprite_sheet = "jump"
  104.             elif self.jump_count == 2:
  105.                 sprite_sheet = "double_jump"
  106.         elif self.y_vel > self.GRAVITY * 2:
  107.             sprite_sheet = "fall"
  108.         elif self.x_vel != 0:
  109.             sprite_sheet = "run"
  110.  
  111.         sprite_sheet_name = sprite_sheet + "_" + self.direction
  112.         sprites = self.SPRITES[sprite_sheet_name]
  113.         sprite_index = (self.animation_count //
  114.                         self.ANIMATION_DELAY) % len(sprites)
  115.         self.sprite = sprites[sprite_index]
  116.         self.animation_count += 1
  117.         self.update()
  118.  
  119.     def update(self):
  120.         self.rect = self.sprite.get_rect(topleft=(self.rect.x, self.rect.y))
  121.         self.mask = pygame.mask.from_surface(self.sprite)
  122.  
  123.     def draw(self, win, offset):
  124.         win.blit(self.sprite, self.rect.topleft - offset)
  125.         self.healthbar.render(win)
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement