繁体   English   中英

Python - NameError: name '' 未定义

[英]Python - NameError: name '' is not defined

我目前正在通过以文本格式编写程序生成的地牢级别来扩展 Python 技能。 我很困惑为什么我的“相交”定义不起作用。 这是包含 def 的类:

class Room:

    global x1
    global x2
    global y1
    global y2

    global w
    global h

    global centre

    def __init__(self,x,y,w,h):
        x1 = x
        x2 = x + w
        y1 = y
        y2 = y + h
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        centre = math.floor((x1 + x2) / 2),math.floor((y1 + y2) / 2)

    #function that checks if the rooms intersect by comparing corner pins relative to the x,y tile map 
    def intersects(self,room):
        if x1 <= room.x2 and x2 >= room.x1 and y1 <= room.y2 and room.y2 >=  room.y1:
            return True
        return False

这是它被称为的地方:

def placeRooms(r):
    rooms = []
    #Where the room data is stored

    for r in range(0,r):
        w = minRoomSize + randint(minRoomSize,maxRoomSize)
        h = minRoomSize + randint(minRoomSize,maxRoomSize)
        x = randint(1,map_width - w - 1) + 1
        y = randint(1,map_height - h - 1) + 1

        newRoom = Room(x,y,w,h)

        failed = False

        #for every room generated, this function checks if new room intersects with the last one
        for otherRoom in rooms:
            if newRoom.intersects(otherRoom):
                failed = True
                break

        if failed == False:
            createRoom(newRoom)

            rooms.append(newRoom)

完整追溯:

Traceback (most recent call last):
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",      line 78, in <module>
placeRooms(2)
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",  line  65, in placeRooms
if newRoom.intersects(otherRoom):
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",   line 41, in intersects
if x1 <= room.x2 and x2 >= room.x1 and y1 <= room.y2 and room.y2 >= room.y1:
NameError: name 'x1' is not defined

我希望有人能帮助我理解为什么这段代码不起作用,谢谢。

我已经设法解决了这个问题。 如果我的问题没有很好地定义,我很抱歉。 我只学习了 4 周左右的 Python,我已经习惯了语法非常不同的 Java。 这是我的解决方案:

def __init__(self,x,y,w,h):
    self.x1 = x
    self.x2 = x + w
    self.y1 = y
    self.y2 = y + h
    self.x = x
    self.y = y
    self.w = w
    self.h = h

正如之前的大多数评论所说,您使用的全局变量根本不应该是全局变量。

我理解你的代码的方式,你的意思是x1x2y1y2是你的Room实例的属性,这意味着每个房间都有自己的x1x2y1y2 在 Python 中,您不必在类的开头(在此声明所有全局变量)声明属性,您只需在__init__方法中初始化属性。

这意味着您可以安全地删除所有global行,并将您的__init__更改为

def __init__(self,x,y,w,h):
    self.x1 = x
    self.x2 = x + w
    self.y1 = y
    self.y2 = y + h
    self.w = w
    self.h = h
    centre = (self.x1 + self.x2) // 2,(self.y1 + self.y2) // 2

(请注意,您不需要math.floor因为您已经在处理整数,只需使用整数除法运算符//

这样您就可以将x1y1x2y2whcenter为类的属性,这意味着每个实例对这些变量都有自己的值。 在 Python 中,您需要添加self. 在所有调用对象本身的属性之前,因此您还应该修改intersects以添加self. 在每次访问当前对象的属性之前(所有x1x2等,在您的代码中还没有以room.为前缀)。

另外,虽然我们在做,但我认为您的intersect函数没有按预期工作,但这是另一个问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM