类型错误:new()缺少1个必需的位置参数:“y”

2024-10-03 04:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个机器人环境,这是英特尔提供的免费课程的一部分。课程作业中有多个文件,MakeItLearn.py是我们应该编辑的文件,并在其中添加我们自己的网络,用于机器人的培训

但是,当我尝试使用ExploreAndCollect.py文件收集数据时(Intel也提供了该文件,我们没有被告知更改该文件中的任何内容),它会抛出以下错误:

Traceback (most recent call last):   File "ExploreAndCollect.py", line 268, in <module>     env = BotEnv()   File "ExploreAndCollect.py", line 35, in __init__     self.BuildBot(50.01, 450.01, 20)   File "ExploreAndCollect.py", line 69, in BuildBot     BoxPoints = list(map(Vec2d, [(-size, -size), (-size, size), (size,size), (size, -size)])) TypeError: __new__() missing 1 required positional argument: 'y'

下面是定义类和函数的代码:

class BotEnv:
    def __init__(self):
        ## Initialize Required Variables
        self.crashed = False
        self.DetectCrash = 0
        self.space = pymunk.Space()
        self.BuildBot(50.01, 450.01, 20)  <---------------------------------------------
        self.walls = []
        self.WallShapes = []
        self.WallRects = []
        ## Add Walls To The Map ###
        WallBody, WallShape, WallRect = self.BuildWall(200, 50, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(200, 125, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(200, 550, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(200, 450, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(400, 350, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(400, 250, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(500, 250, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(600, 250, 50)
        self.WallRects.append(WallRect)
        WallBody, WallShape, WallRect = self.BuildWall(115, 1050, 500)
        self.WallRects.append(WallRect)
        self.PreviousAction = 0

    def BuildBot(self, x, y, r):
        ### Build The Bot Object ###
        size = r
        BoxPoints = list(map(Vec2d, [(-size, -size), (-size, size), (size,size), (size, -size)])) <----------------
        mass  = 0.5
        moment = pymunk.moment_for_poly(mass,BoxPoints, Vec2d(0,0))
        self.Bot = pymunk.Body(mass, moment)
        self.Bot.position = (x,y) # Declare Bot Position
        self.Bot.angle = 1.54     # Set the Bot Angle
        BotDirection = Vec2d(PointsFromAngle(self.Bot.angle)) # Get The Direction Vector From Angle
        self.space.add(self.Bot)
        self.BotRect = pygame.Rect(x-r,600-y-r, 2*r, 2*r)
        return self.Bot

我查看了上面提到的函数调用,其中包含y组件,因此我似乎无法找到导致错误的原因。python环境要求是Python2.7和Python3的所有版本


Tags: 文件pyselfsizebotfileappendbuildbot
1条回答
网友
1楼 · 发布于 2024-10-03 04:29:50

出什么问题了?

您正在学习的课程附带的代码使用pymunk.vec2d.Vec2d。最近对Vec2d进行了更新,使其成为NamedTuple。在此更改之前,可以使用元组创建Vec2d

vec2d = Vec2d((x, y))

这已经不可能了Vec2d是一个带有字段{}和{}的命名元组,可以使用以下方法创建:{}单独的参数、*元组解包语法或Vec2d._make

v1 = Vec2d(3, 4)
v2 = Vec2d(*(3, 4))
v3 = Vec2d._make((3, 4))

这些都在the Changelog for Pymunk(版本6.0.0)中描述:

  • Made Vec2d a subclass of NamedTuple.
    • Vec2ds has to be constructed with separate x and a y values.
    • Vec2d((1,2)) can be changed to Vec2d(*(1,2)).
    • Vec2d(Vec2d(1,2)) can be changed to Vec2d(*Vec2d(1,2)).
    • Vec2d() can be changed to Vec2d(0,0) or Vec2d.zero().
    • Vec2d(1,2) is no longer equal to [1,2] since they are of different types. (but Vec2d(1,2) == (1,2) is still true)

那你能做什么呢

您可能应该让英特尔的员工知道,他们的代码已被Pymunk 6.0.0及更高版本破坏。目前,您可以尝试使用Pymunk 6.0.0之前的版本(这可能是最好的解决方案),也可以更改课程附带的代码,并使用上述选项之一

相关问题 更多 >