Python 向左向右移动精灵会使其掉落

Python 向左向右移动精灵会使其掉落,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,你可能认为这是因为重力,但事实并非如此。我试着将重力设置为0,但没有成功。当“人”精灵降落在我放在他下面的平台上,并抓住他时,精灵会完全静止在平台上,但当我左右移动他时,他会随着每次按键而沉入平台,就像流沙一样。请记住,这也发生在半空中,当他在平台上时更容易观察到。这可能是什么原因 import pygame, sys, time, random """ def create_mov_plat(): new_plat = platform.get_rect

你可能认为这是因为重力,但事实并非如此。我试着将重力设置为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\u
设置为
0.1
。按向左或向右键时,会添加
LEFT_mov
RIGHT_mov
,因此现在是
2.1

然后排队

 man_rec.centery += mov_of_man
man\u rect
向下移动该量(四舍五入到2像素)


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

兄弟,我爱你,它终于起作用了。非常感谢。