python - 类型错误 : argument 1 must be pygame. 表面,而不是 pygame.Rect

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

最佳答案

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/

相关文章:

python - sklearn 中的 CountVectorizer 仅包含出现次数高于某个最小次数的单词

python - 执行 pygame 时提高性能?

Python-创建虚拟环境时没有pip

python - 在pygame中慢慢画一条线,而其他线保持静止

python - 在 while 循环中使用生成器将文本传输到屏幕上

python - 在 Python 中运行游戏循环时多线程显示游戏分数?

python - 如何创建多个相似的 Sprite ?

python - 在 wxpython 中,将所有文本从一个 TextCtrl 复制、剪切、粘贴并选择到另一个

python - 如何在 pandas 中按 user_id 按组从列表列中获取唯一值

python - itertools 多个元素的所有可能组合