gpt4 book ai didi

python - * 之后未通过 render() 参数传递的参数必须是可迭代的,而不是 pygame.Surface

转载 作者:行者123 更新时间:2023-11-28 20:04:02 25 4
gpt4 key购买 nike

我正在尝试使用 pygame 并试图创建一个这样的类。

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()

错误代码是 * 之后的 render() 参数必须是可迭代的,而不是 pygame.Surface 为什么它不能通过 gameDisplay/我该怎么做?

这是完整的回溯:

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

最佳答案

问题与您传递给 Thread 构造函数的 args 有关。表达式 (gameDisplay) 是对绑定(bind)到 gameDisplaySurface 对象的简单引用,而不是我怀疑您输入的 1 元组。您需要在括号末尾添加一个额外的逗号来告诉 Python 您确实需要一个元组,而不仅仅是为了操作顺序而使用括号:

renderThread = Thread(target=self.render, args=(gameDisplay,))  # add comma
updateThread = Thread(target=self.update, args=(clock,)) # here too

关于python - * 之后未通过 render() 参数传递的参数必须是可迭代的,而不是 pygame.Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com