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()