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

时间:2017-11-09 12:44:44

标签: python 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()

1 个答案:

答案 0 :(得分:0)

以下是现在的工作版本:

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()

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