问题描述
这是我的代码,它真的很简单:
Here is my code, it is really simple:
bif="C:\\Users\\Andrew\\Pictures\\pygame pictures\\Background(black big).png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((1000,1000),0,32)
background=pygame.image.load(bif).convert()
line=pygame.draw.line(background, (255,255,255), (30,31), (20,31))
while True:
mousex, mousey = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
screen.blit(line,(mousex,mousey))
pygame.display.update()
这样做的目的是让线条在屏幕上跟随我的鼠标.但是,每当我尝试运行该程序时,都会出现错误:
The goal of this is to have the line follow my mouse on the screen. However, whenever I try to run the program I get the error :
TypeError: 参数 1 必须是 pygame.Surface,而不是 pygame.Rect
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
这是什么意思,我做错了什么?
What does this mean, and what am I doing wrong?
推荐答案
该错误意味着它期望的是表面而不是矩形.这是违规行:
The error means that it is expecting a surface not a rectangle. This is the offending line:
screen.blit(line,(mousex,mousey))
问题在于您将 draw.line() 的结果分配给 line.
The problem is that you are assigning the result of draw.line() to line.
line=pygame.draw.line(background, (255,255,255), (30,31), (20,31))
来自 pygame 文档:
From the pygame docs:
所有绘图函数都尊重 Surface 的裁剪区域,并且将被限制在该区域.函数返回一个矩形表示改变像素的边界区域.
所以您要为线分配一个矩形.不确定您到底要做什么,但您可以通过以下方式绘制跟随鼠标的线:
So you are assigning a rectangle to line. Not sure exactly what you want to do, but you can draw the line following the mouse in the following way:
pygame.draw.line(background, (255,255,255), (x,y), (x,y+10))
这篇关于这个错误是什么意思:TypeError: argument 1 must be pygame.Surface, not pygame.Rect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!