[Python] Pygame: How do I get rid of the jump delay when the enemy does his attack animation +...

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    This is my first post on stackoverflow so I thank you for your patience in advance. I'm creating my first game using Pygame and I am running into an issue where when my enemy jumps after his attack animation, instead of simultaneously. I am trying to make it so that the non-blue enemy jumps at the same time as its attack animation. Currently, the character jumps and then does the animation. However I think I have implemented the gravity wrong because after the first jump, it jumps again but shoots into the sky...

    I am also running into an issue where I am trying to only have one type of enemy have the ability to jump, but both are affected even though I have specified only non-blue enemies to jump.

    I've tried changing the 'if self.image == self.kick' into a while loop, but that crashed the game, I've also tried making the gravity -= 5, which gets rid of the second shooting-up jump but now they aren't jumping high enough.

    any help or advice is appreciated :)

    class Opps(pygame.sprite.Sprite):

    def __init__(self, type):

    super().__init__()

    if type == 'blue':
    b_idle = pygame.image.load('/tiny embers jam/graphics/opps/blue/b_idle.png').convert_alpha()
    b_bounce = pygame.image.load('/tiny embers jam/graphics/opps/blue/b_bounce.png').convert_alpha()
    self.frames = [b_idle,b_bounce,b_idle,b_bounce]
    self.kick = pygame.image.load('/tiny embers jam/graphics/opps/blue/b_kick.png').convert_alpha()
    else:
    p_idle = pygame.image.load('/tiny embers jam/graphics/opps/pink/p_idle.png').convert_alpha()
    p_bounce = pygame.image.load('/tiny embers jam/graphics/opps/pink/p_bounce.png').convert_alpha()
    self.frames = [p_idle,p_bounce,p_idle,p_bounce]
    self.kick = pygame.image.load('/tiny embers jam/graphics/opps/pink/p_jump.png').convert_alpha()

    self.gravity = 0
    self.y_pos = 280
    self.animation_index = 0
    self.frames.append(self.kick)
    self.image = self.frames[self.animation_index]
    self.rect = self.image.get_rect(midbottom = (randint(660,900),self.y_pos))

    def apply_gravity(self):
    self.gravity += 1
    self.rect.y += self.gravity
    if self.rect.bottom >= 280:
    self.rect.bottom = 280

    def animation_state(self):
    self.animation_index += 0.1
    if self.animation_index >= len(self.frames): self.animation_index = 0
    self.image = self.frames[int(self.animation_index)]

    if type != 'blue':
    if self.image == self.kick:
    self.gravity -= 6

    Continue reading...

Compartilhe esta Página