Programming Project 7: Game of Worms

Worms (or snakes) is one of the classic computer games. The players starts of with a single-segment worm and grows the worm by running over food. The object is to get the worm to grow as long as possible without hitting the walls or the worm itself. Your game might look something like the following video. (Yes, this speeds up as the game progresses; that is not a requirement.)


Write a Java program (class), WormGame, that lets a single user play a basic version of the worm game. Your WormGame class should extend java.awt.Frame. Specifically, your game should:

  1. Create a window showing a single-segment worm at a random location on the board and a food piece at another random location (not the same). The worm and food should be rendered using different colors. Your constructor should consume two int parameters that represent the logical width and height of the board. food and worm segments should be (at least initially) rendered as circles or squares of a particular size. I suggest that you work with logical locations on the board and then translate them for rendering (but you may choose otherwise and deal with increments/decrements of cell sizes).
  2. Useing a KeyAdapter (about which you will hear more during class but you can also learn about KeyAdapter at the Java API), make the single-segment worm move in four directions. Have the program terminate if the worm runs into one of the walls.
  3. When the worm head runs over a food piece, the worm should grow by one segment. Use a Queue to represent the body of a worm; moving the worm amounts to removing the last segment of the worm and placing it on the front in the direction that the worm is currently moving. At this point, you probably have three classes: the WormGame itself, an extension of KeyAdapter, and a Worm class. Your worm class should now consist of (at least) a direction and a Queue of segments, where each segment is a location on the board.
  4. The game should already end when the worm runs into a wall but probably allows the worm to recoil over itself. Update your game so that the game ends when it runs into itself.
  5. Update your game so that players can either quit the game or continue playing by starting over with a new single-segment worm. You should keep track of and display on the window (i) the average length of worm at the time of its death, (ii) the longest worm at time of death, and (iii) the number of attempts.

Submission instructions:

Do not forget to use the javadoc constructs for your documentation and include the html file produced by the javadoc tool in your final submission. Make sure that you create a folder named with your Westmont email name followed by “Ex7”. For example, someone with email address “cjones” would create a folder called “cjonesEx7”. Make sure that inside that folder you have: for both parts of the assignment, java source files and html documentation files resulting from javadoc. Finally, either tar or zip the folder so that when we extract it, the folder “<emailname>Ex7” will be created. Submit the packaged file for the assignment on Eureka.