左右移动精灵会使其下落



你可能认为这是因为重力,但事实并非如此。我试过把重力设为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。当你按时,left_movright_mov被添加,所以现在是2.1

然后是

man_rec.centery += mov_of_man

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

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

最新更新