Arduino OLED Module(Snake Game)
Introduction
Four buttons control the snake to move up and down, left and right. The snake is displayed on the OLED screen. And it continues to move forward and eat the food.
Components
– 1 * Adeept UNO R3 Board
– 4 * Button Module
– 1 * OLED Module
– 1 * USB Cable
– 4 * 3-Pin Wires
– 1 * Breadboard
– Several Jumper Wires
Experimental Principle
The image:
Pin definition:
The schematic diagram:
The image:
Pin definition:
Experimental Procedures
Step 1: Build the circuit
Step 2: Use the 3D printer to print the keypad to secure the platform
Step 3: Install the function(U8glib) library:U8glib
Step 4: Program Adeept_snakeGame.ino
/***********************************************************
File name: Adeept_snakeGame.ino
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/01/07
***********************************************************/
#include "U8glib.h"
#define RIGHT 0
#define UP 1
#define LEFT 2
#define DOWN 3
//CLK = 10, MOSI = 9 , CS = 12, DC = 11, RES = 13
U8GLIB_SSD1306_128X64 u8g(10, 9, 12, 11, 13);
int score = 0;
int level = 1;
int gamespeed = 100;
int i;
//Constitute the unit, 4x4 pixels in the box.
const uint8_t ele[] PROGMEM = {
0xf0, //B1111000
0xb0, //B1011000
0xd0, //B1101000
0xf0, //B1111000
};
//snake
void element(int x, int y) {
u8g.drawBitmapP(x,y, 1,4, ele);
}
struct FOOD {
int x;
int y;
int yes;
};
FOOD food = {5, 50, 1};//(,97)
struct SNAKE {
int x[200];
int y[200];
int node;
int dir;
int lefe;
};
SNAKE snake = {{9,5}, {30,30}, 2, RIGHT, 0};
void UI() { //Basic game interface
u8g.drawFrame(0,1, 102,62); //Internal boundary
u8g.drawFrame(0,0, 102,64); //External boundary
u8g.setFont(u8g_font_5x7); //Set the font
u8g.drawStr(104,12, "LEVEL"); //Level of tips
u8g.drawStr(104,40, "SCORE"); //Score tips
}
void printScore(int x, int y, int s) {
u8g.setFont(u8g_font_6x10);
u8g.setPrintPos(x, y);
u8g.print(s);
}
void key() {
if(LOW == digitalRead(2)) {
snake.dir = DOWN;
}
if(LOW == digitalRead(3)) {
snake.dir = RIGHT;
}
if(LOW == digitalRead(4)) {
snake.dir = LEFT;
}
if(LOW == digitalRead(5)) {
snake.dir = UP;
}
}
void snakeGame() {
switch(snake.dir) {
case RIGHT:
snake.x[0] += 4;
if(snake.x[0]>=101) {
snake.x[0] = 1;
} break;
case UP:
snake.y[0] -= 4;
if(snake.y[0]<=1) {
snake.y[0] = 58;
} break;
case LEFT:
snake.x[0] -= 4;
if(snake.x[0]<=0) {
snake.x[0] = 97;
} break;
case DOWN:
snake.y[0] += 4;
if(snake.y[0]>=62) {
snake.y[0] = 2;
} break;
}
if((snake.x[0] == food.x) && (snake.y[0] == food.y)) {
snake.x[0] = food.x;
snake.y[0] = food.y;
snake.node++;
food.yes = 1;
score += 2;
level = score/10+1;//FOOD food = {random(1, 20), 50, 1};
}
for(i=snake.node-1;i>0;i--) {
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
}
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}
void loop() {
u8g.firstPage();
do {
UI();
for(i=0; i<snake.node;i++) {
element(snake.x[i], snake.y[i]);
}
element(food.x, food.y);
printScore(103, 62, food.x);
printScore(116, 62, food.y);
printScore(109, 22, level);
printScore(109, 50, score);
}while(u8g.nextPage());
key();
snakeGame();
delay(gamespeed);
}