如何解决“跳转”问题?

我是这个网站和 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()


紫衣仙女
浏览 155回答 2
2回答

慕村225694

您将键盘事件处理与跳转逻辑混合在一起。我做了两件事,改变空格键检测来触发isjump和处理跳转逻辑,不管是否有按键按下:import pygamepygame.init()win = pygame.display.set_mode((500, 500))x = 0y = 490width = 10height = 10vel = 5pygame.key.set_repeat(1)isjump = Falsejumpcount = 10while True:&nbsp; &nbsp; pygame.time.delay(30)&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; keys = pygame.key.get_pressed()&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_a] and x>vel-5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x -= vel&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_d] and x < 500 - width:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += vel&nbsp; &nbsp; &nbsp; &nbsp; # if we're not already jumping, check if space is pressed to start a jump&nbsp; &nbsp; &nbsp; &nbsp; if not isjump and keys[pygame.K_SPACE]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isjump = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jumpcount = 10&nbsp; &nbsp; # if we're supposed to jump, handle the jump logic&nbsp; &nbsp; if isjump:&nbsp; &nbsp; &nbsp; &nbsp; if jumpcount >= -10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neg = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if jumpcount < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neg = -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y -= (jumpcount ** 2) /2 * neg&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jumpcount -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isjump = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jumpcount = 10&nbsp; &nbsp; win.fill((0, 0, 0))&nbsp; &nbsp; pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))&nbsp; &nbsp; pygame.display.update()pygame.quit()

蛊毒传说

线条keys = pygame.key.get_pressed()和整个游戏逻辑和绘图代码不应在事件循环(for event in pygame.event.get():)中,否则代码会在队列中的每个事件中执行一次,如果队列中没有事件,则根本不会执行。您可以将其keys = pygame.key.get_pressed()下方的所有行缩成凹痕。或者,您可以在事件循环中检查是否按下了空格键(使用if event.type == pygame.KEYDOWN:)然后设置isjump为True(这意味着玩家每次按键只会跳跃一次)。import pygamepygame.init()win = pygame.display.set_mode((500, 500))clock = pygame.time.Clock()x = 0y = 490width = 10height = 10vel = 5isjump = Falsejumpcount = 10running = Truewhile running:&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = False&nbsp; &nbsp; &nbsp; &nbsp; elif event.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.key == pygame.K_SPACE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isjump = True&nbsp; &nbsp; keys = pygame.key.get_pressed()&nbsp; &nbsp; if keys[pygame.K_a] and x > vel - 5:&nbsp; &nbsp; &nbsp; &nbsp; x -= vel&nbsp; &nbsp; elif keys[pygame.K_d] and x < 500 - width:&nbsp; &nbsp; &nbsp; &nbsp; x += vel&nbsp; &nbsp; if isjump:&nbsp; &nbsp; &nbsp; &nbsp; if jumpcount >= -10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neg = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if jumpcount < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neg = -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y -= jumpcount**2 / 2 * neg&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jumpcount -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isjump = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jumpcount = 10&nbsp; &nbsp; win.fill((0, 0, 0))&nbsp; &nbsp; pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))&nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; clock.tick(30)pygame.quit()我还建议添加一个pygame.time.Clock实例并调用clock.tick(FPS)来调节帧速率。而且我宁愿以这种方式实现跳跃,每个重力帧的重力常数都会添加到y速度中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python