top of page

05.10.2018: One Lesson of Python

Today's soundtrack is Jelly Roll and Struggle Jennings: Waylon & Willie.

This evening I'm working on the next lesson in Coding Projects in Python, "Caterpillar." This is the first lesson in the last section of the book, "Games in Python"!

The game that I'll be making in this lesson is like the classic game "Snake."

Following the book's directions, I began by making a new file in the shell called "caterpillar.py". I then imported the two modules that I'll need for this project, "turtle" and "random." I then set the background color as "greenyellow" so that it looks like the classic Nokia screen.

Next, I added the code that told Turtle what color and shape the caterpillar would be. The book's version has a red caterpillar; however, I chose 'darkolivegreen' so as to give the effect of an old-school backlit screen. After that, I created the leaf shape for the caterpillar to eat, then entered some directions and created some variables that would make sure the game wouldn't start before it was loaded, and to write the score atop the screen.

After that, I made the functions that would make the game run; finally, I wrote the functions that would allow me to control the caterpillar's movements with the arrow keys.

When I tried to start the game, I found that the arrow key function prevented the game from starting, because the 'space' key wasn't registering. After 15 minutes of troubleshooting, I was exasperated; I Googled for a solution, and I was fortunate to find a kind of fix posted to Stack Overflow by cdlane, who advised the person originally asking the question that the code itself was not well-written (even though that same code was copied directly from the book, the same book that I'm using!) - and posted a fixed version. I was able to use that version and modify it to the way that I wanted it.


bottom of page