Last active
August 29, 2015 14:14
-
-
Save Happsson/8db098491245ecc0ec74 to your computer and use it in GitHub Desktop.
Snake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LedControlMS.h" | |
#include <math.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#define NBR_MTX 2 | |
LedControl lc=LedControl(12,11,10, NBR_MTX); | |
int speed = 100; | |
//numbers 0-9. Kan kombineras genom att förskjuta 4 steg i x-led | |
int n[11][8][8] = {{ | |
{0,0,0,0,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{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,1,0,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,1,1,1,0,0,0,0}, | |
{0,0,0,0,0,0,0,0} | |
}, | |
{ | |
{0,0,0,0,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,1,0,0,0,0,0,0}, | |
{1,1,1,1,0,0,0,0}, | |
{0,0,0,0,0,0,0,0} | |
}, | |
{ | |
{0,0,0,0,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{0,0,0,0,0,0,0,0} | |
}, | |
{ | |
{0,0,0,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,1,1,1,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{0,0,0,1,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,0}, | |
{1,0,0,0,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{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,1,1,0,0,0,0,0}, | |
{1,0,0,0,0,0,0,0}, | |
{1,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,1,1,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,0}, | |
{0,0,0,1,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,0,1,0,0,0,0,0}, | |
{0,1,0,0,0,0,0,0}, | |
{0,1,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,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{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,1,1,0,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{1,0,0,1,0,0,0,0}, | |
{0,1,1,1,0,0,0,0}, | |
{0,0,0,1,0,0,0,0}, | |
{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,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,0,0,0}, | |
{0,0,0,0,0,0,0,0}, | |
{0,0,0,0,0,0,0,0} | |
}}; | |
const int LRin = A0; //left / right | |
const int UDin = A1; //up / down. | |
bool blinkCandy = true; //switch every loop to blink candy. | |
int moveDir = 0; //0 = höger, 1 = upp, 2 = vänster, 3 = ner. | |
int length = 1; | |
int last[64][2]; //plats för en orm som tar upp alla lampor | |
int x = 3; | |
int y = 3; | |
int lastId = 0; //Id i arrayen. | |
int c = 0; //autoplayer counter | |
bool collision = false; //Set to true if collision. | |
bool incLength = false; | |
int candyPos[2]; | |
bool candyOnScreen = false; | |
void setup() { | |
Serial.begin (9600); | |
for(int i = 0; i < 64; i++){ //Sätt alla punkter utanför spelplanen | |
last[i][0] = -1; | |
last[i][1] = -1; | |
} | |
pinMode(5, INPUT); | |
for (int i=0; i< NBR_MTX; i++){ | |
lc.shutdown(i,false); | |
/* Set the brightness to a medium values */ | |
lc.setIntensity(i,10); | |
/* and clear the display */ | |
lc.clearDisplay(i); | |
} | |
} | |
int looper = 0; | |
//The int looper is used instead of a delay(), so I can check the input for the controller more often without having to deal with | |
//interrupts. | |
void loop() { | |
looper++; | |
looper = looper % speed; | |
CheckMove(); | |
if(looper==0) DoMove(); | |
//c++; //not used in actual game. Used to generate a perfect game. | |
if(looper==0) checkCollision(); | |
if(collision) { //vid kollision är spelet över | |
//delay(500); | |
GameOver(); | |
} | |
candyCollision(); //kolla om man tagit godisen | |
while(!candyOnScreen) createCandy(); //radnomiza fram ny godis om det behövs. loopa tills det dyker upp en | |
if(looper % 100 == 0) candyBlink(); | |
delay(1); | |
} | |
void GameOver(void){ | |
//Spelet slut, fastna i oändlig loop. | |
while(1){ | |
lc.clearDisplay(0); | |
delay(100); | |
PS(length-1); //length-1 is the same as number of candies collected. This is the score. | |
delay(500); | |
} | |
} | |
//print score | |
void PS(int a){ | |
int nr1; | |
int nr2; | |
if(a > 10){ | |
nr1 = a / 10; | |
nr2 = a - (nr1 * 10); | |
} | |
else if(a==10){ | |
nr1 = 1; | |
nr2 = 0; | |
} | |
else{ | |
nr1 = 10; | |
nr2 = a; | |
} | |
for(int x1 = 0; x1 < 8; x1++){ | |
for(int y1 = 0; y1 < 8; y1++){ | |
if(n[nr1][x1][y1] == 1) lc.setLed(0,y1,7-x1,true); | |
} | |
} | |
for(int x1 = 0; x1 < 8; x1++){ | |
for(int y1 = 0; y1 < 8; y1++){ | |
if(n[nr2][x1][y1] == 1) lc.setLed(0,y1+4,7-x1,true); | |
} | |
} | |
} | |
void candyBlink(void){ | |
if(blinkCandy) lc.setLed(0,candyPos[0],candyPos[1],true); | |
else lc.setLed(0,candyPos[0],candyPos[1],false); | |
blinkCandy = !blinkCandy; | |
} | |
void Led(int x, int y){ | |
if(!incLength){ | |
lc.setLed(0,last[0][0], last[0][1],false); //släck längst bak, endast om vi inte ska göra ormen längre. | |
//Bygg om listan med tända lampor varje gång. index [0] är alltid sista lampan i svansen, index [length] är alltid "huvudet" | |
//om ormen ska bli längre skippas denna loop. | |
for(int i = 0; i < length; i++){ | |
last[i][0] = last[i+1][0]; | |
last[i][1] = last[i+1][1]; | |
} | |
} | |
else{ //ormen ska göras längre, incrementera positionen length med 1 bara. | |
length++; | |
incLength = false; | |
} | |
last[length][0] = x; | |
last[length][1] = y; | |
lc.setLed(0,last[length][0],last[length][1],true); // tänd ny pos | |
} | |
void CheckMove(void){ | |
//perfect game. | |
/*c = c%64; | |
if(c == 0 || c == 15 || c == 29 || c == 43) moveDir = 0; //höger | |
if(c == 7 || c == 14 || c == 21 || c == 28 || c == 35 || c == 42 || c == 49) moveDir = 3; //ner | |
if(c == 8 || c == 22 || c == 36 || c == 50) moveDir = 2; //vänster | |
if(c == 57) moveDir = 1; //upp | |
*/ | |
int LR = analogRead(LRin); | |
int UD = analogRead(UDin); | |
if(LR > 600 && moveDir != 0) moveDir = 2; //höger, bara om man inte för tillfället kör åt vänster | |
if(LR < 100 && moveDir != 2) moveDir = 0; //vänster, om man inte kör åt höger. | |
if(UD > 600 && moveDir != 3) moveDir = 1; //Ner | |
if(UD < 100 && moveDir != 1) moveDir = 3; //upp | |
} | |
void DoMove(void){ | |
if(moveDir == 0) MoveRight(); | |
if(moveDir == 1) MoveUp(); | |
if(moveDir == 2) MoveLeft(); | |
if(moveDir == 3) MoveDown(); | |
Led(x,y); | |
speed = 0 - 670 + analogRead(A3); //also check speed | |
} | |
void IncrementLength(void){ | |
incLength = true; | |
} | |
void MoveRight(void){ | |
x++; | |
} | |
void MoveLeft(void){ | |
x--; | |
} | |
void MoveUp(void){ | |
y--; | |
} | |
void MoveDown(void){ | |
y++; | |
} | |
//create candy | |
void createCandy(void){ | |
int xR = rand() % 8; | |
int yR = rand() % 8; | |
for(int i = 0; i <= length; i++){ | |
if(xR == last[i][0] && yR == last[i][1]){ | |
candyOnScreen = false; | |
return; | |
} | |
} | |
candyPos[0] = xR; | |
candyPos[1] = yR; | |
candyOnScreen = true; | |
lc.setLed(0,xR,yR,true); | |
} | |
void candyCollision(){ | |
if(last[length][0] == candyPos[0] && last[length][1] == candyPos[1]){ | |
candyOnScreen = false; | |
IncrementLength(); | |
} | |
} | |
// checks collision with snake and wall. | |
void checkCollision(){ | |
int xP = last[length][0]; | |
int yP = last[length][1]; | |
if(xP == -1 || xP == 8 || yP == -1 || yP == 8){ | |
collision = true; | |
return; | |
} | |
else{ | |
for(int i = 0; i < length; i++){ | |
if(last[i][0] == xP && last[i][1] == yP){ | |
collision = true; | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment