[英]TypeError: must be pygame.Surface, not tuple. Python/Pygame noobie
Hello stack overflow users!堆栈溢出用户您好!
I have written this code and it programs to draw lines whenever you click on the pygame screen, but when I run the program, I get an error message saying "TypeError: must be pygame.Surface, not tuple."
我已经编写了这段代码,它会在您点击 pygame 屏幕时绘制线条,但是当我运行该程序时,我收到一条错误消息,指出
"TypeError: must be pygame.Surface, not tuple."
. . I attempted to mess around with the program, but i had no luck.
我试图弄乱程序,但我没有运气。 I would greatly appreciate it if anyone can help me figure out what's wrong with the code.
如果有人能帮我弄清楚代码有什么问题,我将不胜感激。 Thanks!
谢谢! The code is on the bottom.
代码在底部。 Thanks again!
再次感谢!
import pygame
pygame.init()
pygame.display.set_mode((800,600), 0, 32)
myWindow = ((800,600),0,32)
color = (200,155,64)
points = []
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
points.append(event.pos)
if len(points) > 1:
pygame.draw.line(myWindow, color, False, points, 5)
pygame.display.update()
我认为my window
应该是这样的,而不是tuple
请通过这个
myWindow = pygame.display.set_mode((800,600), 0, 32)
The problem is, that myWindow is just a tuple, not a surface.问题是, myWindow 只是一个元组,而不是一个表面。 Pygame cannot draw anything onto it.
Pygame 无法在其上绘制任何内容。 You must either make myWindow the display, then you can draw onto it, or you initialise another display and make myWindow a mere Surface, then it works too.
您必须使 myWindow 成为显示,然后您可以在其上绘图,或者您初始化另一个显示并使 myWindow 仅成为 Surface,然后它也可以工作。
First approach:第一种方法:
myWindow = pygame.display.set_mode((800, 600), 0, 32)
# Rest of the code goes here
pygame.draw.line(myWindow, color, False, points, 5)
pygame.display.update()
Second approach:第二种方法:
screen = pygame.display.set_mode((800, 600), 0, 32)
myWindow = pygame.Surface((800, 600)).convert()
#Rest of the code goes here
screen.blit(myWindow, (0, 0))
pygame.display.update()
那是因为代码中的myWindow
是一个元组,而它应该是:
myWindow = pygame.display.set_mode((800,600), 0, 32)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.