Type Error: new() missing 1 required positional argument: 'y'

Type Error: new() missing 1 required positional argument: 'y'

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

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

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 和函数的代码:

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 环境要求是 Python 2.7 和 Python 3

的所有版本

出了什么问题?

您正在学习的课程附带的代码使用 pymunk.vec2d.Vec2d. There recently has been an update to Vec2d making it a NamedTuple。在此更改之前,可以使用元组创建 Vec2d

vec2d = Vec2d((x, y))

这已经不可能了。 Vec2d 是一个具有字段 xy 的命名元组可以使用以下方法创建:单独的参数,* 用于元组解包的语法或 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)

那你能做什么?

您可能应该让 Intel 的人员知道他们的代码在 Pymunk 6.0.0 及更高版本中损坏。目前,您可以尝试使用 6.0.0 之前的 Pymunk 版本(这可能是最佳解决方案),或者您可以更改课程附带的代码并使用上述选项之一。