1

I have been having this issue where when i run my code, the sprites keep jittering even though the screen doesn't force them to do this.

This is where I define the screen size

This is where I update my actions and draw

Here is the part where I load my images from the spritesheet

Any help will be much appreciated.

I tried importing the sprites from the spritesheet onto the game with the animations all working however there are no animations and the sprite continues to jitter up and down

if you would like the code for my fighter class please see below:

def load_images(self, sprite_sheet, animation_steps):
    #extract the certain animation images from spritesheet
    animation_list = []
    for y, animation in enumerate(animation_steps):
        temporary_img_list = []
        for x in range(animation):
            
            temporaryimg = sprite_sheet.subsurface(x , y , self.size, self.size)
            temporary_img_list.append(pygame.transform.scale(temporaryimg, (self.size * self.image_scale, self.image_scale * self.size)))
        
        animation_list.append(temporary_img_list)
    print(animation_list)
    return animation_list

def move(self, screen_base, screen_height, surface, target):
    speed = 10
    gravity = 2
    dx = 0
    dy = 0
    self.running = False
    self.attack_type = 0

    #key-presses recall
    key = pygame.key.get_pressed() = pygame.key.get_pressed()

    #can only happen if no other attack is occuring
    if self.attacking == False:
        
        #movement defined here
        if key[pygame.K_a]:
            dx = -speed
            self.running = True 

        if key[pygame.K_d]:
            dx = speed
            self.running = True
        
        #jump is defined here
        if key[pygame.K_w] and self.jump == False:
            self.vel_y =  -30
            self.jump = True

        #attack is defined here
        if key[pygame.K_k] or key[pygame.K_j]:
            self.attack(surface, target)
            #determine which attack type was used when the key was pressed
            if key[pygame.K_k]:
                self.attack_type = 1
            if key[pygame.K_j]:
                self.attack_type = 2

    #apply gravity for the jumps
    self.vel_y += gravity
    dy += self.vel_y

    #this makes sure that the character does not go off the screen
    if self.rect.left + dx < 0:
        dx = -self.rect.left
    if self.rect.right + dx > screen_base:
        dx = screen_base - self.rect.right
    if self.rect.bottom + dy > screen_height - 207:
        self.vel_y = 0
        self.jump = False
        dy = screen_height - 207 - self.rect.bottom
    
    #ensures the players face each other
    if target.rect.centerx > self.rect.centerx:
        self.flip = False
    else:
        self.flip = True
    
    #apply the attack cooldown to balance the amount of attacks possible
    if self.attack_cooldown > 0:
        self.attack_cooldown -= 1

    #update the player position constantly
    self.rect.x += dx
    self.rect.y += dy

#handle the animation updates
def update(self):

    #check what action the player is currently doing
    if self.health <= 0:
        self.health = 0
        self.alive = False
        self.update_action(6)
    elif self.hit == True:
        self.update_action(5)
    elif self.attacking == True:
        if self.attack_type == 1:
            self.update_action(5) # 3 = Attack
        elif self.attack_type == 2:
            self.update_action(6) # 4 = Attack2

    elif self.jump == True:
        self.update_action(7) # 7 = Jump
        
    elif self.running == True:
        self.update_action(2) # 2 = Run
    
    else:
        self.update_action(0) # 0 = Idle
    
    animation_cooldown = 50

    #update the image
    self.image = self.animation_list[self.action][self.frameindex]
    
    #check if enough time has passed since the last update
    if pygame.time.get_ticks() - self.update_time > animation_cooldown:
        self.frameindex += 1
        self.updatetime = pygame.time.get_ticks()
    
    #check if the animation has finished
    if self.frameindex >= len(self.animation_list[self.action]):
        #if the player is dead end the animation
        if self.alive == False:
            self.frameindex = len(self.animation_list[self.action]) - 1
        else:
            self.frameindex = 0

            #check if an attack was executed
            if self.action == 5 or self.action == 6:
                self.attacking = False
                self.attack_cooldown = 20
            #check if damage was taken
            if self.action == 7:
                self.hit = False
                #if the player was in the middle on an attack, then the attack is stopped
                self.attacking= False
                self.attack_cooldown = 20

def attack(self, surface, target):
    if self.attack_cooldown == 0:
        self.attacking = True
        attacking_rect = pygame.Rect(self.rect.centerx - (2 * self.rect.width * self.flip), self.rect.y, 2 * self.rect.width, self.rect.height)
        if attacking_rect.colliderect(target.rect):
            target.health -= 5
            target.hit = True
        pygame.draw.rect(surface, (0, 255, 0), attacking_rect)

def update_action(self, new_action):
    #check if the new action is different to the previous one
    if new_action != self.action:
        self.action = new_action
        #update the animation settings 
        self.frameindex = 0
        self.update_time = pygame.time.get_ticks()

def draw(self, surface):
    img = pygame.transform.flip(self.image, self.flip, False)
    #pygame.draw.rect(surface,(255, 0, 0), self.rect)
    surface.blit(img, (self.rect.x - (self.offset[0] * self.image_scale), self.rect.y - (self.offset[1] * self.image_scale)))

  • If anyone needs any more information let me know – Sofian Ghailane Dec 05 '22 at 14:23
  • Please provide enough code so others can better understand or reproduce the problem. – user11717481 Dec 05 '22 at 15:07
  • sorry about that, quite new to the platform, that should be all good now right? – Sofian Ghailane Dec 05 '22 at 20:12
  • The problem is not reproducible. Most likely the sprites are not cropped well from the sprite sheet. If there is random spacing on the cropped sprites (for example: the sprites are different sizes or some sprites have a border at the bottom and some sprites have a border at the top), then you get a jittery and wobbly character. However, please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). That is way too much code. I am very sure that the problem can be made reproducible with less code. – Rabbid76 Dec 05 '22 at 21:08

1 Answers1

0

Most likely the sprites are not cropped well from the sprite sheet. If there is random spacing on the cropped sprites (for example: the sprites are different sizes or some sprites have a border at the bottom and some sprites have a border at the top), then you get a jittery and wobbly character.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174