【问题标题】:How do I fix my code with Pygame to jump correctly?如何使用 Pygame 修复我的代码以正确跳转?
【发布时间】:2020-09-25 13:40:47
【问题描述】:

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

如果我把 player_movement = 2 放在最后一个 else 中(重置变量),它会慢慢下降,但我需要设置一个障碍,这样它就不会离开屏幕底部。但是,我已经拥有的代码应该可以为我做到这一点……对吧?

更新代码:

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

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

    这是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
    

    【讨论】:

    • 非常感谢!我修复了它,但现在有一个新问题。你能看看我对我的帖子所做的编辑,看看你是否能提供更多帮助吗?你太棒了:)
    • 如果我像教程中那样把它设为 1,它就会飞出屏幕:/
    • 哦,我做到了!飞扬的鸟教程将它作为 += 所以我改变了它。我现在做到了=它似乎可以工作,除了播放器现在位于屏幕顶部。我应该可以用 player_rect = player_surface.get_rect(center=(100, 1077)) 这样的东西来改变它,对吧?
    • 是的,但无论我将高度设置为多少,它仍然显示在屏幕顶部
    • @Cutecat42 我看不到您的实际代码。不管怎样,如果你还做player_rect.centery = player_movement,你得把player_movement的初始值改一下。
    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 2013-09-22
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多