pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((width_display,height_display))
pygame.display.set_caption("Ballzy")
ballImg = pygame.draw.circle(screen,(255,0,0),(250,250),36)
def ball(x,y):
screen.blit(ballImg,(x,y))
我收到一个错误,内容为
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
球函数内。如果球是实际图像,那么这将起作用,但我需要它是在 pygame 中绘制的形状。
最佳答案
Pygame 文档是这样描述 pygame.draw.circle 的:
pygame.draw.circle()
draw a circle around a point
circle(Surface, color, pos, radius, width=0) -> Rect
这意味着该函数返回一个 pygame 矩形。 Pygame 矩形仅存储 x 和 y 坐标以及宽度和高度。这意味着 ballImg 是一个矩形,而不是图像。它不存储任何图像数据,而只是存储一个区域。
您的功能球将 ballImg 传输到屏幕。
def ball(x,y):
screen.blit(ballImg,(x,y))
pygame 文档是这样描述 blit 的:
blit()
draw one image onto another
blit(source, dest, area=None, special_flags = 0) -> Rect
源应该是一个表面,目标应该是图像被传输到的位置。
但是,在您的代码中,您尝试将一个矩形传输到屏幕上,但这不起作用。事实上,当你调用 pygame.draw.circle() 时,你已经将圆绘制到了屏幕上。您根本不需要将任何图像传输到屏幕上。
如果您不是在 ball() 中将 ballImg 传输到屏幕,而是简单地在 x 和 y 坐标处画一个圆,那么您的问题应该得到解决:
def ball(x,y):
pygame.draw.circle(screen, (255,0,0), (x, y), 36)
关于python - 类型错误 : argument 1 must be pygame. 表面,而不是 pygame.Rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48049789/