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()
我认为my window
应该是这样的,而不是tuple
请通过这个
myWindow = pygame.display.set_mode((800,600), 0, 32)
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()
那是因为代码中的myWindow
是一个元组,而它应该是:
myWindow = pygame.display.set_mode((800,600), 0, 32)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.