GridWorld Snake Project

posted by: Mr. Bergquist 25 March 2012 No Comment

Thanks to Dave Ruth from Niles North High School!

You will implement a game of Snake using GridWorld!  A bug will represent the head of the snake and its body will be an ArrayList of rocks.  Its goal will be to eat a flower.  You will start with the client which controls the snake with user input.  Eventually, your game will look like this:

Create a class called SnakeBug that extends class Bug.  Your bug needs to keep track of two pieces of information: how much it needs to grow by and an arraylist of the Locations it currently occupies.

Write a no-argument constructor that initializes variable grow to what will be the initial length of the snake (4 is a good value) and snake to an empty ArrayList().  Run SnakeGame to verify that you can control the snake head with the arrow keys.

public void randomFlower()
Creates a new Flower object in a random, empty Location in the Grid. You may want to use a local variable Grid gr as a reference to the Grid the SnakeBug is in. Select a random cell from gr that is empty (null). You may need to try several random cells before you find one that is empty. Make no presumptions about the size of gr — use Grid methods of to determine its number of rows and columns. Once an empty location is found, create a new Location with the row and column value for the random cell and ‘put’ a new Flower object into gr at that newly determined Location (using the Flower’s putSelfInGrid method).

Uncomment the call to head.randomFlower() in the SnakeGame main method. Run SnakeGame. One Flower
should appear in a random Location in the Grid.

You will be overriding the act() method so that the SnakeBug has the following behavior:

  • If the SnakeBug cannot move, end the game with the following code:
    javax.swing.JOptionPane.showMessageDialog(null, "Score: " + snake.size(), "GAME OVER!", 0);
  • If a Flower is directly ahead of the SnakeBug, increase the grow field by 3, remove the Flower and spawn a new Flower.  You may want local variables to represent the grid and the location in front of the SnakeBug based on its direction.
  • If the SnakeBug can move, it moves.  It will leave a rock behinds itself instead of a flower.  Create and call a method addRockBehindHead that takes in a location parameter.  It will create a Rock object at location loc and add loc to the beginning of the snake ArrayList.
  • If the snake needs to grow this turn, simply decrease the grow variable by one.  Otherwise, you will need to remove the last Rock in the snake to create the illusion of movement.  Write a method called eraseTail that removes the last element of the snake ArrayList from both the ArrayList and the grid.
1 Star2 Stars3 Stars4 Stars5 Stars (20 votes, average: 4.25 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>