TypeError: must be pygame.Surface, not tuple. Python/Pygame noobie

允我心安 提交于 2021-02-05 07:49:26

问题


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

回答1:


i think my window should be like this not tuple please go through this

myWindow = pygame.display.set_mode((800,600), 0, 32)



回答2:


The problem is, that myWindow is just a tuple, not a surface. Pygame cannot draw anything onto it. 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.

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



回答3:


That's because myWindow in your code is a tuple, while it should be:

myWindow = pygame.display.set_mode((800,600), 0, 32)


来源:https://stackoverflow.com/questions/24821483/typeerror-must-be-pygame-surface-not-tuple-python-pygame-noobie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!