Ximon78-snake
2020-12-19 15:30:00
Snake.ino
#include "LedControl.h"
#define LEFT A1
#define UP A2
#define DOWN A3
#define RIGHT A4
#define START_LEN 3
const int DIN_PIN = 4;
const int CS_PIN = 3;
const int CLK_PIN = 2;
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN);
int direction = 2;
int vx = 1;
int vy = 0;
//define the snake array with shape (64,2)
//i.e. a max of 64 parts and 2 (x,y) values per part
int snake[64][2];
//we can only call extend_snake when the snake already
//has one block, so we define it with one
int snake_len = 1;
//an x,y pair of the apple location
int apple[2];
//boolean - is the game over?
bool gameover = false;
void setup(){
Serial.begin(9600);
//setup the chip (wake it, set brighntess, clear it)
lc.shutdown(0,false);
lc.setIntensity(0,1);
lc.clearDisplay(0);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
pinMode(UP, INPUT_PULLUP);
pinMode(LEFT, INPUT_PULLUP);
randomSeed(analogRead(0));
snake[0][0] = random(0,8);
snake[0][1] = random(0,8);
for (int i = snake_len; i < START_LEN; i++){
extend_snake();
}
new_apple();
}
void loop(){
//if (am_i_dead()) return;
check_buttons();
move_snake();
check_eaten_apple();
draw();
delay(150);
}
bool am_i_dead(){
for (int i = 0; i < snake_len-2; i++){
if (snake[i][0] == snake[snake_len-1][0] && snake[i][1] == snake[snake_len-1][1]){
return true;
}
}
return false;
}
void check_eaten_apple(){
if (snake[snake_len-1][0] == apple[0] && snake[snake_len-1][1] == apple[1]){ //eaten an apple
extend_snake();
new_apple();
}
}
void new_apple() {
apple[0] = random(0,8);
apple[1] = random(0,8);
}
void move_snake(){
for (int i = 0; i < snake_len-1; i++){
snake[i][0] = snake[i+1][0];
snake[i][1] = snake[i+1][1];
}
snake[snake_len-1][0] = (snake[snake_len - 2][0] + vx + 8) % 8;
snake[snake_len-1][1] = (snake[snake_len - 2][1] + vy + 8) % 8;
}
void extend_snake(){
snake[snake_len][0] = (snake[snake_len - 1][0] + vx + 8) % 8;
snake[snake_len][1] = (snake[snake_len - 1][1] + vy + 8) % 8;
snake_len++;
}
void draw(){
lc.clearDisplay(0);
for (int i = 0; i < snake_len ; i++){
lc.setLed(0, snake[i][0], snake[i][1], HIGH);
}
lc.setLed(0, apple[0], apple[1], HIGH);
}
void _up() {
vx = -1;
vy = 0;
direction = 0;
}
void _right() {
vx = 0;
vy = 1;
direction = 1;
}
void _down() {
vx = 1;
vy = 0;
direction = 2;
}
void _left() {
vx = 0;
vy = -1;
direction = 3;
}
void check_buttons(){
if (digitalRead(UP)){ _up(); }
if (digitalRead(DOWN)){ _down(); }
if (digitalRead(LEFT)){ _left(); }
if (digitalRead(RIGHT)){ _right(); }
}