0

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

TTaykAH
  • 35
  • 5
  • 2
    `pygame.event.get()` only works on the main thread; that's your problem. Also, why do you want to use another thread? Usually this is not needed at all. – sloth Mar 28 '20 at 23:20
  • @sloth In General, this is not necessary in my game, but because of this "news" will have to complicate the code. When I found out about this problem, it became more a matter of principle than necessity. In any case thank you for your response. – TTaykAH Mar 29 '20 at 12:47
  • 1
    If you really need threading, you can make it work doing all pygame related stuff on the main thread, like in this [answer](https://stackoverflow.com/questions/20519420/using-threading-in-pygame/20521359#20521359). Maybe a coroutine is what will work for you, like in [this](https://stackoverflow.com/questions/53211801/threading-issue-with-pygame/53221703#53221703) or [this](https://stackoverflow.com/questions/53586802/using-pygame-time-wait-between-display-updates/53589557#53589557) example. Feel free to ping me if you have a specific problem to solve. – sloth Mar 29 '20 at 12:58
  • @sloth Sorry, I've not written so long. First example a little bit wrong for me, but second like magic for me, I've never heard of such a trick before. Thank you for opening up some magic for me :) I'm sure I'll be using this technique for a long time. This will help me simplify my code and make it more readable, even though I couldn't use threads the way I wanted to. – TTaykAH Mar 29 '20 at 17:23

0 Answers0