Coding Global Background
Coding Global

Code improvement for viewpoint in 2d array based on coordinate

Archived a year ago
2 messages
2 members
a year ago
Open in Discord
R
RotatingPanels
Copy Paster!

**mapViewer.py๐Ÿ“ ** Looking for code quality improvements for this segment of code. ```python ''' coord: (X,Y) -> represents coordinates for viewpoint range: ((VX0,VX1),(VY0,VY1)) -> vision range for each axis ''' def Viewpoint(mapObj,coord:tuple,range:tuple) -> None: mapLayout = mapObj.map range = ((range[0],range[0]),(range[1],range[1])) #Fixing view range in both dimensions, Depending on boundaries of map #If coord + range = outofbounds: lower range until its within bounds if coord[0] - range[0][0] < 0: range[0][0] -= abs(coord[0] - range[0][0]) if coord[0] - range[0][1] < 0: range[0][1] -= abs(coord[0] - range[0][1]) if coord[1] - range[1][0] < 0: range[1][0] -= abs(coord[1] - range[1][0]) if coord[1] - range[1][1] < 0: range[1][1] -= abs(coord[1] - range[1][1]) #With range within bounds, We can start string formatting for visual layout relative to coordinates for j,y in enumerate(mapLayout[coord[1] - range[1][0]: (coord[1] + range[1][1])+1]): print(f"{"\n"}{"-"*(((range[0][0]+range[0][1]+1)*2)+1)}{"\n"}") output = "" for i,x in enumerate(y[coord[0] - range[0][0]: (coord[0] + range[0][1])+1]): if (i,j) == (0+range[0][0],0+range[1][0]): output+=f"|P" else: output+=f"|{x}" output+="|" print(output) print(f"{"\n"}{"-"*(((range[0][0]+range[0][1]+1)*2)+1)}{"\n"}") ```

Replies (2)