Assignment #6: Nibbler
This time you program an entire computer game (it's a long way
from the boring Fibonacci numbers to something over-exciting like this!).
The game is 'Nibbler', or 'Snakes', a classic. A snake is placed into a maze (don't ask me why) and does
what snakes in mazes are told to do: eating dots. In order to do so it moves through the maze; and when its head
reaches a dot, the dot is eaten. Each 5th move the snake grows, no matter if it's eating dots or not.
This picture gives you an idea about the maze and the snake:
The picture also shows all of the framework provided. You can download (below) the code to
generate this image, the code contains everything to build, print and use the maze, it also contains the
basic code to handle a snake (the drawing of the snake) as well as a Main class, that puts these things
together to give you an idea how to work with it. You receive the following files:
- Maze.java: a java class that builds a maze from an image file. It extends JPanel, so you can directly
add it into a JFrame. Maze draws the maze and also calls the drawing routine of the snake. The maze is defined
by the image 'testpic.gif'. All black points translate into walls, white points into dots. The Maze class
also automatically detects the size of the maze. You can change the maze by yourself, if you wish. Just load
'testpic.gif' into paint, and change it.
In its paintComponent() method, 'Maze' calls a function 'Snake.draw(Graphics g)' of class 'Snake'. The drawing of
the snake belongs exactly there. You don't have to deal with it, just leave Snake.draw untouched (if you don't
want better graphics). Maze defines some constants in the beginning, e.g. for colors and size. You can play with
them to change the visual appearance.
- Snake.java: a java class giving a basic framework for the snake. it includes the drawing of the snake, and
the LinkedList. Each element of a snake is a java.awt.Point, a simple object that consists of fields 'x' and 'y'
only (you can look it up in the java API). Snake.java is the class where you will have to add all the methods
- Main.java: just an examle program to instantiate a Snake and a Maze. Just have a look at it.
- testpic.gif: a gif-image that defines the maze. Maze.java reads this file and translates it into wall/dots.
Here are some constraints:
- The movement of the snake has to be programmed in the way demonstrated in class: find the new position for the
head, create a new segment and add it to the LinkedList at the first position. If the snake does not grow, remove
the last element.
- The snake grows every 5th step. You can change this number to increase the playability.
- The game must react to the keyboard's arrow buttons. That's a new event type, but it's as simple as screen buttons.
Look it up in the API.
- You must detect if the snake bites itself. This means, the new head position already exists in one of the
segments. Therefore you have to traverse the list to check if a cell exists that has the same coordinates. You
must use an Iterator for this. Do NOT use 'get'. You know why !
- You must also detect when all dots are eaten. The game should show some positive reaction. Maze offers
a method 'getNumberOfDots()'. Please call it only once (it scans the whole array), and decrease the number of dots
by yourself each time the snake hits a dot.
Here's the code: NIBBLER
That's it, enjoy !