如何在pygame中创建跳跃运动

问题描述

我是 Python 新手,我一直在尝试制作一个简单的游戏,您可以在其中向左、向右移动一个方块并使其跳跃。在过去的几天里,我一直在尝试进行跳跃运动,但没有成功。有人可以帮我吗?

import pygame
pygame.init()

xscreen,yscreen = 1000,500
screen = pygame.display.set_mode((xscreen,yscreen))
pygame.display.set_caption("Pygame")

width,height = 50,50
x,y = 950,200
vel,jumping_points = 10,10

run = True
while run:
    pygame.time.delay(100)

    # Checks if the player wants to quit the game.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # Checks if a key was pressed and moves the square accordingly.
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] and x + width < xscreen:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    # Unfinished jump movement.
    if keys[pygame.K_SPACE] and y > 0:
        while True:
            if jumping_points >= -10:
                y -= (jumping_points ** 2) / 5
                jumping_points -= 1
                animation()
            else:
                jumping_points = 10
                break

    # Updates the screen surface.
    animation()

pygame.quit()

编辑:已删除,对问题不重要。

解决方法

切勿尝试在应用程序循环中实现控制游戏的循环。使用应用程序循环。添加变量 jump = False。当按下 SPACE 时设置变量。使用 KEYDOWN 事件而不是 pygame.key.get_pressed()。在应用程序循环中实现跳转算法,而不是额外的循环:

jump = False

run = True
while run:
    # [...]    

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                jump = True


    # Unfinished jump movement.
    if jump:
        if jumping_points >= -10:
            y -= jumping_points
            jumping_points -= 1
        else:
            jumping_points = 10
            jump = False

    # [...]

此外,您需要更改用于计算跳跃的公式。 (jump_points ** 2) 的结果总是一个正值,玩家永远不会下来:

y -= (jumping_points ** 2) / 5

y -= jumping_points

y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)

使用 pygame.time.Clock 控制每秒帧数,从而控制游戏速度。

tick() 对象的方法 pygame.time.Clock 以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。见pygame.time.Clock.tick()

这个方法应该每帧调用一次。

这意味着循环:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)

每秒运行 60 次。


最小示例:

import pygame
pygame.init()

xscreen,yscreen = 1000,500
screen = pygame.display.set_mode((xscreen,yscreen))
pygame.display.set_caption("Pygame")
clock = pygame.time.Clock()

def animation():
    screen.fill(0)
    pygame.draw.rect(screen,(255,0),(x,y,20,20))
    pygame.display.flip()

width,height = 50,50
x,y = 950,200
vel,jumping_points = 10,10
jump = False


run = True
while run:
    clock.tick(60)

    # Checks if the player wants to quit the game.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                jump = True

    # Checks if a key was pressed and moves the square accordingly.
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] and x + width < xscreen:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel

    # Unfinished jump movement.
    if jump:
        if jumping_points >= -10:
            
            # either
            # y -= jumping_points
            # or
            y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)
            
            jumping_points -= 1
        else:
            jumping_points = 10
            jump = False

    # Updates the screen surface.
    animation()

pygame.quit()