如何解决“跳转”问题?

时间:2018-08-22 08:14:03

标签: python pygame

我是这个网站和pygame的新手,所以请耐心等待。我正在尝试pygame,并做了一个简单的平台游戏。但是,每当我“跳转”时,该块都会一帧跳,因此我必须按住空格键。任何帮助将不胜感激! 这是我的代码:

import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 0
y = 490
width = 10
height = 10
vel = 5
pygame.key.set_repeat(1)
isjump = False
jumpcount = 10
while True:
    pygame.time.delay(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and x>vel-5:
            x -= vel
        if keys[pygame.K_d] and x < 500 - width:
            x += vel
        if not(isjump):
            if keys[pygame.K_SPACE]:
                isjump = True
        else:
            if jumpcount >= -10:
                neg = 1
                if jumpcount < 0:
                    neg = -1
                y -= (jumpcount ** 2) /2 * neg
                jumpcount -= 1
            else:
                isjump = False
                jumpcount = 10
        win.fill((0, 0, 0))
        pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
        pygame.display.update()
pygame.quit()

2 个答案:

答案 0 :(得分:2)

您正在将键盘事件处理与跳转逻辑混合在一起。我已经做了两件事,将空格键检测更改为触发isjump并处理跳转逻辑,而不管是否按下任何键:

import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 0
y = 490
width = 10
height = 10
vel = 5
pygame.key.set_repeat(1)
isjump = False
jumpcount = 10
while True:
    pygame.time.delay(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and x>vel-5:
            x -= vel
        if keys[pygame.K_d] and x < 500 - width:
            x += vel
        # if we're not already jumping, check if space is pressed to start a jump
        if not isjump and keys[pygame.K_SPACE]:
            isjump = True
            jumpcount = 10
    # if we're supposed to jump, handle the jump logic
    if isjump:
        if jumpcount >= -10:
            neg = 1
            if jumpcount < 0:
                neg = -1
            y -= (jumpcount ** 2) /2 * neg
            jumpcount -= 1
        else:
            isjump = False
            jumpcount = 10
    win.fill((0, 0, 0))
    pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
    pygame.display.update()
pygame.quit()

答案 1 :(得分:1)

keys = pygame.key.get_pressed()行以及整个游戏逻辑和绘图代码不应位于事件循环(for event in pygame.event.get():)中,否则该代码将在队列中的每个事件中执行一次,如果没有事件发生,在队列中,它根本不会执行。

您可以使keys = pygame.key.get_pressed()和下面的所有行都凹陷。

或者,您可以在事件循环中检查是否按下了空格键(使用if event.type == pygame.KEYDOWN:),然后将isjump设置为True(这意味着播放器每次跳动一次)键)。

import pygame


pygame.init()
win = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
x = 0
y = 490
width = 10
height = 10
vel = 5
isjump = False
jumpcount = 10

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                isjump = True

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a] and x > vel - 5:
        x -= vel
    elif keys[pygame.K_d] and x < 500 - width:
        x += vel

    if isjump:
        if jumpcount >= -10:
            neg = 1
            if jumpcount < 0:
                neg = -1
            y -= jumpcount**2 / 2 * neg
            jumpcount -= 1
        else:
            isjump = False
            jumpcount = 10

    win.fill((0, 0, 0))
    pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
    pygame.display.update()
    clock.tick(30)

pygame.quit()

我还建议添加一个pygame.time.Clock实例并调用clock.tick(FPS)来调节帧速率。

我宁愿在this way中实现跳跃,并使用重力常数添加到每帧的y速度中。