首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame collidepoint()不考虑blit()

Pygame collidepoint()不考虑blit()
EN

Stack Overflow用户
提问于 2020-04-18 08:11:23
回答 1查看 212关注 0票数 0

很抱歉出现语法错误,因为我不会说英语

我正在做一个小游戏,在这个隔离期间,我想做一个地图制作者,以加快设计关卡的过程。我的问题是,即使网格像我预期的那样在屏幕上显示,"getcollide“函数也不能解释曲面的"blit”。我花了两天的时间来解决这个问题,但想不出其他方法来解决这个问题,下面是一个示例代码:

代码语言:javascript
复制
    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 * 32。但是将鼠标悬停在栅格上没有任何效果。当您将鼠标悬停在网格所在的位置时,如果我在0,0处对曲面进行了blit,则将调用print语句。

我是不是漏掉了什么?在搜索了这个网站,pygame文档和各种论坛后,我似乎是唯一一个遇到这种麻烦的人,我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2020-04-18 08:29:12

这是因为你把网格放在一个表面上,在屏幕上对这个表面进行蓝化,所以当你制作网格时,你从表面的0开始。然后,当你做collidepoint的时候,在屏幕的中间(略微)的表面上涂抹一下。当鼠标悬停在接近0的位置时,整个屏幕的左上角。基本上,鼠标在窗口上的位置被平移到曲面上,因此当鼠标悬停在栅格上时,鼠标在窗口中的位置不在曲面上的栅格上。如果这有意义的话。

轻松修复:

代码语言:javascript
复制
if row.collidepoint((mouse_pos[0] - 350,mouse_pos[1] - 250)):

向后移动鼠标位置,使表面上的0,0在窗口上为0,0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61282635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档