【发布时间】:2017-10-08 21:03:03
【问题描述】:
我正在尝试在 Python 3.6 中编写一个程序,借助 pygame(一个 Python 模块),它应该在屏幕上快速闪烁红色、绿色和蓝色。程序按预期运行大约 10 到 15 秒,然后停止响应。 (我注意到只有 3 个事件被打印到控制台,而应该还有更多。)
import pygame
import threading
import time
'''
IMPORTS ARE ABOVE THIS LINE
'''
class EventHandler(threading.Thread):
def run(self):
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
'''
CLASSES ARE ABOVE THIS LINE
'''
# Initializer
gameInit = pygame.init()
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# Setup Crap
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame Colors")
# Event Handler
handler = EventHandler()
handler.start()
# Game Loop
while True:
gameDisplay.fill(red)
pygame.display.update()
time.sleep(0.1)
gameDisplay.fill(green)
pygame.display.update()
time.sleep(0.1)
gameDisplay.fill(blue)
pygame.display.update()
time.sleep(0.1)
【问题讨论】:
-
您需要调用其中一个事件函数regularly in the thread that initialize the video system。如果您不定期处理事件,请使用操作系统thinks the program has become unresponsive/crashed。
标签: python python-3.x pygame