首页 > 解决方案 > Pygame窗口一直冻结

问题描述

我正在 pygame 中制作一个小程序,但它一直冻结并说“没有响应”,即使程序仍在运行并调用 pygame.draw 函数。

 if timer==0:
    mouseMoved=False
    import ctypes
    ctypes.windll.user32.SetProcessDPIAware()
    true_res = (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
    disp = pygame.display.set_mode(true_res,pygame.FULLSCREEN)
    del ctypes
    color=(0,0,0)
    pos=[50,60]
    pygame.Surface.fill(disp, (255,255,255))
    #mouse.move(mPos[0], mPos[1])
    firstMove=True
    x, y = pygame.display.get_surface().get_size()
    while not mouseMoved:
        #mouse.move(mPos[0], mPos[1])
        mouseMoved=False
        pMouse=mPos
        #print(pMouse)
        mPos=mouse.get_position()
        #print(pos)
        if pos[0]+4>=x:
            horiz=-1
            color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        elif pos[0]-4<=0:
            horiz=1
            color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        if pos[1]-4<=0:
            vert=1
            color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        elif pos[1]+4>=y:
            vert=-1
            color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        pos[0]+=horiz
        pos[1]+=vert
        #pygame.Surface.fill(disp, (250,250,250))
        pygame.draw.circle(disp, color, (pos[0], pos[1]), 9)
        pygame.display.update()
        #time.sleep(0.01)
        if not pMouse==mPos:
            if firstMove:
                firstMove=False
            else:
                timer = 360
                mouseMoved=True
                #pygame.display.iconify()
                #disp = pygame.display.set_mode((200,200))
                pygame.display.quit()
                #pygame.display.update()
                print(f"moved from {mPos} to {pMouse}")

这个东西应该一直运行,直到鼠标移开显示器。

标签: pythonpython-3.xpygame

解决方案


在您的主 while 循环中,添加类似这样的内容,它应该允许调整屏幕大小和移动屏幕。这取决于您如何设置原始屏幕,我们无法从您的代码示例中看到或运行。

for event in pg.event.get():
    if event.type == pg.QUIT:
        pg.quit()
        quit()

    elif event.type == pg.VIDEORESIZE:
        self.height = event.h
        self.width = event.w
        self.screen = pg.display.set_mode((self.width, self.height), pg.RESIZABLE)

推荐阅读