【问题标题】:Python Pygame Window Not RespondingPython Pygame 窗口没有响应
【发布时间】: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)

【问题讨论】:

标签: python python-3.x pygame


【解决方案1】:

run 方法中需要一个 while 循环,并将主循环放入一个函数中。

import pygame
import threading
import time


class EventHandler(threading.Thread):

    def run(self):
        while True:
            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

gameInit = pygame.init()

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

gameDisplay = pygame.display.set_mode((800, 600))

def main_loop():
    while True:
        gameDisplay.fill(red)
        pygame.display.update()
        time.sleep(0.4)
        gameDisplay.fill(green)
        pygame.display.update()
        time.sleep(0.4)
        gameDisplay.fill(blue)
        pygame.display.update()
        time.sleep(0.4)

handler = EventHandler()
handler.start()
t = threading.Thread(target=main_loop)
t.start()

但是,在这种情况下确实不需要threading,而且代码对我来说看起来很奇怪。您可以使用pygame.time.get_ticks() 计算经过的时间,然后在超过您的时间限制时更改颜色。如果您想无限循环多个值,itertools.cycle 非常方便。

import itertools

import pygame


pygame.init()

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# An infinite iterator that cycles through these colors.
colors = itertools.cycle((red, green, blue))
color = next(colors)

gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
start_time = 0

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    current_time = pygame.time.get_ticks()
    if current_time - start_time > 500:  # 500 milliseconds.
        color = next(colors)
        start_time = current_time

    gameDisplay.fill(color)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

【讨论】:

    【解决方案2】:

    我建议您的代码运行太快,并增加time.sleep 的值。

    【讨论】:

    • 我一直将 time.sleep(0.1) 更改为 time.sleep(10),但同样的事情发生了。
    • 那时我只能假设您的崩溃是由while True 循环引起的,这会使您的系统过载。
    • 删除 while True 循环后,只需简单地执行 pygame.display.fill(green) 和 pygame.dispaly.update() 之后,程序仍然崩溃。 @josh-dinsdale
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2022-12-12
    相关资源
    最近更新 更多