Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arguments not being passed through render() argument after * must be an iterable, not pygame.Surface

Tags:

python

class

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
like image 795
Marc Frame Avatar asked Jul 02 '16 22:07

Marc Frame


1 Answers

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
like image 88
Blckknght Avatar answered Nov 13 '22 05:11

Blckknght