【问题标题】:How to create a jump movement in pygame [duplicate]如何在pygame中创建跳跃动作[重复]
【发布时间】:2021-04-08 07:43:21
【问题描述】:

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

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

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    切勿尝试在应用程序循环中实现控制游戏的循环。使用应用程序循环。添加变量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 控制每秒帧数,从而控制游戏速度。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多