微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我看过很多视频,但无法将代码实现到我的代码中。我所需要的只是平稳跳跃。我从 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

检查它们是 True 还是 False,然后在下面的 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,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,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,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))

# 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,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,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,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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。