运行使用边界检查的程序时崩溃 (pygame)



当我运行代码时,窗口会自动崩溃。我似乎找不到任何原因。这是一段非常简单的代码,因此不需要上下文:)

代码如下:

import pygame
size=(0,0)
WHITE=(255,255,255)
BLACK=(0,0,0)
rectX=(0)
rectY=(0)
rectSizeX=(100)
rectSizeY=(100)
screen=pygame.display.set_mode(size)
screen.fill(WHITE)
pygame.display.flip()
while (True):
    pygame.draw.rect(screen, BLACK, [rectX, rectY, rectSizeX, rectSizeY])
    pygame.display.flip()
    mouse_pos = pygame.mouse.get_pos()
    while mouse_pos[0] >= (rectX) and mouse_pos[1] >= (rectY) and mouse_pos[0] <= (rectX+rectSizeX) and mouse_pos[1] <= (rectY+rectSizeY):
        print("meme")
        mouse_pos = pygame.mouse.get_pos()

这是现在的工作版本:

import pygame
import sys
size=(0,0)
WHITE=(255,255,255)
BLACK=(0,0,0)
rectX=(0)
rectY=(0)
rectSizeX=(100)
rectSizeY=(100)
screen=pygame.display.set_mode(size)
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, [rectX, rectY, rectSizeX, rectSizeY])
pygame.display.flip()
mouse_pos = pygame.mouse.get_pos()
while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION:
            mouse_pos = pygame.mouse.get_pos()
            if mouse_pos[0] >= (rectX) and mouse_pos[1] >= (rectY) and mouse_pos[0] <= (rectX+rectSizeX) and mouse_pos[1] <= (rectY+rectSizeY):
                print("meme")
            else:
                print (mouse_pos)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

另请注意,您的窗口不是"无响应",但您无法关闭它,因为您也需要处理QUIT类型事件。

最新更新