From a9770aff7eb34e238e00bebd74b2e215b9da2256 Mon Sep 17 00:00:00 2001 From: Jefferson Mousikas Date: Sun, 10 Mar 2024 23:47:22 +0000 Subject: [PATCH] Update viecher.py added animations for old man walking, old man attacking and zombie attacking --- viecher.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 112 insertions(+), 11 deletions(-) diff --git a/viecher.py b/viecher.py index 0e2f3c3..b5772ed 100644 --- a/viecher.py +++ b/viecher.py @@ -151,27 +151,78 @@ class Fighter(Objects): class MainCharacter(Fighter): - def __init__(self, name, ms, sprite, x, y, health, damage, level, asp, atr, killed =[]) -> None: + def __init__(self, name, ms, sprite, x, y, health, damage, level, asp, atr, killed=[]) -> None: super().__init__(name, ms, sprite, x, y, health, damage, level, asp, atr) self.book = Book(0, 0, [], None, None) self.talking = False - self.level = Level(1000, 38, 150, 40, level, f'will to live: {level}%', 'simple', 20, ) - self.health = Hearts(health, sprite=['fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png'], x=900, y= 50, hurtCooldown=self.hurtCooldown) - self.thinks = Thinks(self.x+20, self.y-50, 150, 100, 'brr I\'m freezing') + self.level = Level(1000, 38, 150, 40, level, f'will to live: {level}%', 'simple', 20) + self.health = Hearts(health, sprite=['fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png'], x=900, y=50, hurtCooldown=self.hurtCooldown) + self.thinks = Thinks(self.x + 20, self.y - 50, 150, 100, 'brr I\'m freezing') self.freezing = True - self.killed = killed #amount of mobs that were killed + self.killed = killed # amount of mobs that were killed + self.load_frames(f'art/images/people/oldmanwalk.png', f'art/images/people/oldmanattack.png') + self.current_frame = 0 + self.animation_speed = 0.1 + self.last_frame_update = pg.time.get_ticks() + self.rect = pg.Rect(x, y, self.walk_frames[0].get_width(), self.walk_frames[0].get_height()) + self.is_attacking = False + + def load_frames(self, walk_sprite_sheet, attack_sprite_sheet): + self.walk_frames = self.load_animation_frames(walk_sprite_sheet, frame_width=40, frame_height=64) + self.attack_frames = self.load_animation_frames(attack_sprite_sheet, frame_width=66, frame_height=64) # Adjust frame width and height as needed + + def load_animation_frames(self, sprite_sheet, frame_width, frame_height): + sprite_sheet = pg.transform.scale2x(pg.image.load(sprite_sheet)) + animation_frames = [] + if frame_width == 40: + frames_coordinates = [ + (frame_width, 0), + (frame_width*2, 0), + (frame_width*3, 0), + (frame_width*4, 0), + (frame_width*5, 0), + (frame_width*6, 0), + (frame_width*7, 0), + (frame_width*8, 0), + ] + else: + frames_coordinates = [ + (frame_width, 0), + (frame_width*2, 0), + (frame_width*3, 0), + (frame_width*4, 0), + (frame_width*5, 0), + (frame_width*6, 0) + ] + for x, y in frames_coordinates: + frame = pg.Surface((frame_width, frame_height), pg.SRCALPHA) + frame.blit(sprite_sheet, (0, 0), (x, y, frame_width, frame_height)) + animation_frames.append(frame) + return animation_frames + def draw(self, screen): if self.hidden: return + current_time = pg.time.get_ticks() + if self.is_attacking: + animation_frames = self.attack_frames + else: + animation_frames = self.walk_frames + + if current_time - self.last_frame_update > self.animation_speed * 1000: + self.current_frame = (self.current_frame + 1) % len(animation_frames) + self.last_frame_update = current_time + current_frame_image = animation_frames[self.current_frame] + screen.blit(current_frame_image, (self.x, self.y)) + self.rect.x, self.rect.y = self.x, self.y - screen.blit(self.sprite, self.rect) self.health.draw(screen) self.level.draw(screen) self.book.draw(screen) pg.draw.rect(screen, '#e900fa', self.rect, 2) if self.thinks.hidden == False: - self.thinks.draw(screen, self.x+20, self.y-100) + self.thinks.draw(screen, self.x + 20, self.y - 100) def hurt(self, damage, objects): if not self.talking: @@ -256,12 +307,31 @@ class MainCharacter(Fighter): weapon = Punch('punch', 100, self.x, self.y, moveto, 1, Mobs, life_ticks=500) obj[3].append(weapon) self.lastAttack = pg.time.get_ticks() + if not self.is_attacking: + self.current_frame = 0 + self.is_attacking = True def update(self, keys, mouse, objects): if not self.talking: - self.walk(keys, objects) + is_moving = False + if keys[pg.K_w] or keys[pg.K_UP] or keys[pg.K_a] or keys[pg.K_LEFT] or keys[pg.K_s] or keys[pg.K_DOWN] or keys[pg.K_d] or keys[pg.K_RIGHT]: + is_moving = True + if is_moving: + self.walk(keys, objects) + self.is_attacking = False + else: + current_time = pg.time.get_ticks() + if current_time - self.last_frame_update > self.animation_speed * 1000: + self.current_frame = (self.current_frame + 1) % len(self.walk_frames) + self.last_frame_update = current_time + if not self.is_attacking: + self.current_frame = 0 if pg.mouse.get_pressed()[0]: self.attack(objects, vec(mouse)) + if self.is_attacking: + if self.current_frame == len(self.attack_frames) - 1: + self.is_attacking = False + self.current_frame = 0 self.thinks.update(objects, self) if self.health.health <= 0: return 'village' @@ -464,13 +534,44 @@ class Skeleton(Mobs): class Zombie(Mobs): - def __init__(self, name, ms, x, y, health, damage, level, asp, atr, sprite='people/zombie.png', drops=0) -> None: - super().__init__(name, ms, sprite, x, y, health, damage, level, asp, atr, drops) + def __init__(self, name, ms, x, y, health, damage, level, asp, atr, sprite_sheet='people/zombiewalk.png', drops=0) -> None: + super().__init__(name, ms, sprite_sheet, x, y, health, damage, level, asp, atr, drops) + self.load_frames(f'art/images/{sprite_sheet}') + self.current_frame = 0 + self.animation_speed = 250 + self.last_frame_update = pg.time.get_ticks() + self.hidden = False + self.rect = pg.Rect(x, y, 40, 64) + + def load_frames(self, sprite_sheet): + sprite_sheet = pg.transform.scale2x(pg.image.load(sprite_sheet).convert_alpha()) + frame_width = 40 + frame_height = 64 + frames_coordinates = [(40, 0),(80, 0),(120, 0),(160, 0),(200, 0),(240, 0),(280, 0), (320, 0)] + self.animation_frames = [] + for x, y in frames_coordinates: + frame = pg.Surface((frame_width, frame_height), pg.SRCALPHA) + frame.blit(sprite_sheet, (0, 0), (x, y, frame_width, frame_height)) + self.animation_frames.append(frame) + + def draw(self, screen): + if self.hidden: + return + current_time = pg.time.get_ticks() + if current_time - self.last_frame_update > self.animation_speed: + self.current_frame = (self.current_frame + 1) % len(self.animation_frames) + self.last_frame_update = current_time + + current_frame_image = self.animation_frames[self.current_frame] + screen.blit(current_frame_image, (self.x, self.y)) + + self.rect.topleft = (self.x, self.y) + pg.draw.rect(screen, '#ef0120', self.rect, 2) def attack(self, moveto, obj): if self.lastAttack + self.attack_speed * 1000 < pg.time.get_ticks(): - obj[3].append(Punch('punch', 100, self.x, self.y, moveto, self.damage, MainCharacter)) + obj[3].append(Punch('punch', 100, self.x, self.y, moveto, self.damage)) self.lastAttack = pg.time.get_ticks() class Rat(Mobs): -- 2.45.3