两幅运动图像之间的碰撞

2024-10-02 16:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让它在两个图像相互碰撞时退出。我希望不用上课也能做到,因为我对它们的工作原理仍然感到困惑

下面是它们和主循环的函数

def Good(type, x,y):
    screen.blit(type,(x,y))

def boulder(x,y):
    screen.blit(rock,(x,y))

def main_loop():
    good_y = 500
    good_x = 336
    change = 0
    boulder_y = 0
    boulder_x = random.randint(64, 736)


    done = False


    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    change = -10
                if event.key == pygame.K_d:
                    change = 10
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    change = 0
        good_x += change
        screen.fill(white)
        Good(neutral,good_x,good_y)
        boulder(boulder_x,boulder_y)
        pygame.display.update()
        clock.tick(60)


        if good_x > w - sprite_size or good_x < 0:
            done = True
        boulder_y = boulder_y + boulder_fall
        if boulder_y > h:
            boulder_x = random.randint(64, 736)
            boulder_y = 0

Tags: keyeventifdeftyperandomchangescreen