I am absolute crap at managing sprites and animations so I’m doing a mess to program my character animation, imma leave the code here which basically sucks: when I punch the character just keeps punching over and over and from the log, in any movement I make, it keeps showing the first frame alternating with the other ones making the animation look laggy. I really need some help!!
import pygame
from spritesheet import Spritesheet
class Fighter:
def __init__(self, x, y, scale):
self.rect = pygame.Rect(x, y, 64 * scale, 64 * scale)
self.scale = scale
self.yspeed = 0
self.jumping = False
self.attacktype = 0
self.attacking = False
self.attackrect = 0
self.health = 100
self.flip = False
self.action = 0 # 0 idle, 1 walk, 2 jump, 3 attack1, 4 attack2, 5 hit, 6 death
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_idle.png").convert_alpha())
self.framelimit = 0
self.update_time = pygame.time.get_ticks()
self.frame = 0
self.cooldown = 500
self.attackcooldown = 1000
self.attacktime = self.update_time
def update(self):
if pygame.time.get_ticks() - self.update_time > self.cooldown:
if (self.frame < self.framelimit - 1):
self.frame += 1
else:
self.frame = 0
self.update_time = pygame.time.get_ticks()
def checkaction(self, new_action):
if new_action != self.action:
self.action = new_action
self.frame = 0
self.update_time = pygame.time.get_ticks() # Reset update time when action changes
def addressaction(self, dx):
new_action = 0
"""if self.attacking:
if self.attacktype == 1:
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_att1.png").convert_alpha())
new_action = 3
self.framelimit = 5
self.cooldown = 75
elif self.attacktype == 2:
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_att2.png").convert_alpha())
new_action = 4
self.framelimit = 5
self.cooldown = 55 """
if self.jumping:
self.jump_animating = True
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_jump.png").convert_alpha())
new_action = 2
self.framelimit = 8
self.cooldown = 100
elif (dx != 0):
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_walk.png").convert_alpha())
new_action = 1
self.framelimit = 8
self.cooldown = 75
else:
self.image = Spritesheet(pygame.image.load("assets/lazzaro/lazzaro_idle.png").convert_alpha())
new_action = 0
self.framelimit = 4
self.cooldown = 100
self.checkaction(new_action)
def draw(self, WIN):
pygame.draw.rect(WIN, (255, 0, 0), self.rect)
if (self.attacking == True):
pygame.draw.rect(WIN, (0, 255, 0), self.attackrect)
self.update()
image = self.image.get_image(self.frame, 64, 64, self.scale,(255, 255, 0))
if (self.flip == True):
image = pygame.transform.flip(image, 180,0)
WIN.blit(image, (self.rect.x, self.rect.y))
print(self.frame)
def attack(self, target):
self.attacking = True
self.attackrect = pygame.Rect(self.rect.centerx - (2 * self.rect.width * self.flip), self.rect.y,
self.rect.width * 2,
self.rect.height) # if self.flip == False it means it's equal to 0
if self.attackrect.colliderect(target.rect):
target.health -= 10
def stop_attack(self):
self.attacking = False
def jump(self):
self.jumping = True
def stop_jump(self):
self.jumping = False
def move(self, WIDTH, HEIGHT, target):
SPEED = 10
GRAVITY = 2
dx = 0
dy = 0
key = pygame.key.get_pressed()
self.attacktype = 0
# controls
if not self.attacking:
if key[pygame.K_a]:
dx = -SPEED
if key[pygame.K_d]:
dx = SPEED
if key[pygame.K_w] and self.jumping == False:
self.yspeed = -30
self.jump()
# attack
if (key[pygame.K_e] or key[pygame.K_q]) and self.jumping == False and pygame.time.get_ticks() - self.attacktime > self.attackcooldown:
if not self.attacking:
self.attack(target)
if key[pygame.K_e]:
self.attacktype = 1
if key[pygame.K_q]:
self.attacktype = 2
self.attacktime = pygame.time.get_ticks()
# gravity
self.yspeed += GRAVITY
dy += self.yspeed
# borders
if self.rect.bottom + dy > HEIGHT - 100:
self.yspeed = 0
dy = HEIGHT - 100 - self.rect.bottom
self.stop_jump()
if self.rect.left + dx < 0:
dx = -self.rect.left
if self.rect.right + dx > WIDTH:
dx = WIDTH - self.rect.right
# addressing facing
if self.rect.centerx > target.rect.centerx:
self.flip = True
else:
self.flip = False
# updating position of the rectangle
self.rect.x += dx
self.rect.y += dy
self.addressaction(dx)
import pygame
class Spritesheet():
def __init__(self, image):
self.sheet = image
def get_image(self, frame, width, height, scale, color):
image = pygame.Surface((width, height)).convert_alpha()
image.fill(color)
image.blit(self.sheet, (0, 0), ((frame * width), 0, width, height))
image = pygame.transform.scale(image, (width * scale, height * scale))
image.set_colorkey(color)
return image
import pygame
from fighter import Fighter
from spritesheet import Spritesheet
import sys
pygame.init()
#general variables
WIDTH = 960
HEIGHT = 540
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0,0,0)
FPS = 60
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#player informations
PLSIDE = 64
PLSCALE = 4
BG = pygame.image.load("assets/bg.jpeg")
BG = pygame.transform.scale(BG, (WIDTH, HEIGHT))
pygame.display.set_caption("Docenti VS Alunni")
def drawhealth(health, x, y):
ratio = health / 100
pygame.draw.rect(WIN, WHITE, (x - 5, y - 5, 410, 40))
pygame.draw.rect(WIN, RED, (x, y, 400, 30))
pygame.draw.rect(WIN, YELLOW, (x, y, 400 * ratio, 30))
def drawupdate(pl1, pl2):
WIN.blit(BG, (0, 0))
pl1.draw(WIN)
pl2.draw(WIN)
#WIN.blit(frame_0, (pl1.rect.x,pl1.rect.y))
drawhealth(pl1.health, 20, 20)
drawhealth(pl2.health, 540, 20)
pygame.display.update()
def gameloop():
run = True
clock = pygame.time.Clock()
pl1 = Fighter(200, HEIGHT - 100, PLSCALE)
pl2 = Fighter(700, HEIGHT - 100, PLSCALE)
while run:
clock.tick(FPS)
pl1.move(WIDTH, HEIGHT, pl2)
# pl2.move(WIDTH, HEIGHT, WIN, pl1)
drawupdate(pl1, pl2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
gameloop()
Tried everything but nothing really worked so I’m hopoing for a hand of yours.
New contributor
Lazarus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0