Python pygame.draw.rect可以';t处理元组中的值和整数之间的操作,但任何其他函数都可以
在我的程序中,我试图找到包含将显示在屏幕上的文本的矩形的坐标。当我尝试使用pygame.draw.rect并从一个rect的坐标中减去一个数字时,我得到一个错误,表示我无法对元组执行操作。然而,当我试着打印时,它没有出现任何错误。代码在解释方面可能会做得更好Python pygame.draw.rect可以';t处理元组中的值和整数之间的操作,但任何其他函数都可以,python,pygame,Python,Pygame,在我的程序中,我试图找到包含将显示在屏幕上的文本的矩形的坐标。当我尝试使用pygame.draw.rect并从一个rect的坐标中减去一个数字时,我得到一个错误,表示我无法对元组执行操作。然而,当我试着打印时,它没有出现任何错误。代码在解释方面可能会做得更好 while True: ### text1, text1_rect = gameFont.render("Start", (255,255,255)) text2, text2_rect =
while True:
###
text1, text1_rect = gameFont.render("Start", (255,255,255))
text2, text2_rect = gameFont.render("Continue", (255,255,255))
text3, text3_rect = gameFont.render("Quit", (255,255,255))
text1_rect.center = (WIDTH/2, 400)
text2_rect.topleft = (text1_rect.x,text1_rect.y + 50)
text3_rect.topleft = (text1_rect.x,text2_rect.y + 50)
###
screen.fill(BLACK) # Scroll right to read message -->
pygame.draw.rect(screen, (255,255,255), (text1_rect.x - 10,text1_rect.center-3,6,6)) # When I say text1_rect.x - 10, it raises an error. If I put this into print(), it returns a value without the error.
screen.blit(text1, text1_rect)
screen.blit(text2, text2_rect)
screen.blit(text3, text3_rect)
pygame.display.update()
这句话就是我所说的:
text1_rect.x - 10
当放入pygame.draw.rect时,出现错误。但是当我打印它时,没有错误。到底发生了什么?您正在将参数放入一个元组中。尝试使用列表代替。 另外,我很确定你需要为线条粗细添加另一个参数 替换
pygame.draw.rect(screen, (255,255,255), (text1_rect.x - 10,text1_rect.center-3,6,6))
与
如果我错了,请纠正我,但我非常确定text1_rect.x是一个整数,对吗?该函数接受多个参数。特定的参数必须有特定的类型,第三个参数应该是一个列表,而不是您创建的元组。我认为当您试图从元组
text1\u rect.center-3
中减去一个整数时,代码不会起作用,但第三个参数是一个rect,不是rect元组?不,第三个参数是一组参数[x,y,宽度,高度]定义矩形的位置和大小。
pygame.draw.rect(screen, (255, 255, 255), [text1_rect.x - 10,text1_rect.center-3,6,6], 2)