如何解决如何实现一个类?
我一直在练习,我们需要使用其他函数的size属性创建一个毛毛虫。在下面的这段代码中,我在0到3之间添加了一个size属性。
class Caterpillar:
def __init__(self):
self.size = random.randrange(0,3)
x = random.randrange(50,950)
y = random.randrange(50,950)
self.xcoord = x
self.ycoord = y
但是在下面的这段代码中,当我尝试在此行pygame.draw.ellipse(screen,red,[x,y,size,40,45])
中添加大小时,却出现了错误。
def draw_critter(self,screen):
x = self.xcoord
y = self.ycoord
size = self.size
pygame.draw.ellipse(screen,45])
pygame.draw.ellipse(screen,black,[x + 6,y + 10,10,15])
pygame.draw.ellipse(screen,[x + 24,15])
pygame.draw.line(screen,(x + 11,y + 1),(x + 9,y - 10),3)
pygame.draw.line(screen,(x + 25,(x + 26,3)
我不明白为什么会收到此错误
File "D:/UniFiles/Sem/159/critters.py",line 53,in <module>
critter.draw_critter(screen)
File "D:\UniFiles\Sem\159\mycritters.py",line 27,in draw_critter
pygame.draw.ellipse(screen,45])
TypeError: Invalid recstyle argument
我不确定我是否真的知道上课的方式。有人可以给我一个例子,说明我如何实现draw_critter函数来创建整个毛毛虫,而不仅仅是头部。
解决方法
查看椭圆https://www.pygame.org/docs/ref/draw.html#pygame.draw.ellipse的API文档
ellipse()
期望:
pygame.draw.ellipse() draw an ellipse ellipse(surface,color,rect) -> Rect ellipse(surface,rect,width=0) -> Rect
什么是Rect?它期望这样:
pygame.Rect pygame object for storing rectangular coordinates Rect(left,top,width,height) -> Rect Rect((left,top),(width,height)) -> Rect Rect(object) -> Rect
它在任何地方都没有提到size参数。
我不确定您要做什么,但这听起来像是想要一个长椭圆形的毛毛虫?然后,也许您可以画出size
个椭圆并将它们隔开一点。
例如:
for s in range(self.size):
pygame.draw.ellipse(screen,red,[x + s*10,y,40,45])
那只是一种方式,我不知道你想要它的外观。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。