GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Exercise 5: Snake Game


Program a Snake game

Game rules:

  • The snake's head can be moved using the cursor keys
  • The mouse (food) is automatically generated at a random position in the grid
  • After each mouse, the snake grows by one limb
  • If the snake collides with the boarder or itself, it's game over
  • Try to make your snake as long as possible!

 

As template you can use the file SnakeGame_0.java.

Edit SnakeGame_0.java in the Online-Editor

Download Snake_0.zip

 

We recommend to develop the game in the following steps:

  1. Make the limbs follow the snake's head
    Open the template in the online editor. In the source code of the example SnakeGame_0.java it is shown how the limbs of the snake can be stored in an arraylist and how to move them.
  2. Control the snake's head with keyboard keys
  3. Generate mice (food) at random locations. Make the snake grow longer after eating a mouse
  4. Let the game end, if the snake collides with itself or the boarder