Jogo da Cobrinha (Snake) em Matriz LED 8x8 com Arduino
Componentes e Materiais
| Componente | Quantidade |
|---|---|
| Arduino Proto Shield | 1 |
| Arduino UNO | 1 |
| Joystick Analógico (Genérico) | 1 |
| Jumpers Macho/Fêmea | 1 kit |
| Matriz LED 8x8 (SparkFun LED Array) | 1 |
Aplicativos e Serviços
Sobre o Projeto
Criei um console de jogos usando estrutura de Meccano/Erector Set e o código de Sergey Royz (confira!), com melhorias na velocidade para movimento suave. Não explico a estrutura — recomendo criar a sua. Mas ela é necessária. Divirta-se! Comente dúvidas ou sugestões para próximos projetos. O Fritzing não funcionou, então segue a ligação:
Esquemático
Encaixe o shield no Arduino UNO para ter mais saídas GND e 5V sem protoboard.
Matriz LED 8x8
- VCC → 5V Arduino
- GND → GND Arduino
- DIN → Pino digital 10
- CS → Pino digital 9
- CLK → Pino digital 8
Joystick
- GND → GND Arduino
- +5V → 5V Arduino
- VRX → A0
- VRY → A1
- SW → Pino digital 2
Código
Snake (C/C++)
Copie e cole o código abaixo:
/** * Snake * Implementação do clássico jogo da cobrinha * onde a cobra se move constantemente e o jogador * deve evitar colisões com si mesma e as bordas. * Ao comer a comida, cresce e acelera ligeiramente. * * 23 Jun 2016 * by Sergey Royz and hunter Buzzell */
#include <LedControl.h>
/* Pinos do Joystick */
#define VRX A0
#define VRY A1
#define SW 2
/* Pinos do Display */
#define CLK 8
#define CS 9
#define DIN 10
#define SIZE 8
#define ADVANCE_DELAY 20
int snake[SIZE*SIZE][2];
int length;
int food[2], v[2];
bool is_game_over = false;
long current_time, prev_advance;
int blink_count;
const short messageSpeed = 5;
LedControl lc = LedControl(DIN, CLK, CS, 1);
void init_game() {
prev_advance = current_time = 0;
blink_count = 3;
int half = SIZE / 2;
length = SIZE / 3;
for (int i = 0; i < length; i++) {
snake[i][0] = half - 1;
snake[i][1] = half + i;
}
food[0] = half + 1;
food[1] = half - 1;
v[0] = 0; v[1] = -1;
}
void render() {
for (int i = 0; i < length; i++) {
lc.setLed(0, snake[i][0], snake[i][1], 1);
}
lc.setLed(0, food[0], food[1], 1);
}
void clearScreen() {
for (int x = 0; x < SIZE; x++)
for (int y = 0; y < SIZE; y++)
lc.setLed(0, x, y, 0);
}
/** Move a cobra * @return true se game over */
bool advance() {
int head[2] = {snake[0][0] + v[0], snake[0][1] + v[1]};
if (head[0] < 0 || head[0] >= SIZE || head[1] < 0 || head[1] >= SIZE) {
delay(1000); showGameOverMessage(); return true;
}
for (int i = 0; i < length; i++)
if (snake[i][0] == head[0] && snake[i][1] == head[1]) {
delay(1000); showGameOverMessage(); return true;
}
bool grow = (head[0] == food[0] && head[1] == food[1]);
if (grow) {
length++;
randomSeed(current_time);
food[0] = random(SIZE);
food[1] = random(SIZE);
}
for (int i = length - 1; i >= 0; i--) {
snake[i + 1][0] = snake[i][0];
snake[i + 1][1] = snake[i][1];
}
snake[0][0] += v[0];
snake[0][1] += v[1];
return false;
}
void setup() {
pinMode(SW, INPUT_PULLUP);
pinMode(VRX, INPUT); pinMode(VRY, INPUT);
attachInterrupt(digitalPinToInterrupt(SW), restart, RISING);
lc.shutdown(0, false);
lc.setIntensity(0, 8);
init_game();
render();
}
void loop() {
if (!is_game_over) {
clearScreen(); render();
if (current_time - prev_advance > ADVANCE_DELAY) {
is_game_over = advance();
prev_advance = current_time;
}
} else {
while (blink_count > 0) {
clearScreen(); delay(300);
render(); delay(300);
blink_count--;
}
}
readControls();
current_time++;
}
void restart() { init_game(); is_game_over = false; }
void readControls() {
int dx = map(analogRead(VRX), 0, 906, 2, -2);
int dy = map(analogRead(VRY), 0, 906, -2, 2);
if (dx != 0) dx = dx / abs(dx);
if (dy != 0) dy = dy / abs(dy);
if (dy != 0 && v[0] != 0) { v[0] = 0; v[1] = dy; }
if (dx != 0 && v[1] != 0) { v[0] = dx; v[1] = 0; }
}
const PROGMEM bool gameOverMessage[8][90] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0},
{0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}
};
void showGameOverMessage() {
for (int d = 0; d < sizeof(gameOverMessage[0]) - 7; d++) {
for (int col = 0; col < 8; col++) {
delay(messageSpeed);
for (int row = 0; row < 8; row++) {
lc.setLed(0, row, col, pgm_read_byte(&(gameOverMessage[row][col + d])));
}
}
}
}
Processo de manufatura
- Controle USB para Jogos DIY com Arduino – Construa Seu Próprio Gamepad de Alto Desempenho
- Construa um Console Pong 24×16 LED com Arduino Nano e MAX7219
- Construa um Jogo Tetris com Arduino Nano em Matriz LED 16×8 DIY
- Controlar um LED via Bluetooth com Arduino – Guia DIY Simples
- Display Interativo de Matriz LED na Porta com Sensor PIR – Saudação de Férias Arduino
- Construa um Robô Quadrúpede Inteligente com Arduino: Guia Passo a Passo
- Pixel Chaser: Jogo LED Interativo de Um Toque com Arduino Nano
- Relógio Flip Arduino com Matriz LED 8×8 – Projeto DIY de Relógio em Tempo Real
- Misturador de Cores RGB com Arduino – Projeto DIY para Iniciantes
- Controlando Matriz de LED 8×8 com Arduino Uno: Guia Passo a Passo