左右移动精灵使其掉落

Moving the sprite left and righ makes it fall down

你可能认为这是因为重力,但事实并非如此。我尝试将重力设置为 0,但不行。当“人”精灵降落在我放在他下方的平台上并成功抓住他时,精灵然后在平台上保持完美静止,但当我左右移动他时,他每次都沉入平台像流沙一样按下键。请记住这也发生在半空中,当他在平台上时更容易观察。这可能是什么原因?

import pygame, sys, time, random

"""
def create_mov_plat():
    new_plat = platform.get_rect(midtop = (250, 450))
    return new_plat
"""

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 800))
pygame.display.set_caption('Climbing Man')

#Game elements
gravity = 0.15
mov_of_man = 0
right_mov = 2
left_mov = 2

#background
bg_image = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/bckwall.jpg").convert()
bg_image = pygame.transform.scale(bg_image,(500, 900))

#the ladder
ladder = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/ladder.png").convert_alpha()
ladder = pygame.transform.scale(ladder, (43,206))
ladder_rec = ladder.get_rect(center = (250,600))

#the player
man = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/man.png").convert_alpha()
man = pygame.transform.scale(man, (51, 70))
man_rec = man.get_rect(center = (250,300))

#the starting platform
first_platform = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/platform.png").convert_alpha()
first_platform = pygame.transform.scale(first_platform,(300,60))
first_platform_rec = first_platform.get_rect(center = (250,730))

def check_collison(first_platform):
    if man_rec.colliderect(first_platform_rec):
        global mov_of_man
        mov_of_man = 0.1
        return

"""
#the platforms player moves on
platform = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/platform.jpg").convert_alpha()
platform = pygame.transform.scale(platform,(280,80))
platform_pos = 700
"""

#moving platforms
plat_list = []
PLATMOV = pygame.USEREVENT
pygame.time.set_timer(PLATMOV, 1200)

while True:
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                mov_of_man += right_mov
                man_rec.centerx += mov_of_man
            if event.key == pygame.K_LEFT:
                mov_of_man += left_mov
                man_rec.centerx -= mov_of_man
       
        #if event.type == PLATMOV:
        #plat_list.append(create_plat())
        

    #surfaces
    screen.blit(bg_image,(0,0))
    screen.blit(man, man_rec)
    #screen.blit(ladder, ladder_rec)
    screen.blit(first_platform, (first_platform_rec))
    #screen.blit(platform,(25,platform_pos))
    #platform_pos += 1
    
    mov_of_man += gravity
    man_rec.centery += mov_of_man

    check_collison(first_platform)

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

mov_of_man 在主循环结束时设置为 0.1。当您按 LEFTRIGHT 时,会添加 left_movright_mov,因此现在是 2.1

然后是行

 man_rec.centery += mov_of_man

man_rect 向下移动该数量(四舍五入为 2 像素)。

您应该使用两个变量来跟踪玩家的速度,或者改用向量。