使用我的程序,我试图找到包含将在屏幕上显示的文本的Rect的坐标。当我尝试使用pygame.draw.rect并从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时出错。但是当我打印时,没有错误。到底是怎么回事?
答案 0 :(得分:1)
您正在将参数放入元组。尝试改用列表。 另外,我很确定您需要为线宽添加另一个参数。
替换
pygame.draw.rect(screen, (255,255,255), (text1_rect.x - 10,text1_rect.center-3,6,6))
使用
pygame.draw.rect(screen, (255, 255, 255), [text1_rect.x - 10,text1_rect.center-3,6,6], 2)