python - 为什么pygame队列不响应它在线程中的迭代?
问题描述
你好。开门见山。我有线程 python 模块的问题。当我创建一个从 Thread 类继承的类时,我认为我的程序正在从一个重载的事件队列中崩溃,即使我正在run
线程方法中遍历所有这些队列。但如果我通过pygame.event.get()
主线程,一切正常。例如我写了一个简单的“游戏”。
有用:
import pygame
import threading as tg
import sys
screen = pygame.display.set_mode((300, 300))
screen.fill((155, 155, 155))
ball = pygame.Surface((10, 10))
ball.fill((100, 200, 100))
x = 80
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x += 1
screen.blit(ball, (x, 40))
pygame.display.flip()
不工作:
import pygame
import threading as tg
import sys
class SimpleThread(tg.Thread):
def __init__(self, x):
tg.Thread.__init__(self)
self.run1 = True
self.x = x
def run(self):
while self.run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.run1 = False
break
if event.type == pygame.MOUSEBUTTONDOWN:
self.x += 1
screen = pygame.display.set_mode((300, 300))
screen.fill((155, 155, 155))
ball = pygame.Surface((10, 10))
ball.fill((100, 200, 100))
x = 80
th = SimpleThread(x)
th.start()
while th.run:
screen.blit(ball, (th.x, 40))
pygame.display.flip()
pygame.quit()
sys.exit()
在我看来,它应该以相同的方式工作,但事实并非如此。
我认为这与线程中的命名空间有关,但希望您能帮助我理解这个问题。感谢您阅读此内容:o
解决方案
推荐阅读
- flask - 使用 OAuth 从 Microsoft Graph 显示用户配置文件图片
- html - 请告知如何正确设置背景图像
- javascript - 如何计算javascript中对象数组中存在的键?
- python - HTTPError:500 在尝试使用 Django 框架(python 程序)中的 requests.post() 发布数据时
- android - 我无法从一个活动移动到另一个活动(从点击注册到注册表单)
- sql - 更改 csv 文件 'a' 中的 id 并将 csv 文件 'b' 列中所有出现的旧 csv 文件 'a' id 更改为新 id
- c++ - 如何在c ++中的类构造函数中将整个数组初始化为单个元素
- java - 如何从 SOAP 标头中删除 cxf 'tmu:Timestamp'
- python - 为什么我的函数和打印语句不起作用
- javascript - 如何在 React Bootstrap 中使用自定义元素作为下拉按钮