#pygame在Python中:如何制作,让我的Square可以降落在另一个Square曲面上并停留在它上面

问题描述 投票:0回答:1

我是一名新的python pygame学习者,我想学习碰撞

但是我只知道如何进行碰撞检测主要需求如何添加碰撞,使我可以站在我的小方块上,我的小方块可以移动,但我不知道如何使它站立在另一个方块上而不是扔掉它在脚本的底部,我创建了碰撞检测,但无法弄清楚如何使其有效地站立在我的大块上,而不是从侧面和顶部将其扔掉]]

pygame.init()


win = pygame.display.set_mode((500,500))
pygame.display.set_caption("THIS GAME IS KINDA ASS")


# first squrae
x = 50
y = 50
height= 50
width = 50
speed = 5
isJump = False
JumpCount = 10

# square 2
cordx = 100
cordy = 200
cordheight = 100
cordwidth = 100
    # First Square





# main loop
runningame = True
while runningame:
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runningame = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if not(isJump):
        if keys[pygame.K_UP]:
            y -= speed
        if keys[pygame.K_DOWN]:
            y += speed
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if JumpCount >= -10:
            y -= (JumpCount *abs(JumpCount))
            JumpCount -= 1
        else:
            isJump = False
            JumpCount = 10



    win.fill((0,0,0))
# I added a varabial to my player square  
    Player = pygame.draw.rect(win, (115, 235, 174), (x,y, height,width))

    # second square
   # this is the big box and its the enemy square that I want my script to stand on
    Enemy = pygame.draw.rect(win, (202, 185 , 241), (cordx, cordy, cordheight,cordwidth))


    if Player.colliderect(Enemy):
        pygame.draw.rect(win, (140, 101, 211), (50,50,50,50))


    pygame.display.update()

pygame.quit()

# I am just a beginner and I want to learn how pygame works if you privded me with the script please explain how it works to me I have tried to watch other tutorials on youtube but couldn't understand and I thought stackoverflow might help Thank you!

我是一名新的python pygame学习者,我想学习碰撞,但是我只知道如何进行碰撞检测MAIN QUEST如何添加碰撞,使我能够站在我的小方块上的大方块上……

python pygame new-operator
1个回答
0
投票
所以您有Player.colliderect(Enemy),所以您知道它发生碰撞时,您只需要从哪里弄清楚并相应地移动播放器即可。
© www.soinside.com 2019 - 2024. All rights reserved.