首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pygame窗口在使用线程时冻结

Pygame窗口在使用线程时冻结
EN

Stack Overflow用户
提问于 2019-04-29 01:09:29
回答 1查看 301关注 0票数 0

我有一个pygame脚本,它以白屏启动,然后在用户键入内容时转换为黑屏。用户输入由另一个线程处理,我使用queue.Queue将消息从输入线程传递到PyGame线程。

问题是,每当我运行脚本时,pygame窗口都会在一段时间后冻结。如果我快速输入某些内容,屏幕将从白色变为黑色,但窗口仍然冻结。我不确定脚本在哪里卡住了?

代码语言:javascript
复制
import pygame
import threading 
import queue

q = queue.Queue()

pygame.init()

#rgb codes 
black = (0, 0, 0)
white = (255, 255, 255)

game_display = pygame.display.set_mode((800, 800))

def screen_1():

    crashed = False

    #holds messages from input thread
    msg = ''

    game_display.fill(white)

    while not crashed:

        #check if there are any messages in the queue
        try:
            msg = q.get(False)
        except queue.Empty:
            pass

        if msg:
            return screen_2()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True        

        pygame.display.update()

def screen_2():

    crashed = False

    game_display.fill(black)

    while not crashed:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True

        pygame.display.update()        

def inputFunc():
    msg = input('Type something:\n')
    q.put(msg)

t1 = threading.Thread(target = screen_1)
t2 = threading.Thread(target = inputFunc)

t1.start()
t2.start()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-29 01:20:00

好吧,我刚刚发现在线程中运行pygam位会导致窗口冻结。如果我只为inputFunc创建一个线程并调用screen_1,一切都会正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55892684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档