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
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
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()
You get a IndexError: list index out of range
if you try to evaluate Grid[RandomColumn][RandomRow]
on an empty list.
You can initiate Grid with an 10x10 array filled with None
with:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.