Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from healthbar import HealthBar
- from functions import load_sprite_sheets
- class Player(pygame.sprite.Sprite):
- COLOR = (255, 0, 0)
- GRAVITY = 1
- SPRITES = load_sprite_sheets("MainCharacters", "MaskDude", 32, 32, True) #load spritesheet
- ANIMATION_DELAY = 3
- def __init__(self, x, y, width, height):
- super().__init__()
- self.rect = pygame.Rect(x, y, width, height)
- self.x_vel = 0
- self.y_vel = 0
- self.mask = None
- self.direction = "right"
- self.animation_count = 0
- self.fall_count = 60
- self.jump_count = 0
- self.hit = False
- self.hit_count = 0
- self.finished = False
- self.healthbar = HealthBar (10,10)
- self.hit_cooldown = False
- self.hit_cooldown_event = pygame.USEREVENT + 1
- def jump(self):
- self.y_vel = -self.GRAVITY * 8
- self.animation_count = 0
- self.jump_count += 1
- if self.jump_count == 1:
- self.fall_count = 0
- def move(self, dx, dy):
- self.rect.x += dx
- self.rect.y += dy
- def make_hit(self):
- self.hit = True
- self.hit_count = 0
- def move_left(self, vel):
- self.x_vel = -vel
- if self.direction != "left":
- self.direction = "left"
- self.animation_count = 0
- def move_right(self, vel):
- self.x_vel = vel
- if self.direction != "right":
- self.direction = "right"
- self.animation_count = 0
- def loop(self, fps):
- self.y_vel += min(1, (self.fall_count / fps) * self.GRAVITY)
- if self.y_vel and abs(self.y_vel) < 0.1:
- self.y_vel = 0
- self.move(self.x_vel, self.y_vel)
- if self.hit:
- self.hit_count += 1
- if self.hit_count > fps * 2: #2 seconds
- self.hit = False
- self.hit_count = 0
- self.fall_count += 1
- self.update_sprite()
- def landed(self):
- self.fall_count = 0
- self.y_vel = 0
- self.jump_count = 0
- def hit_head(self):
- self.count = 0
- self.y_vel *= -1 #move downwards
- def player_hit(self, damage):
- if self.hit_cooldown == False:
- self.hit_cooldown = True
- self.healthbar.takeDamage(damage)
- pygame.time.set_timer(self.hit_cooldown_event, 2000) #damage cooldown = 2 seconds (animation length)
- def player_heal(self, heal):
- if self.hit_cooldown == False:
- self.hit_cooldown = True
- self.healthbar.heal(heal)
- pygame.time.set_timer(self.hit_cooldown_event, 500) #heal cooldown = 0.5 seconds
- def update_sprite(self):
- sprite_sheet = "idle"
- if self.hit:
- sprite_sheet = "hit"
- elif self.y_vel < 0:
- if self.jump_count == 1:
- sprite_sheet = "jump"
- elif self.jump_count == 2:
- sprite_sheet = "double_jump"
- elif self.y_vel > self.GRAVITY * 2:
- sprite_sheet = "fall"
- elif self.x_vel != 0:
- sprite_sheet = "run"
- sprite_sheet_name = sprite_sheet + "_" + self.direction
- sprites = self.SPRITES[sprite_sheet_name]
- sprite_index = (self.animation_count //
- self.ANIMATION_DELAY) % len(sprites)
- self.sprite = sprites[sprite_index]
- self.animation_count += 1
- self.update()
- def update(self):
- self.rect = self.sprite.get_rect(topleft=(self.rect.x, self.rect.y))
- self.mask = pygame.mask.from_surface(self.sprite)
- def draw(self, win, offset):
- win.blit(self.sprite, self.rect.topleft - offset)
- self.healthbar.render(win)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement