Author: Ben Stephenson (ben.stephenson@ucalgary.ca)
This assignment was first used with a small group of students in the summer of 2019. A number of minor revisions were made to the assignment after its first use and the provided materials were updated to include artwork contributed by a student that completed the assignment. The assignment was used for a second time (during the fall of 2019) with almost 400 students. The student handout from fall 2019 (with a couple of minor corrections) is available as a PDF and a Word document.
Summary | This assignment has students add the human player to a variant of Snake which I have named Viper. The provided materials include a polished user interface and game graphics that are platform independent. The result of the assignment is a fully functional game, including an AI that provides an appropriate amount of challenge for novice players. |
Audience | I have used this assignment as the third of four assignments in a programming intensive CS1 course that takes an objects-late approach. It can be used whenever students are being introduced to functions and lists (including 2D lists). |
Difficulty | Students found this assignment to be moderately difficult because it was the first assignment where they were required to write their own functions and it was their first assignment involving lists. I gave the students between 2 and 3 weeks to complete the assignment and my impression was that this was appropriate. |
Topics | The assignment focuses on functions and lists, including 2D lists. Students also need to write loops and if statements which I assume they are comfortable with from prior assignments that focused on such. |
Strengths |
This assignment has students write code involving both one and
two dimensional lists, as well as functions. Unlike many game
assignments that use a 2D list to represent a game board, this
assignment starts out by
having students solve problems involving only one dimensional
lists. Then students go on and use 2D lists in the final part
of the assignment where they check for collisions between the
human player's snake and the AI snakes. I believe that this is
beneficial because it gives students more opportunity to work with
one dimensional lists before tackling the complexity of 2D lists.
Students that complete the assignment end up with a fully functional game. The provided code includes a polished user interface and an AI that provides an appropriate level of challenge for (reasonably) novice players and this allows students to create something that looks much like the games that most students play. Fundamentally, the game is simple. It's just 4 lines being drawn on the screen. This simplicity keeps the number of game concepts that students need to understand to a minimum, allowing them to focus on concepts related to lists and functions, while simultaneously creating a game that is fun to play. |
Weaknesses |
This assignment has students add to a substantial existing code base.
My impression is that this can be intimidating for some
students. In the fall of 2019 there were also a few students that
managed to write reasonable code that caused crashes in the provided
code. The code provided to the students has subsequently been
updated to address this, but crashes in the provided code due to
errors made by students are always a risk when students are asked to
add to an existing program.
While the game is perfectly playable in its current form, players will gain the skill necessary to beat the AI players most of the time quite quickly. I strongly suspect that improving the AI so that it considers its turning radius when deciding what its heading should be would offer more challenge, but this improvement has not yet been implemented. The game does not include any sounds because, to the best of my knowledge, there is not a good option for platform independent sound that doesn't require students to install Python modules that are not included with the standard distribution. |
Dependencies |
The user interface is built using the SimpleGraphics module (which is
included in the provided materials). It is a wrapper for Python's
standard TKInter graphics module which is included with most standard
Python distributions. When the TKInter module is installed the
provided code executes successfully with Python 3 on Windows, MacOS
and Linux.
Do you have Python 3 but seem to be missing the TKInter module? You might need to install a package named python3-tk. Lots of information about installing Tk and using it with Python can be found at https://tkdocs.com/tutorial/install.html. |
Variants |
I chose to have students implement a single human player. The
difficulty of the assignment could be increased by having students
write the code needed to support anywhere from one to four human
players (with the balance of the players being AI controlled).
In the AGA Viper game, upon which my game is based, the snakes had gaps introduced into them periodically which provided opportunities for one snake to pass through the tail of another (and score a point when doing so). Implementing this feature would add further complexity to the assignment because the snakes would need to be data structures that include information about where the gaps are, in addition to all of the line segments. Students could be asked to add "power-ups" to the game which could include items such as a temporary speed boost, increasing your snake’s (but not your opponent’s) maximum length, freezing (or drastically slowing) all of your opponents for a short period of time, adding one point to your score, or temporarily allowing you to pass through the AI snakes (or yourself) without crashing. The students could also be asked to update the AI so that the AI snakes seek out the power-ups. The complexity of the gameplay could be increased by adding barriers to the playing field. Students would need to update the AI so that it detected and avoided the barriers, as well as determining if either the player snake or any of the AI snakes had crashed into the barrier. I provided students with the option to complete this task to raise the maximum possible grade on their assignment from A to A+ when I used this assignment in the fall of 2019. It might be beneficial to rename to the game from Viper to any other snake to make it more difficult for students to locate solutions from previous semesters. |
Acknowledgments |
The game that students create in this assignment is based on AGA
Viper, originally developed by Nathaniel Myhre and Robert Drennan in
1994 (which I remember playing on a friend’s Amiga 4000). A video
showing the original game can be found on YouTube.
I’d like to thank Ray Zegrer for creating the art assets for the game. |
Notes |
Some of the functionality needed by the AI players is very similar to the functionality that students need to implement. While this might initially appear to be a problem, I implemented the AI's functionality using a functional approach. This greatly reduces CS1 students' ability to copy the AIs code and adapt it to their own purposes while also providing better performance when using the standard Python distribution. |
My solution can be found here: viper_completed.py. To run my solution place it, SimpleGraphics.py and ViperImages.gif in the same folder and run it with Python 3.x.