python - 具有不同文件的局部和全局变量
问题描述
在 floor_algorithm.py 中,我有一个名为的函数make_floor_outline
,它循环遍历同一文件中的一些其他函数,并在单独的文件夹make_floor_outline
中输出我在函数中使用的一些数组。game
如果我make_floor_outline
在 algorithm.py 末尾调用该game
函数运行良好,但如果我在函数中调用make_floor_outline
,game
或在该函数所在的文件中的任何位置调用game
,我会收到一条错误消息,指出我尝试使用的数组make_floor_outline
未定义。为什么会这样?floor_algorithm.py ...
def floor_room_IDs():
global floor_roomIDs
rand_ID = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
r_y = 0
for Layer in floor_roomIDs:
r_x = 0
for Tile in Layer:
floor_roomIDs[r_y][r_x] = floor_map[r_y][r_x]
r_x += 1
r_y += 1
r_y = 0
for Layer in floor_roomIDs:
r_x = 0
for Title in Layer:
if floor_roomIDs[r_y][r_x] == '1':
room_ID = rand_ID.pop(random.randint(0, len(rand_ID)-1))
room_ID_level = globals()['r' + str(room_ID)]
roomC_ID = 'rc' + str(room_ID)
globals()['rc' + str(room_ID)] = room_ID_level.copy()
floor_roomIDs[r_y][r_x] = str(roomC_ID)
if floor_roomIDs[r_y][r_x] == 2:
floor_roomIDs[r_y][r_x] = 'r2'
if floor_roomIDs[r_y][r_x] == '3':
floor_roomIDs[r_y][r_x] = 'r3'
r_x += 1
r_y += 1
def floor_doors():
global floor_map
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if floor_map[r_y][r_x] == '1' or floor_map[r_y][r_x] == 2:
door_right = False
door_bottom = False
door_left = False
door_top = False
if r_x < 8:
if floor_map[r_y][r_x+1] != '/':
door_right = True
if r_y < 8:
if floor_map[r_y+1][r_x] != '/':
door_bottom = True
if r_x > 0:
if floor_map[r_y][r_x-1] != '/':
door_left = True
if r_y > 0:
if floor_map[r_y-1][r_x] != '/':
door_top = True
if door_top == True:
floor_map[r_y][r_x] = 'a'
if door_left == True:
floor_map[r_y][r_x] = 'b'
if door_bottom == True:
floor_map[r_y][r_x] = 'c'
if door_right == True:
floor_map[r_y][r_x] = 'd'
if door_top == True and door_left == True:
floor_map[r_y][r_x] = 'e'
if door_top == True and door_bottom == True:
floor_map[r_y][r_x] = 'f'
if door_top == True and door_right == True:
floor_map[r_y][r_x] = 'g'
if door_left == True and door_bottom == True:
floor_map[r_y][r_x] = 'h'
if door_left == True and door_right == True:
floor_map[r_y][r_x] = 'i'
if door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'j'
if door_top == True and door_left == True and door_right == True:
floor_map[r_y][r_x] = 'k'
if door_top == True and door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'l'
if door_left == True and door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'm'
if door_top == True and door_bottom == True and door_left == True:
floor_map[r_y][r_x] = 'n'
if door_top == True and door_bottom == True and door_right == True and door_left == True:
floor_map[r_y][r_x] = 'o'
r_x += 1
r_y += 1
def floor_validate():
global room_amount
room_amount = 0
end_room = 0
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if floor_map[r_y][r_x] == '1':
room_amount += 1
if r_y == 0 or r_y == 1 or r_y == 8 or r_y == 7 or r_x == 0 or r_x == 1 or r_x == 8 or r_x == 7:
if floor_map[r_y][r_x] == '1':
if end_room == 0:
if r_y == 8 and r_x != 8 and r_x != 0:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 0 and r_x != 8 and r_x != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_x == 0 and r_y != 8 and r_y != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_x == 8 and r_y != 8 and r_y != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 0 and r_x == 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_y == 0 and r_x == 8:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 8 and r_x == 0:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_y == 8 and r_x == 8:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if end_room > 1:
end_room = 0
if end_room == 1:
floor_map[r_y][r_x] = 2
r_x += 1
r_y += 1
if room_amount < 18 or room_amount > 22 or end_room != 1:
make_floor_outline()
else:
floor_room_IDs()
floor_doors()
def make_floor_outline():
global floor_map
global floor_roomIDs
read = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
floor_map = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','3','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
floor_roomIDs = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','3','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
for i in range(0, 17):
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if Tile == '3':
floor_map[r_y][r_x+1] = '1'
floor_map[r_y][r_x-1] = '1'
floor_map[r_y+1][r_x] = '1'
floor_map[r_y-1][r_x] = '1'
if Tile == '1':
if read[r_y][r_x] == '/':
read[r_y][r_x] = '1'
if r_y < 8:
if floor_map[r_y+1][r_x] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y+1][r_x] = '1'
if r_y > 0:
if floor_map[r_y-1][r_x] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y-1][r_x] = '1'
if r_x < 8:
if floor_map[r_y][r_x+1] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y][r_x+1] = '1'
if r_x > 0:
if floor_map[r_y][r_x-1] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y][r_x-1] = '1'
r_x += 1
r_y += 1
floor_validate()
我的游戏功能是带有代码的标准 while 循环。我尝试将global variable
每个变量放入make_floor_outline
我的游戏函数中,但这似乎没有任何区别。
解决方案
推荐阅读
- google-cloud-platform - Google API Gateway:在标头中提供 API 密钥
- cmake - 未指定 target_include_directories 的 CMake 构建目标
- visual-studio - Blazor 和 Visual Studio 的 Intellisense - 一些 Blazor 组件引用显示为错误错误,直到您单击文件
- mysql - Mysql:为视图设置排序规则不起作用
- amazon-cloudformation - 使用 CloudFormation 重新评估 AWS Config 规则
- bert-language-model - IndexError:在添加特殊标记后尝试微调 Roberta 模型时,自身的索引超出范围
- python - 使用 matplotlib 在 3D 中绘制多条彩色线和矢量
- java - Java/JPA/Sping - Spring 不会将正确的模型属性名称发送到前端/端。一些缓存发生在背面和正面之间
- typescript - 尝试创建新的 PowerBi 自定义视觉对象会出现样式错误
- azure - PowerShell:如何判断用户是否有权从 Azure Key Vault 读取机密?