简体   繁体   English

如何解决“跳转”问题?

[英]How do I fix my 'Jump'?

I am new to this website and pygame so bear with me. 我是这个网站和pygame的新手,所以请多多包涵。 I am experimenting with pygame and made a simple platformer. 我正在尝试pygame,并做了一个简单的平台游戏。 However, whenever I 'Jump', the block jumps frame by frame, so I have to hold down the spacebar. 但是,每当我“跳转”时,该块都会一帧跳,因此我必须按住空格键。 Any help would be greatly appreciated! 任何帮助将不胜感激! here is my code: 这是我的代码:

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

You are mixing your keyboard event handling with your jump-logic. 您正在将键盘事件处理与跳转逻辑混合在一起。 I've done two things, change the spacebar detection to trigger isjump and handle the jump logic irregardless of whether there is a key pressed or not: 我已经做了两件事,更改空格键检测以触发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()

The line keys = pygame.key.get_pressed() and the whole game logic and drawing code should not be in the event loop ( for event in pygame.event.get(): ), otherwise the code gets executed once per event in the queue, and if no events are in the queue, it won't be executed at all. keys = pygame.key.get_pressed() ,整个游戏逻辑和绘图代码不应位于事件循环中( for event in pygame.event.get():中的事件),否则,代码将在每个事件中执行一次队列中,如果队列中没有事件,则根本不会执行。

You could just dedent keys = pygame.key.get_pressed() and all lines beneath. 您可以将keys = pygame.key.get_pressed()和下面的所有行都keys = pygame.key.get_pressed()

Alternatively, you could check in the event loop if the space key was pressed (with if event.type == pygame.KEYDOWN: ) and then set isjump to True (that means the player will only jump once per keypress). 或者,您可以在事件循环中检查是否按下了空格键( 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()

I also recommend adding a pygame.time.Clock instance and call clock.tick(FPS) to regulate the frame rate. 我还建议添加pygame.time.Clock实例并调用clock.tick(FPS)来调节帧速率。

And I'd rather implement the jumping in this way , with a gravity constant which gets added to the y-velocity each frame. 而且我宁愿以这种方式实现跳跃,每个框架的重力常数都会添加到y速度中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM