TypeError:参数1必须是pygame.Surface,而不是pygame.Rect

时间:2018-01-01 12:49:47

标签: python pygame

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中绘制的形状。

1 个答案:

答案 0 :(得分:3)

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是一个矩形,而不是一个图像。它不存储任何图像数据,而只存储区域。

你的功能球将球打到屏幕上。

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

源应该是一个曲面,而dest应该是图像blit的位置。

但是,在您的代码中,您尝试将一个矩形blit到屏幕上,这不起作用。实际上,当你调用pygame.draw.circle()时,你已经将圆圈绘制到了屏幕上。您根本不需要将任何图像blit到屏幕上。

如果没有将球撞到球()的屏幕上,你只需在x和y坐标处绘制一个圆圈,你的问题就应该解决了:

def ball(x,y):
    pygame.draw.circle(screen, (255,0,0), (x, y), 36)