Better Programming

Advice for programmers.

Follow publication

Member-only story

Making Grids in Python

Hip to be square

Keno Leon
Better Programming
Published in
6 min readNov 10, 2020

--

Grids
Photo by the author.

At some point, you might need to make a grid or grid-like visual structure for a project or coding interview question (mazes, search). A grid is also the foundation for placing objects on a canvas surface in an orderly manner and more complex cases like isometric views and tiled games.

This article is meant to take us both from zero grids to Intermediate Gridology in a hopefully painless way.

Note: The code and concepts discussed here are somehow interchangeable between languages, but some languages provide a better native experience and primitives are needed for grids. Python is notorious for not having a native/simple GUI solution, so I’ll use the next best thing: pygame (install it if you want to code along):

pip install pygame

Or:

python3 -m pip install pygame==2.0.0

Pygame is not really a GUI library but rather a simple game engine you can install and understand in a few minutes and then adapt to your GUI or language of choice. See the following section for a crash-course snippet…

Prerequisites

The following is a small script that creates a window, a surface to draw on, and draws a diagonal line in pygame. Most of it is set up. The important bits are the…

--

--

Responses (1)

Write a response