The Shining, a 1980 psychological horror film directed by Stanley Kubrick, takes place at the haunted Overlook Hotel. The hotel carpet’s hexagonal design became an iconic part of this famous movie. The carpet features alternating and interlocking hexagons whose mesmerizing effect is well-suited for such an unnerving film. The short program in this project, similar to Project 35, “Hex Grid,” prints this repetitive pattern on the screen.
Note that this program uses raw strings, which prefix the opening quote with a lowercase r, so that the backslashes in the string aren’t interpreted as escape characters.
The Program in Action
When you run shiningcarpet.py, the output will look like this:
The creation of a program like this (or the similar Project 35) doesn’t begin with coding but rather just drawing tessellating shapes in a text editor. Once you’ve written out the pattern, you can cut it down to the smallest unit to be tiled:
After you’ve copied and pasted this text into the source code, you can write the rest of the program around it. Software is not just a matter of sitting down and writing code from beginning to end. Every professional software developer goes through several iterations of tinkering, experimentation, and debugging. The end result may be just nine lines of code, but a small program doesn’t necessarily imply that a small amount of effort went into making it.
1. """Shining Carpet, by Al Sweigart [email protected]
2. Displays a tessellation of the carpet pattern from The Shining.
3. View this code at https://nostarch.com/big-book-small-python-projects
4. Tags: tiny, beginner, artistic"""
6. # Set up the constants:
7. X_REPEAT = 6 # How many times to tessellate horizontally.
8. Y_REPEAT = 4 # How many times to tessellate vertically.
10. for i in range(Y_REPEAT):
11. print(r'_ \ \ \_/ __' * X_REPEAT)
12. print(r' \ \ \___/ _' * X_REPEAT)
13. print(r'\ \ \_____/ ' * X_REPEAT)
14. print(r'/ / / ___ \_' * X_REPEAT)
15. print(r'_/ / / _ \__' * X_REPEAT)
16. print(r'__/ / / \___' * X_REPEAT)
Exploring the Program
For practice, try creating patterns such as the following: