4. App Snake Game

In this chapter, we will play a classic game, snake.

4.1. App Snake Game

Now, let’s create and experience our own game.

4.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

Jumper Wires x12

jumper-wire

Push button x4

button-small

Resistor 10kΩ x4

Resistor-10kΩ

4.1.2. Circuit

Schematic diagram

snake_game_Sc

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

snake_game_Fr

4.1.3. Sketch

4.1.3.1. Sketch SnakeGame

  1. Use Processing to open the file Sketch_04_1_1_SnakeGame.

$ processing ~/Freenove_Kit/Processing/Apps/App_04_1_1_Snake_Game/App_04_1_1_Snake_Game.pde
  1. Click on “RUN” to run the code.

After the program is executed, Display Window displays as below.

../../../_images/snake_game.png

Pressing the space can start the game:

../../../_images/snake_game_1.png

You can control the movement direction of the snake through the four buttons in circuit or four arrow keys on the keyboard. The rules are the same as the classic Snake game:

../../../_images/snake_game_2.png

When game is over, pressing the space can restart the game:

../../../_images/snake_game_3.png

You can restart the game by pressing the space bar at any time during the game.