[Tutor] Fwd: Re: Mapping memory coordinate to screen coordinates

Alan Gauld alan.gauld at yahoo.co.uk
Wed Dec 2 03:57:54 EST 2020


I seem to have sent this just to Phil. Forwarding to list...



On 02/12/2020 00:10, Phil wrote:

> I came across the following example a couple of days ago and it shows
> the problem perfectly. Can anyone suggest a logical solution where
> memory grid[x][y] is the same as screen grid[x]y]?

My usual solution to these kind of issues is replace grid
with a dictionary keyed by x,y tuples.

If that's too simplistic then I'd create a grid class
with indexing implemented such that the coordinates
get reversed. But a dictionary is easier if you just
need data accessed by x,y

rows,cols = 4,4
grid = dict()
for row in range(rows):
for col in range(cols):
grid[col,row] = 0 #initialise.

#print column 3:
for row in range(rows):
print( grid[3,row] )

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list