【问题标题】:Moving the sprite left and righ makes it fall down左右移动精灵使其落下
【发布时间】:2021-02-08 05:42:30
【问题描述】:

您可能认为这是因为重力,但事实并非如此。我尝试将重力设置为 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)

【问题讨论】:

    标签: python python-3.x pygame pygame-surface


    【解决方案1】:

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

    然后一行

     man_rec.centery += mov_of_man
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多