我在 pygame 中创建了一个没有动画的基本平台游戏。我已经为左右移动使用了事件处理,但我无法添加跳跃机制

I've created a basic platformer in pygame with no animations. I have used event handling for right and left movement but i cant add a jump mechanic

我看了很多视频,但无法将代码实现到我的代码中。我需要的只是平稳的跳跃。我从 DaFluffyPotato 的视频教程中编写了大部分内容。我刚刚开始 OOP,所以如果没有太多 类 之类的东西就好了!

请阅读代码中的注释

# I have a player image and with the a and d keys, I can move left and right.
# The w key is for jumping
# I've also implemented Gravity on the Y axis.


import pygame
import sys
pygame.init()
clock = pygame.time.Clock()

# window_width
ww = 760
# window_height
wh = 520
# window_color
wc = (135, 206, 250)
# window
w = pygame.display.set_mode((ww, wh))

tile_size = 40

# loading and scaling images
player_img = pygame.image.load('player.png')
player_img = pygame.transform.scale(player_img, (40, 40))


# global variables for movement
# the 'p's stand for player
# X and Y axis for player
pcoors = [400, 300]
pleft = False
pright = False
pjump = False
pspeed = 2
gravity = 1.2

检查它们是真还是假,然后在下面的player_movement函数中实现移动

def userInput():
    # you cannot alter global variables from a local scope
    global pleft, pright, pjump
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # keyPresses
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pleft = True

            if event.key == pygame.K_d:
                pright = True

            if pjump == False and event.key == pygame.K_SPACE and jump_offset == 0:
                pjump = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pleft = False

            if event.key == pygame.K_d:
                pright = False



def player_movement():
    global pleft, pright, pjump, pcoors, pspeed, gravity

    if pleft:
        pcoors[0] -= pspeed

    if pright:
        pcoors[0] += pspeed

    pcoors[1] += gravity

    

def col_detection():
    global pcoors
    
    if pcoors[1] + 40 >= wh:
        pcoors[1] = wh - 40



def img_blit():
    w.blit(player_img, (pcoors))
    


while True:
    w.fill(wc)

    # calling the functions to keep the while loop clean
    userInput()
    player_movement()
    img_blit()
    col_detection()

    clock.tick(360)
    pygame.display.update()

为玩家的垂直加速度和移动添加变量:

acc_y = 0
vel_y = 0

加速度由每帧中的重力设置。当按下 space> 并且玩家跳跃时,加速度改变:

def userInput():
    # you cannot alter global variables from a local scope
    global pleft, pright, pjump, acc_y
    
    acc_y = gravity
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # keyPresses
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pleft = True
            if event.key == pygame.K_d:
                pright = True

            if pjump == False and event.key == pygame.K_SPACE and not pjump:
                pjump = True
                acc_y = -15   # <---

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pleft = False
            if event.key == pygame.K_d:
                pright = False

根据加速度改变速度,根据每一帧的速度改变y坐标:

def player_movement():
    global pleft, pright, pjump, pcoors, pspeed, vel_y, acc_y

    if pleft:
        pcoors[0] -= pspeed
    if pright:
        pcoors[0] += pspeed

    vel_y += acc_y
    pcoors[1] += vel_y

玩家落地时跳跃结束:

def col_detection():
    global pcoors, pjump, vel_y
    
    if pcoors[1] + 40 >= wh:
        pcoors[1] = wh - 40
        pjump = False
        vel_y = 0

您的帧率非常高 (clock.tick(360))。我建议降低帧率,但提高播放器的速度:

pspeed = 10
clock.tick(60)

完整示例

import pygame
import sys
pygame.init()
clock = pygame.time.Clock()

# window_width
ww = 760
# window_height
wh = 520
# window_color
wc = (135, 206, 250)
# window
w = pygame.display.set_mode((ww, wh))

tile_size = 40

# loading and scaling images
try:
    player_img = pygame.image.load('player.png')
    player_img = pygame.transform.scale(player_img, (40, 40))
except:
    player_img = pygame.Surface((40, 40))
    player_img.fill((255, 0, 0))

# global variables for movement
# the 'p's stand for player
# X and Y axis for player
pcoors = [400, 300]
pleft = False
pright = False
pjump = False
pspeed = 10
gravity = 1.2
acc_y = 0
vel_y = 0

def userInput():
    # you cannot alter global variables from a local scope
    global pleft, pright, pjump, acc_y
    
    acc_y = gravity
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # keyPresses
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pleft = True
            if event.key == pygame.K_d:
                pright = True

            if pjump == False and event.key == pygame.K_SPACE and not pjump:
                pjump = True
                acc_y = -20

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pleft = False
            if event.key == pygame.K_d:
                pright = False


def player_movement():
    global pleft, pright, pjump, pcoors, pspeed, vel_y, acc_y

    if pleft:
        pcoors[0] -= pspeed
    if pright:
        pcoors[0] += pspeed

    vel_y += acc_y
    pcoors[1] += vel_y
    
def col_detection():
    global pcoors, pjump, vel_y
    
    if pcoors[1] + 40 >= wh:
        pcoors[1] = wh - 40
        pjump = False
        vel_y = 0

def img_blit():
    w.blit(player_img, (pcoors))
    
while True:
    w.fill(wc)
    userInput()
    player_movement()
    col_detection()
    img_blit()
    pygame.display.update()
    clock.tick(60)