# File gridhelper.py # # Some functions to support the creation and the management od 2D-viertual worlds with the layout of a gridhelper ############################## # IMPORTS import random as rnd import copy as cp # Print a matrix line by line def printMX(mx,n): for i in range(n): # Print matrix line by line print(mx[i],'\n') # '\n' commands a new line # Generate a matrix as a list with every element a list; all length = n def nmlist(n): mx = [['_' for y in range(n)] for x in range(n)] # Generate 2D matrix from ground printMX(mx,n) return mx # Number of objects based on percentage of objects def numberObj(p,n): maxObj=n*n numberObj=int(p*(maxObj/100)) # Restricting to integers as result print('Number of objects :\n',numberObj) return numberObj # Insert n-many objects ['O'] at selected position in the matrix # Generate randomly n-many coordinates # Attach at these positions by insertion the objects def placeObj(no,n,mx,obj): for i in range(no): x=rnd.randrange(n) y=rnd.randrange(n) mx[x][y]=obj print('Position : \n',x,y) print('New Matrix :\n') printMX(mx,n) return mx # Generate and show list of actual objects with 'name' in the grid # Get objects of type c out of the matrix mx # Attach a copy of the general values to each object # Generate individual IDs for each object def makeobjL(c,objL,mx,n): ol=[] ol=[[y,x,f] for y in range(n) for x in range(n) for f in mx[y][x] if f==c] [ol[i].append(cp.copy(objL[c])) for i in range(len(ol))] for i in range(len(ol)): ol[i][3][0]=i return ol