Das Spiel Snake auf einer 24x16 LED Matrix von Sure electronics
Bilder
Sourcecode
// ********************** // Sure Matrix 24x16 // **********************
include <TimerOne.h>
include <Bounce.h>
include "MatrixDisplay.h"
include "DisplayToolbox.h"
// Macro to make it the initDisplay function a little easier to understand
define setMaster(dispNum, CSPin) initDisplay(dispNum,CSPin,true)
define setSlave(dispNum, CSPin) initDisplay(dispNum,CSPin,false)
// 1 = Number of displays // Data = 6 // WR == 7 // True. Do you want a shadow buffer? (A scratch pad)
// Init Matrix MatrixDisplay disp(1, 6, 7, false); // Pass a copy of the display into the toolbox DisplayToolbox toolbox(&disp);
define COLS 24
define ROWS 16
define MATRIX COLS * ROWS
byte matrix[MATRIX]; uint16_t snake[MATRIX]; int snakehead = 0; int oldsnakehead = 0; int startingpoint = 150; int direction = 1; boolean buttonPressed = false;
int applecaught = 0; int apple = -1; int applecount = 0; int blinkApple = 1; unsigned long lastAppleMillis = 0; int blinkSpeed = 100;
int button1 = 9; // Left int button2 = 10; // Up int button3 = 11; // Down int button4 = 8; // Right
int moveSpeed = 80; unsigned long lastMillis = 0;
Bounce bouncer1 = Bounce(button1, 10); Bounce bouncer2 = Bounce(button2, 10); Bounce bouncer3 = Bounce(button3, 10); Bounce bouncer4 = Bounce(button4, 10); int button1value = LOW; int button2value = LOW; int button3value = LOW; int button4value = LOW;
// initialization void setup() {
randomSeed(analogRead(5));
// buttons to input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
// Enable internal pullup resistors
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
// Timer1 controls output on displays
Timer1.initialize(20000);
Timer1.attachInterrupt(outputDisplay);
buttonPressed = false;
// Prepare display
disp.setMaster(0,4);
resetSnake();
}
// call by timer1 void outputDisplay() {
for(int i=0; i<COLS; i++)
for(int j=0; j<ROWS; j++)
{
int value = matrix[i * ROWS + j];
if ((i * ROWS + j) == apple)
{
if (millis() - lastAppleMillis >= blinkSpeed)
{
blinkApple = !blinkApple;
lastAppleMillis = millis();
}
value = blinkApple;
}
toolbox.setPixel(i, j, value);
}
disp.syncDisplays();
}
// Create empty snake void resetSnake() {
for(int i=0; i<MATRIX; i++)
{
snake[i] = -1;
}
clearMatrix();
// Add starting dot
matrix[startingpoint] = 1;
snakehead = startingpoint;
snake[0] = snakehead;
generateApple();
matrix[apple] = 1;
direction = 1;
applecount = 0;
}
void moveSnake() {
if (millis() - lastMillis >= moveSpeed)
{
oldsnakehead = snakehead;
switch(direction) {
case 0:
// up
if ((snakehead - ROWS) < 0)
snakehead += (ROWS * (COLS - 1));
else
snakehead -= ROWS;
break;
case 1:
// right
if ((snakehead - (snakehead % ROWS)) == (((snakehead+1) - (snakehead+1) % ROWS)))
snakehead += 1;
else
snakehead = snakehead - (ROWS - 1); // to start of row
break;
case 2:
// down
if ((snakehead + ROWS) > ((ROWS * COLS - 1)))
snakehead = snakehead % ROWS;
else
snakehead += ROWS;
break;
case 3:
// left
if (snakehead == 0)
// exception for topleft, pixel 0
snakehead += (ROWS - 1);
else
{
if ((snakehead - (snakehead % ROWS)) == (((snakehead-1) - (snakehead-1) % ROWS)))
snakehead -= 1;
else
snakehead += (ROWS - 1); // end of row
}
break;
default:
// wtf?
break;
}
// Check if we have the apple
if (snakehead == apple)
{
// Got it!
applecaught = 1;
}
// check if we hit the snake
for (int i=1; i<MATRIX; i++)
{
if (snake[i] == snakehead)
endGame();
}
// Add new dot to end of snake
for(int i=MATRIX-1; i>0; i--)
{
snake[i] = snake[i-1];
}
snake[0]=snakehead;
for(int n=0; n<MATRIX-1; n++)
{
if (snake[n] == -1)
{
if (applecaught == 0)
snake[n-1] = -1; // undo last, we are moving on
break;
}
}
if (applecaught == 1)
{
generateApple();
applecaught = 0;
applecount++;
}
updateMatrix();
lastMillis = millis();
}
}
// Update snake in matrix void updateMatrix() {
clearMatrix();
for (int i=1; i<MATRIX; i++) {
if (snake[i] == -1)
break;
matrix[snake[i]] = 1;
}
matrix[snakehead] = 1;
matrix[apple] = 1;
}
// Clear matrix (all LEDs off) void clearMatrix() {
for (int i=0; i<MATRIX; i++) {
matrix[i] = 0;
}
}
// Generate random positon for apple void generateApple() {
apple = random(MATRIX);
for (int n=0; n<MATRIX; n++)
{
if (snake[n] == apple)
generateApple();
}
}
// Game over void endGame() {
delay(500);
apple = -1;
for (int i=0; i<COLS; i++)
{
for (int n=0; n<ROWS; n++)
matrix[n + (i * ROWS)] = 1;
delay(100);
}
delay(1000);
resetSnake();
}
void loop() {
bouncer1.update();
bouncer2.update();
bouncer3.update();
bouncer4.update();
if (bouncer1.read() == LOW) {
if (button1value == HIGH) {
buttonPressed = true;
if (direction != 1)
direction = 3; //LEFT
}
}
button1value = bouncer1.read();
if (bouncer2.read() == LOW) {
if (button2value == HIGH) {
buttonPressed = true;
if (direction != 2)
direction = 0; // UP
}
}
button2value = bouncer2.read();
if (bouncer3.read() == LOW) {
if (button3value == HIGH) {
buttonPressed = true;
if (direction != 0)
direction = 2; // DOWN
}
}
button3value = bouncer3.read();
if (bouncer4.read() == LOW) {
if (button4value == HIGH) {
buttonPressed = true;
if (direction != 3)
direction = 1; // RIGHT
}
}
button4value = bouncer4.read();
moveSnake();
}
Video





