GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing inventwithpython.com — Hex Grid BUY THIS BOOK: No Starch Press (free ebook) | Amazon | Barnes and Noble | Powell's | Bookshop.org | Goodreads | Kobo | Thriftbooks
SUPPORT THE AUTHOR: Patreon or Donate Directly
Prev: Hangman & Guillotine | Next: Hourglass
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.” Note that this program uses raw strings, which prefix the opening quote with a lowercase The Program in ActionFigure 35-1 shows what the output will look like when you run hexgrid.py.
Figure 35-1: Output showing a tessellated image of a hexagonal grid How It WorksThe power behind programming is that it can make a computer carry out repetitive instructions quickly and without mistakes. This is how a dozen lines of code can create hundreds, thousands, or millions of hexagons on the screen. In the Command Prompt or Terminal window, you can redirect a program’s output from the screen to a text file. On Windows, run
After entering the source code and running it a few times, try making experimental changes to it. The comments marked with
For practice, try re-creating this program with larger hexagon grids, such as the following patterns:
Exploring the ProgramThis is a base program, so there aren’t many options to customize it. Instead, consider how you could similarly program patterns of other shapes. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |