Python - NameError: name '' is not defined
Python - NameError: name '' is not defined
我目前正在通过编写文本格式的程序生成地牢关卡来扩展 python 技能。我对为什么我的 "intersects" 定义不起作用感到困惑。这是包含 def:
的 class
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
我希望有人能帮助我理解为什么这段代码不起作用,谢谢。
我已经设法解决了这个问题。如果我的问题没有很好地定义,我很抱歉。我只学习 Python 大约 4 周,我已经习惯了语法非常不同的 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
正如之前大多数评论所说,您使用了根本不应该是全局的全局变量。
根据我对您代码的理解,您的意思是 x1
、x2
、y1
和 y2
是 Room
实例的属性,这意味着每个房间都有自己的 x1
、x2
、y1
和 y2
值。在 Python 中你不必在 class 的开头声明属性(你在这里声明所有的全局变量),你只需要在 __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
,因为您已经在处理整数,只需使用整数除法运算符 //
)
这样你就可以定义 x1
、y1
、x2
、y2
、w
、h
和 center
作为 class 的属性,这意味着每个实例都有自己的这些变量值。在Python中,需要在所有调用对象本身的属性前加上self.
,所以也要修改intersects
,在每次访问一个属性前加上self.
您当前的对象(所有 x1
、x2
等在您的代码中尚未以 room.
为前缀的对象)。
此外,虽然我们正在处理它,但我认为您的 intersect
函数没有按预期工作,但这是另一个问题:)
我目前正在通过编写文本格式的程序生成地牢关卡来扩展 python 技能。我对为什么我的 "intersects" 定义不起作用感到困惑。这是包含 def:
的 classclass 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
我希望有人能帮助我理解为什么这段代码不起作用,谢谢。
我已经设法解决了这个问题。如果我的问题没有很好地定义,我很抱歉。我只学习 Python 大约 4 周,我已经习惯了语法非常不同的 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
正如之前大多数评论所说,您使用了根本不应该是全局的全局变量。
根据我对您代码的理解,您的意思是 x1
、x2
、y1
和 y2
是 Room
实例的属性,这意味着每个房间都有自己的 x1
、x2
、y1
和 y2
值。在 Python 中你不必在 class 的开头声明属性(你在这里声明所有的全局变量),你只需要在 __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
,因为您已经在处理整数,只需使用整数除法运算符 //
)
这样你就可以定义 x1
、y1
、x2
、y2
、w
、h
和 center
作为 class 的属性,这意味着每个实例都有自己的这些变量值。在Python中,需要在所有调用对象本身的属性前加上self.
,所以也要修改intersects
,在每次访问一个属性前加上self.
您当前的对象(所有 x1
、x2
等在您的代码中尚未以 room.
为前缀的对象)。
此外,虽然我们正在处理它,但我认为您的 intersect
函数没有按预期工作,但这是另一个问题:)