HEX Grid
This short program produces a tessellated image of a hexagonal grid, similar to chicken wire. It shows that you don’t need a lot of code to make something interesting. A slightly more complicated variation of this program is Project 65, “Shining Carpet.”
After entering the source code and running it a few times, try making experimental changes to it. The comments marked with (!) have suggestions for small changes you can make. On your own, you can also try to figure out how to do the following:
Create tiled hexagons of a larger size. Create tiled rectangular bricks instead of hexagons. For practice, try re-creating this program with larger hexagon grids, such as the following patterns:
/ \ / \ / \ / \ / \ / \ / \
/ \___/ \___/ \___/ \___/ \___/ \___/ \
\ / \ / \ / \ / \ / \ / \ /
\___/ \___/ \___/ \___/ \___/ \___/ \___/
/ \ / \ / \ / \ / \ / \ / \
/ \___/ \___/ \___/ \___/ \___/ \___/ \
/ \ / \ / \ / \
/ \ / \ / \ / \
/ \_____/ \_____/ \_____/ \_____
\ / \ / \ / \ /
\ / \ / \ / \ /
\_____/ \_____/ \_____/ \_____/
/ \ / \ / \ / \
/ \ / \ / \ / \
/ \_____/ \_____/ \_____/ \_____
hex_grid.py
1"""Hex Grid, by Al Sweigart al@inventwithpython.com
2Displays a simple tessellation of a hexagon grid.
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: tiny, beginner, artistic"""
5
6# Set up the constants:
7# (!) Try changing these values to other numbers:
8X_REPEAT = 19 # How many times to tessellate horizontally.
9Y_REPEAT = 12 # How many times to tessellate vertically.
10
11for y in range(Y_REPEAT):
12 # Display the top half of the hexagon:
13 for x in range(X_REPEAT):
14 print(r'/ \_', end='')
15 print()
16
17 # Display the bottom half of the hexagon:
18 for x in range(X_REPEAT):
19 print(r'\_/ ', end='')
20 print()