简体   繁体   English

类型错误:必须是 pygame.Surface,而不是元组。 Python/Pygame 菜鸟

[英]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.

相关问题 “TypeError: must be pygame.Surface, not tuple”怎么办? (pygame 启动器) - "TypeError: must be pygame.Surface, not tuple" what to do? (pygame starter) Pygame 类型错误:参数 1 必须 > pygame.Surface,而不是 pygame.Rect - Pygame TypeError: argument 1 must be > pygame.Surface, not pygame.Rect TypeError:参数1必须是pygame.Surface,而不是str - TypeError: argument 1 must be pygame.Surface, not str TypeError:参数1必须是pygame.Surface,而不是方法 - TypeError: argument 1 must be pygame.Surface, not method 类型错误:参数 1 必须是 pygame.Surface,而不是类型 - TypeError: argument 1 must be pygame.Surface, not type TypeError:必须是pygame.Surface,而不是None - TypeError: must be pygame.Surface, not None TypeError:参数1必须是pygame.Surface,而不是pygame.Rect - TypeError: argument 1 must be pygame.Surface, not pygame.Rect TypeError:参数1必须是pygame.Surface,而不是str [使用按钮的Pygame] - TypeError: argument 1 must be pygame.Surface, not str [Pygame using buttons] Pygame - TypeError:必须是 pygame.Surface,而不是在翻转图像时列出 - Pygame - TypeError: must be pygame.Surface, not list when flipping an image Pygame 错误“类型错误:参数 1 必须是 pygame.Surface,而不是列表” - Pygame error 'TypeError: argument 1 must be pygame.Surface, not list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM