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

如何使用Pygame修复代码以正确跳转?

如何解决如何使用Pygame修复代码以正确跳转?

编辑: 我现在更新了缩进和其他一些数字(让玩家从地板开始而不是跳得太高)。现在它跳到了一个很高的高度,但是并没有掉下来回到地板上。有关如何解决此新问题的任何想法?

还有很多其他问题,如果我将player_movement = 2放在其他位置(重置变量),它会缓慢下降,但随后我需要设置一个障碍,使其不会脱离屏幕底部。但是,我已经拥有的代码应该可以为我做...对吗?

更新的代码

if not(isJump):
    if keys[pygame.K_UP]:
        isJump = True
else:
    if jumpCount >= -10:
        neg = 1
        if jumpCount < 0:
            neg = -1
        player_movement -= (jumpCount ** 2) * 0.5 * neg
        jumpCount -= 10
    # This will execute when jump is finished
    else:
        # Resetting Variables
        jumpCount = 10
        isJump = False

我最初开始遵循本教程:https://www.youtube.com/watch?v=UZg49z76cLw (通过制作Flappy Bird学习pygame)。但是我决定我只是希望“玩家”在地板上并且能够跳跃(而不是像飞扬的小鸟一样重力/跳跃)。

因此,我将该教程与https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/结合在一起。在添加跳跃之前,“玩家”必须停在地板上。随着跳转,“玩家”在屏幕顶部闪烁。如何解决此问题,使其正常工作?我尝试查找它,我所能找到的就是如何像在屏幕上绘制一个框一样跳跃,而不是使用screen.blit()。

请参阅下面的代码

import pygame


def draw_floor():
    """Sets one floor after the first"""
    screen.blit(floor_surface,(floor_x_pos,900))
    screen.blit(floor_surface,(floor_x_pos + 576,900))


# Needed to start pygame
pygame.init()
# Creating the screen ((width,height))
screen = pygame.display.set_mode((576,1024))
# Creating FPS
clock = pygame.time.Clock()

# Game variables
isJump = False
jumpCount = 10
player_movement = 0

# Importing background image into game
bg_surface = pygame.image.load('assets/bg_day.png').convert()
# Making background image larger
bg_surface = pygame.transform.scale2x(bg_surface)

# Importing floor image into the game
floor_surface = pygame.image.load('assets/base.png').convert()
# Making floor image larger
floor_surface = pygame.transform.scale2x(floor_surface)
# Floor variable to move floor
floor_x_pos = 0

# Importing player image into the game
player_surface = pygame.image.load('assets/player.png').convert()
# Making player image larger
player_surface = pygame.transform.scale2x(player_surface)
# Make a collision Box around player -- center of rectangle x,y
player_rect = player_surface.get_rect(center=(100,899))

run = True
while run:
    # Checks for all events running
    for event in pygame.event.get():
        # if event type is quitting:
        if event.type == pygame.QUIT:
            # set run to False to close loop
            run = False

    keys = pygame.key.get_pressed()

    if not(isJump):
        if keys[pygame.K_UP]:
            isJump = True
        else:
            if jumpCount >= -10:
                player_movement -= (jumpCount * abs(jumpCount)) * 0.5
                jumpCount -= 1
            # This will execute when jump is finished
            else:
                # Resetting Variables
                jumpCount = 10
                isJump = False

    # Setting the background (screen_bg,(x,y)) : (0,0) = top left
    screen.blit(bg_surface,(0,0))
    player_rect.centery = player_movement
    # Putting player on screen
    screen.blit(player_surface,player_rect)
    # Moving the floor
    floor_x_pos += -1
    # Setting the floor surface to go infinitely
    draw_floor()
    if floor_x_pos <= -576:
        floor_x_pos = 0

    # Draws anything from above in while loop and draws on the screen
    pygame.display.update()
    # Setting FPS - won't run faster than [120] frames per second
    clock.tick(120)

# Quits game
pygame.quit()

解决方法

这是Indentation的问题。 else案例属于if not(isJump):

while run:
    # [...]

    if not(isJump):
        if keys[pygame.K_UP]:
            isJump = True
    
    #<--| INDENTATION
    else:
        if jumpCount >= -10:
            player_movement -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        # This will execute when jump is finished
        else:
            # Resetting Variables
            jumpCount = 10
            isJump = False

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