I'm trying to work with pygame and am attempting to create a class like so.
import pygame
from threading import Thread
gameExit = True
class states:
def __init__(self):
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('test')
clock = pygame.time.Clock()
renderThread = Thread(target=self.render, args =(gameDisplay))
updateThread = Thread(target=self.update, args = (clock))
updateThread.start()
renderThread.start()
def update(self, clock):
global gameExit
while gameExit:
print('update')
clock.tick(30)
def render(self, gamedisplay):
global gameExit
print('render')
while gameExit:
print('render')
gameDisplay.fill([255, 255, 255]) # clearing
pygame.display.update()#update
state = states()
the error code is render() argument after * must be an iterable, not pygame.Surface why won't it pass gameDisplay through / how can I do that?
Here's the full traceback:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Marc Frame\AppData\Local\Programs\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Users\Marc Frame\AppData\Local\Programs\Python\Python35\lib\threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: update() argument after * must be an iterable, not Clock
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Marc Frame\AppData\Local\Programs\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Users\Marc Frame\AppData\Local\Programs\Python\Python35\lib\threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: render() argument after * must be an iterable, not pygame.Surface
The issue has to do with the args
you're passing to your Thread
constructors. The expression (gameDisplay)
is a simple reference to the Surface
object bound to gameDisplay
, not a 1-tuple which I suspect you inteded. You need an extra comma at the end of the parentheses to tell Python you really do want a tuple, and are not just using parentheses for order of operations purposes:
renderThread = Thread(target=self.render, args=(gameDisplay,)) # add comma
updateThread = Thread(target=self.update, args=(clock,)) # here too
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With