Hello. Straight to the point. I have problems with threading python module. When I create a class that inherits from the Thread class, I think, my program is breaking down from an overloaded event queue, even though I'm going through all this queue in the run thread method. But if I go through pygame.event.get() in the main thread, all works. For example I wrote a simple "Game".
It Works:
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()
Don't works:
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()
In my mind, it should work the same way, but it doesn't.
I think this is related to the namespace in the thread, but I hope that you can help me understand this issue. Thanks that you read this :o