[英]how can i find the location next to a randomly generated location
i am working on a maze generator using the random module mainly and generating tiles on a grid.我正在开发一个迷宫生成器,主要使用随机模块并在网格上生成图块。 right now i am trying to prevent 2 by 2s as i think it is a waste of space however if i try to calculate the location next to the random tile i have created i get a 'list index out of range' error现在我正试图防止 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
the 'RandomRow + 1' was a test as well as the extra1,2,3 and 4 but none of them have worked '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()
this is the whole code (the problem is at the bottom) when i started to change the grid i had to change a couple of things to make it work however now i am stuck at this one error这是整个代码(问题在底部)当我开始更改网格时,我必须更改一些东西才能使其正常工作,但是现在我陷入了这个错误
Traceback (most recent call last): File "C:\Users\faith\OneDrive\Desktop\websites\maze generator.py", line 88, in if len(Maze.Grid[Column][Row]) == 2: TypeError: object of type 'MapTile' has no len() Traceback(最近一次调用最后一次):文件“C:\Users\faith\OneDrive\Desktop\websites\maze generator.py”,第 88 行,在 if len(Maze.Grid[Column][Row]) == 2 中: TypeError:“MapTile”类型的 object 没有 len()
You get a IndexError: list index out of range
if you try to evaluate Grid[RandomColumn][RandomRow]
on an empty list.如果您尝试在空列表上评估Grid[RandomColumn][RandomRow]
,则会收到IndexError: list index out of range
。
You can initiate Grid with an 10x10 array filled with None
with:您可以使用填充为None
的 10x10 数组启动 Grid :
Grid = [[None for col in range(10)] for row in range(10)]
This will give you a result (not sure it will be what you want):这会给你一个结果(不确定它是否是你想要的):
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.