【问题标题】:Pygame character jump speed problems [duplicate]Pygame角色跳跃速度问题[重复]
【发布时间】:2021-06-06 12:51:14
【问题描述】:

我目前正在尝试制作我的第一款游戏。我试图让我的角色跳跃,但我的代码没有错误,但是当我的角色跳跃时,它碰巧很快。不知道改哪个部分。因为我还在学习,所以我自己无法解决这个问题。这是我的代码:

import pygame

pygame.init()

screen = pygame.display.set_mode((1200, 600))
WinHeight = 600
WinWidth = 1200

# player
player = pygame.image.load("alien.png")
x = 50
y = 450
vel = 0.3
playerSize = 32

# title
pygame.display.set_caption("First Game")

# Jump
isJump = False
jumpCount = 10

running = True

while running:
    screen.fill((255, 255, 255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            break
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a] and x > vel:
        x -= vel
    if keys[pygame.K_d] and x < WinWidth - vel - playerSize:
        x += vel
    if not (isJump):
        if keys[pygame.K_w] and y > vel:
            y -= vel
        if keys[pygame.K_s] and y < WinHeight - vel - playerSize:
            y += vel
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            neg = 1
            if jumpCount < 0:
                neg = -1
            y -= (jumpCount ** 2) * 0.5 * neg
            jumpCount -= 1
        else:
            isJump = False
            jumpCount = 10
    screen.blit(player, (x, y))

    pygame.display.update()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

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

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

    这意味着循环:

    clock = pygame.time.Clock()
    running = True
    while running:
        
        clock.tick(60)
    
        # [...]
    

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2023-03-09
      相关资源
      最近更新 更多