python - 如何找到随机生成的位置旁边的位置
问题描述
我正在开发一个迷宫生成器,主要使用随机模块并在网格上生成图块。现在我正试图防止 2 x 2s,因为我认为这是浪费空间但是如果我尝试计算我创建的随机图块旁边的位置,我会得到一个“列表索引超出范围”错误
import random
end = 90
i = 0
while i < end:
RandomRow = random.randint(0, MazeSize - 1)
RandomColumn = random.randint(0, MazeSize - 1)
Extra1 = RandomRow + 1
Extra2 = RandomRow - 1
Extra3 = RandomColumn + 1
Extra4 = RandomColumn - 1
if not Grid[RandomColumn][RandomRow]:
if not Grid[RandomColumn][RandomRow + 1]:
TempTile = MapTile('walls', RandomColumn, RandomRow)
Grid[RandomColumn][RandomRow].append(TempTile)
else:
end += 1
i += 1
'RandomRow + 1' 是一个测试以及额外的 1、2、3 和 4,但它们都没有工作
import pygame
import random
import time
pygame.init()
clock = pygame.time.Clock()
Screen = pygame.display.set_mode([710, 710])
Done = False
MazeSize = 10
TileWidth = 60
TileMargin = 10
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
class MapTile(object):
def __init__(self, Name, Column, Row):
self.Name = Name
self.Column = Column
self.Row = Row
class Character(object):
def __init__(self, Name, Column, Row):
self.Name = Name
self.Column = Column
self.Row = Row
Maze.update()
class Maze(object): #The main class; where the action happens
global MapSize
Grid = [[None for col in range(11)] for row in range(11)]
# Creating grid
for Row in range(MazeSize):
Grid.append([])
for Column in range(MazeSize):
Grid[Row].append([])
#making walls
end = 9
i = 0
while i < end:
RandomRow = random.randint(0, MazeSize - 1)
RandomColumn = random.randint(0, MazeSize - 1)
Extra1 = RandomRow + 1
Extra2 = RandomRow - 1
Extra3 = RandomColumn + 1
Extra4 = RandomColumn - 1
if not Grid[RandomColumn][RandomRow]:
if not Grid[RandomColumn][RandomRow + 1]:
TempTile = 'x'
Grid[RandomColumn][RandomRow] = (TempTile)
else:
end += 1
i += 1
#making the whole grid flat land
for Row in range(MazeSize):
for Column in range(MazeSize):
TempTile = MapTile('path', Column, Row)
Grid[Column][Row] = (TempTile)
Maze = Maze()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
Screen.fill(BLACK)
for Row in range(MazeSize):
for Column in range(MazeSize):
for i in range(1):
Color = WHITE
if len(Maze.Grid[Column][Row]) == 2:
Color = RED
if len(Maze.Grid[Column][Row]) >= 3:
Color = BLUE
pygame.draw.rect(Screen, Color, [(TileMargin + TileWidth) * Column + TileMargin,
(TileMargin + TileWidth) * Row + TileMargin,
TileWidth,
TileWidth])
clock.tick(60)
pygame.display.flip()
pygame.quit()
这是整个代码(问题在底部)当我开始更改网格时,我必须更改一些内容才能使其正常工作,但是现在我陷入了这个错误
Traceback(最近一次调用最后一次):文件“C:\Users\faith\OneDrive\Desktop\websites\maze generator.py”,第 88 行,在 if len(Maze.Grid[Column][Row]) == 2 中: TypeError:“MapTile”类型的对象没有 len()
解决方案
IndexError: list index out of range
如果您尝试Grid[RandomColumn][RandomRow]
在一个空列表上进行评估,您会得到一个。
您可以使用填充有以下内容的 10x10 数组启动 Grid None
:
Grid = [[None for col in range(10)] for row in range(10)]
这会给你一个结果(不确定它是否是你想要的):
import random
MazeSize = 10
Grid = [[None for col in range(11)] for row in range(11)]
end = 90
i = 0
while i < end:
RandomRow = random.randint(0, MazeSize - 1)
RandomColumn = random.randint(0, MazeSize - 1)
if not Grid[RandomColumn][RandomRow]:
if not Grid[RandomColumn][RandomRow + 1]:
TempTile = 'x'
Grid[RandomColumn][RandomRow] = TempTile
else:
end += 1
i += 1
for i in Grid:
print(i)
推荐阅读
- javascript - 如何停止所有媒体流
- objective-c - 如何将 cv::Mat 对象转换为 NSArray?
- azure - dotnet 核心 webapi、docker 和 azure。第一次 api 调用期间的性能问题
- mongodb - 使用 PyMongo 在 mongodb 中创建多个集合
- reactjs - 如何正确设置 axios 默认标头
- c - 无法从 AWS lambda 函数运行静态编译的 hello-world 可执行文件
- domain-driven-design - 单个命令是否应该处理多个聚合?
- java - Java8 maven引发错误对过滤器的引用不明确
- java - 生成范围内的随机数(java)
- apache-kafka - Kafka Manager 应该单独安装 Zookeeper 吗?