Lisette's Final Project; Turtle Guessing Game

by Lisette Dunham

22 Jun 2017

Turtle Guessing Game:

Final Milestones

User functionality

  • User can move turtle left, right, jump, and go up a level
  • User can click to change levels
  • User can click to see help instructions and make instructions dissapear
  • User can click to play again when they reach the top of the screen
  • User can see what level they are playing
  • User can see score
  • User can see a win screen when they correctly guess all the boxes

Game development

  • Create gameboard
  • Create dictionary to store turtle jump information
  • Turtle stays within bounds of the gameboard
  • Generate random numbers and assign to each turtle on all 5 rows
  • Increment a count every time the turtle has jumped and hit a box, but not when jumping and not hitting a box
  • Compare the number of the jump count to the randomly assigned numer
  • Change colors of turtle boxes when guessed correctly or incorrectly
  • Add 1 point when all three boxes on one row are guessed correctly
  • Provide an animation when the user gets a point
  • Play again button pops up when user makes it to the top of the screen and then restarts the game
  • Create additional rows above the 3 rows for additional levels and align with functionality of level one
  • Game board propely resets to level 1 when game is restarted
  • A winning image pops up when the user correctly guesses all of the boxes

Reflection

My trinket program is a guessing game. The goal of the game is to use a turtle to jump and hit three boxes in the correct order before jumping up to the next row of boxes. The user controls a turtle with the left, right, and up arrows (to move the turtle and try to hit the boxes), and the space bar to jump up to the next row of boxes. If the user hits all three boxes on one row in the correct order, they are awarded a point and also a one-up mushroom animation. If the user makes it all the way to the top of the level having guessed all the boxes correctly, an image appears saying “You win!” The user can select from level 1 (three rows to guess), level 2 (four rows to guess), and level 3 (five rows to guess). This game is an homage to one of my favorite Super Mario Brother’s hidden games-within-a-game, where the player can get more lives by guessing the order correctly and then go back to the game they were playing.

Original development

At the start of the development of this game things went very smoothly. I started early and took breaks whenever I wasn’t sure of how to make something work. While I was not coding I would regularly have lightbulb moments of how to integrate different functionality of the game. For example, I wasn’t sure how to recognize that the turtle had hit a box. I was cleaning when I realized that since the turtle only hits the box when jumping, I only needed to make sure they were in the right spot before the jump started and I could increment the jump counter and change the colors of the boxes on the jump. My plan to build the game involved a lot of iteration (and many different saved versions), first I wanted to get everything to work just for the first row of boxes and then implement to the other rows. I really hoped I would write elegant code that would loop through multiple times to run all the functions for each line. However, once I started to modify my code call a function 5 times, I ran into issues with global variables not working in the functions so I copied and pasted the code 5 times to keep the momentum of the game design going.

Change to the design

The design of my game changed a couple of times. I originally wanted the harder levels to have more boxes the user had to guess. I had no problems implementing a 5 box version, but since there is a much lower probability of the user correctly guessing 5 boxes, it wasn’t very fun to play. Adding more rows vertically was the appropriate next idea, but I didn’t know how to fit it all on the screen without rewriting all my code. Luckily for me, Sam had expanded his screen to make room for a score board at the top and showed me the function “setworldcoordinates” that he had used to accomplish it. Sam also showed me how he implemented showing an image and the removing the image using a key stroke. I used his example and modified it to be on a click for my instructions image. At one point, I had implemented a high score function that would write the 3 highest scores to the screen. This included a “done” button that would allow the user to type in their initials to add to the score dictionary and then write the dictionary to the file (never completely implemented). I thought this was a fun touch to make such a simple game a little more competitive, but after learning we needed a completely graphical user interface I never re-implemented using key strokes to type the initials. I had also implemented a hints button that the user could click to indicate which turtle box was either the second or third box (so they have one fewer option to guess the first box). In the end, I thought I couldn’t quickly explain the use of the button and I scrapped the idea.

Issues I ran into

One of the biggest issues I ran into was creating and accessing turtles that had been created with a custom class function. I spent hours and hours trying different ways to call the turtles to change their colors, but I kept getting errors when I tried to access them on modules other than the one the turtles were created on. I believe these errors are caused by being unable to identify a turtle created in another module. I tried using print(type(turtle_name)) function when debugging, but it caused another error. I found this incredibly frustrating because I couldn’t find a solution for this while I was on my own and came up with work-arounds and never brought this question up with my group or in class. I also ran into a bunch of issues with importing functions and turtles into other modules. I believe that I followed the correct steps to “from module import function_name, turtle_name.” When I first started on this program I had many different modules. I tried to construct my program so that I didn’t have to import any modules except for in main.py, but then I kept needing variables, functions, and turtles so I would import the whole module. In the end, I had to create one module (background.py) with nearly 1000 lines of code that draws the game board, creates most of the turtles, and writes information on the screen. Since all of these actions require the same information and turtles, they work best as a single module. Bugs that still exist and things I don’t feel are perfect I want the user to be able to click the “play again” button and the screen would re-draw the gameboard for level one. However, I ran into an issue with the animated one-up mushrooms that appear when a user gets a point. Because I animated these in a never-ending while loop, they can’t be deleted! At one point I did have the screen go back to level 1 when the player restarted the game, but if they had already won a one-up mushroom on the second or third level, the mushroom would basically float on nothing at the top of the screen. I decided it made more sense to leave the brown bars at the top, but only populate the turtles for level one. I actually like the way it looks when there are multiple mushrooms moving around on the screen. There are also some unexpected turtles on the 4th and 5th (top 2) rows that appear after the user has played level 2 or 3 and then tries to play level one afterwards. The function that reset the game board hides the turtles on the tops rows based on what level it is, but there must be some code elsewhere that shows those turtles or not setting the level correctly. I have yet to find the source of this, so I am going to have to live with this bug for now.

Things I learned

Dictionaries and for loops! Both dictionaries and for loops did not come easy for me when we learned them in class, but they have both (especially dictionaries) been incredibly useful in the creation of my game. I used dictionaries to assign random numbers to the boxes that could be overwritten for each game and to store the order the user guesses the boxes. I have a coding mindset that works really well for manipulating and analyzing structured datasets that are already been created. Coding regularly in other languages put me ahead of the other students in some ways and behind in other ways. I had already experienced the frustrations of coding and the thrill of solving an issue after hours of struggling with it. But having had so much experience with structured datasets has made it very difficult for me to think about less structured data formats. This hit me really hard when we started for loops and the CSV exercise. I was unable to see the forest for the trees because all I could think about was how I could solve the exercise using a “where” statement instead of thinking about looping through lists. My mindset did not even include the possibility of looping through lists of lists and the items within a list. I know this is where I really struggle with Python. I know how to do all the things in my toolbox of structured data, but I often don’t know where to begin with different types of data and I can’t always step back to look at it a different way. Another thing I struggle with is trying to find a solution before really understanding how something functions. There were several times on the smaller exercises in class I would struggle with something in the evening, and then more closely read details about it in the chapter the next morning, then I would be able to fix it easily. I think this is part impatience and part a scaffolding problem; the first time I read documentation about a technique I have no scaffolding to build my understand of that technique around, and reading documentation is not always fun. I essentially have a chicken and egg situation where for the greatest success I have to read the chapter and write the code twice each time. I also need to be more willing to walk away from the code and read the documentation when I get frustrated. I didn’t realize until now how much coding is like a painting a picture. You can have a complete game, but you’re never really done. Especially for something as undefined as a game. There are a million little tweaks I would like to make to improve it and make it a better experience for the user. I know I have to step away from this thing and appreciate it for what it is now and how it’s developed my coding skills in just a short time. I also recognize that trying to fix some of the issues that still exist may break other pieces of my game. In the process of creating this game I have about 15 versions saved because I knew I had something that was working and I wanted to be able to go back if I broke anything (which I did several times). I’m really excited to continue my journey into Python. I learned R with the aid of online courses and forcing myself to use it for smaller projects at work. I think I can do the same thing with Python, and I hope I do. I know Python is a useful language and could be something I would use in my data career in the future.

Lisette is a Research Data Statistical Analyst at UNC's Lineberger Comprehensive Cancer Center Find Lisette Dunham on Twitter, Github, and on the web.