Pygame how to make the character jump

Asked

Viewed 65 times

3

I’m taking my first steps in pygame, and I’m trying to get the game character to jump, but I stumbled upon one mistake: the character just teleports up and down in 2 frames he did the whole move, and I can’t find an efficient way to make sure there’s a complete animation of a jump... someone can shed some light on me?

Here’s the jump code:

class Luna(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.images = [pygame.image.load(os.path.join("sprites", "luna2.png")).convert_alpha(),
                       pygame.image.load(os.path.join("sprites", "luna.png")).convert_alpha(),
                       pygame.image.load(os.path.join("sprites", "luna3.png")).convert_alpha()]

        self.current_image = 0
        self.image = pygame.image.load(os.path.join("sprites", "luna2.png")).convert_alpha()

        self.rect = self.image.get_rect()
        self.rect[0] = 0
        self.rect[1] = height - 220
        self.jumping = False
        self.gravity = 10
        self.jump_height = 150

    def update(self):
        # updates walking Luna
        self.current_image = (self.current_image + 1) % 3
        self.image = self.images[self.current_image]
        self.image = pygame.transform.scale(self.image, (200, 200))

        if self.jumping:

            self.rect[1] -= 200 # posição no eixo y
            self.jumping = False


        else:
            while self.rect[1] < window_height:
                self.rect[1] += 1

    def jump(self):
        self.jumping = True

def draw that draws the character on the screen:

def draw_window():
    window.blit(background_image, (0, 0))

    ground_group.update()
    ground_group.draw(window)

    luna_group.update()
    luna_group.draw(window)
    pygame.display.update()

and the main pygame loop:

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    luna.jump()

        draw_window()
    pygame.quit()

1 answer

0

I was suffering from the same thing. import pygame from sys import Exit pygame.init() clock = pygame.time.Clock() width, height = 544, 320

Creating the screen

screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('One more game test')

Creating the background

bg = pygame.image.load('images/Parallax-Forest-preview.png') bg_rect = bg.get_rect(topleft=(0, 0))

Creating the hero

heroi = pygame.image.load('images/spellun-Sprite.png'). convert_alpha() heroi_rect = heroi.get_rect(midbottom=(272, 300))

gravity_heroi = 0

Main loop

while True: # Draw the background screen.blit(bg, bg_rect)

# Evento para fechar o jogo
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        exit()

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            gravidade_heroi = -20

gravidade_heroi += 1
heroi_rect.y += gravidade_heroi

if heroi_rect.bottom >= 300:
    heroi_rect.bottom = 300

screen.blit(heroi, heroi_rect)

pygame.display.update()
clock.tick(60)

Note the variable clock at the beginning of the program and the final clock.tick(60). That settled it for me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.