I can't figure out why I'm getting an index error. I have check the indexes of the list matrix I'm modifying and I shouldn't be trying to modify non-existent values. I am using a list matrix to represent square tiles on a game map. I want each tile that has the forest value of 1, to be surrounded on all eight sides by tiles of the same value.
I feel like it would be too lengthy and unnecessary to show you all of my code. The parameter mapMatrix is a list matrix of ints. It's got 200 lists as elements inside and, initially, each list has 100 int elements of 0. Though I used a for-loop to create it, I am absolutely sure I am not wrong about its elements and values. I have used print() and len() statements to check before calling the function I've included. genPoint is a tuple of ints representing a point. It is (50, 75). I assigned this manually in the function that calls this one.
The error I get (if I remove the try/except statements):
file destination address\python.exe file destination address/map_main.py Traceback (most recent call last): File "file destination address", line 56, in main() File "file destination address/Last_Map/map_main.py", line 28, in main mapMatrix = forest_gen(mapMatrix, GEN_POINT) # add Deep Forest File "file destination address\forest_gen.py", line 64, in forest_gen mapMatrix[p[y + 1]][p[x]] = 1 IndexError: tuple index out of range
"""
Generates a forest on map matrix.
"""
__author__ = "LuminousNutria"
__Date__ = "4-28-18"
import random as r
def forest_gen(mapMatrix, genPoint):
"""
Adds int values of 1 to mapMatrix. This represents forested tiles.
Preconditions:
First parameter, mapMatrix, should only have zeroes.
Algorithm:
One point is taken from the genPoints parameter.
Ten points are generated within 7 tiles of this.
Each tile surrounding the ten tiles is converted to 1 from 0.
Roughly half of the tiles surrounding those tiles are turned to 1.
Roughly one-quarter of the previous tiles are turned to 1.
All points surrounded by at least 6 points of 1, are turned to 1.
:param genPoint: tuple of ints
A two value tuple representing an (x, y) point.
:param mapMatrix: list matrix of ints
A matrix representing the map.
:return: list matrix of ints
Returns mapMatrix
"""
pointList = []
# The point indicated from genPoint is forest.
genX = genPoint[0]
genY = genPoint[1]
mapMatrix[genY][genX] = 1
height = len(mapMatrix)
width = len(mapMatrix[0])
# Find ten points within 7 tiles of genPoint.
xPos = r.randint(0, width)
yPos = r.randint(0, height)
distance = abs((genX - xPos) + (genY - yPos))
for i in range(10):
while distance > 7:
xPos = r.randint(0, width)
yPos = r.randint(0, height)
distance = abs((genX - xPos) + (genY - yPos))
pointList.append((xPos, yPos))
# Make all points in pointList forest.
for p in pointList:
mapMatrix[p[1]][p[0]] = 1
# Surround each forest tile with forest tiles.
for y in range(height):
for x in range(width):
if mapMatrix[y][x] == 1:
try:
mapMatrix[p[y + 1]][p[x]] = 1
mapMatrix[p[y - 1]][p[x]] = 1
mapMatrix[p[y]][p[x + 1]] = 1
mapMatrix[p[y]][p[x - 1]] = 1
mapMatrix[p[y + 1]][p[x + 1]] = 1
mapMatrix[p[y - 1]][p[x - 1]] = 1
mapMatrix[p[y + 1]][p[x - 1]] = 1
mapMatrix[p[y - 1]][p[x + 1]] = 1
except IndexError:
print("ERROR1 forest_gen function:")
print("\tForest is off the edge of the map.")
print("\tAt point:", (x, y))
return False
Aucun commentaire:
Enregistrer un commentaire