Pygame collidepoint()不占blit()

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

对不起,因为我不是英语使用者,所以在语法上有任何错误

我在隔离期间正在研究一款小型游戏,并希望制作地图制作者,以加快设计关卡的过程。我的问题是,即使网格按我期望的方式在屏幕上显示,“ getcollide”功能也不能解决表面的“发白”问题。我花了两天时间,想不出另一种方法来解决这个问题,这里是一个示例代码:

    import pygame

    def run(l, h, fps, scene):
        pygame.init()
        ecran = pygame.display.set_mode((l, h))
        clock = pygame.time.Clock()

        scene_active = scene

        while scene_active != None:
            key = pygame.key.get_pressed()
            filtre_touche = []

            for event in pygame.event.get():
                close = False
                if event.type == pygame.QUIT:
                    close = True
                elif event.type == pygame.KEYDOWN:
                    alt = key[pygame.K_LALT] or touche_active[pygame.K_RALT]
                    if event.key == pygame.K_ESCAPE:
                        close = True
                    elif event.key == pygame.K_F4 and alt:
                        close = True
                if close :
                    scene.quit()
                else :
                    mouse_pos = pygame.mouse.get_pos()
                    filtre_touche.append(event)

            scene_active.traite_input(filtre_touche, key, mouse_pos)
            scene_active.update()
            scene_active.render(ecran)

            scene_active = scene_active.next

            pygame.display.flip()
            clock.tick(fps)

    surface = pygame.Surface((320,320))
    M = []
    for i in range(10):
        x = []
        for j in range(10):
            x.append(pygame.draw.rect(surface, (255,255,255),(i*32, j*32, 32,32), 1))
        M.append(x)
    print(M)

    class FirstScreen():
        def __init__(self):
            self.next = self
        def traite_input(self, evenement, touche, mouse_pos):
            for event in evenement :
                if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                    pygame.quit()
            for line in M :
                for row in line:
                    if row.collidepoint(mouse_pos):
                        print('collide')

        def update (self):
            pass
        def render(self, ecran):
            ecran.fill((200,200,200))
            ecran.blit(surface, (350,250), surface.get_rect())

    run(800,600,60, FirstScreen())

如果运行此命令,将看到表面在我们期望的位置,并且图块实际上是32 x32。但是将鼠标悬停在网格上没有任何效果。如果我将表面的宽度设为0,0,则将鼠标悬停在网格应位于的位置时,将调用print语句。

我想念什么吗?在搜索了该网站,pygame doc和各种论坛之后,我似乎是唯一遇到此麻烦的人,我在做什么错了?

python pygame grid collision blit
1个回答
0
投票

这是因为您将网格放在表面上,使屏幕上的表面变白,因此,当您创建网格时,您将从表面的0开始。然后,当执行collidepoint时,将屏幕中间的表面涂成白色。鼠标悬停在接近整个屏幕左上方的0处。基本上,鼠标在窗口上的位置会平移到表面上,因此,当您将鼠标悬停在网格上时,窗口中鼠标的位置不在表面上的网格上。如果这样的话。

简易修复:

if row.collidepoint((mouse_pos[0] - 350,mouse_pos[1] - 250)):

将鼠标移回原位,因此表面上的0,0为窗口上的0,0

© www.soinside.com 2019 - 2024. All rights reserved.