python名字未定义_Python名称错误:未定义名称“”

我目前正在扩展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

它的名字是:

^{pr2}$

完全回溯:Traceback (most recent call last):

File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py", line 78, in

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

你可能感兴趣的:(python名字未定义)