Ich habe noch etwa 100 Varianten herausgefunden, wie es nicht funktioniert ;-(
#include "LedControl.h"
#include "Pitches.h"
//Anschluss - 8x8 Matrix
const byte DIN = 12;
const byte CLK = 11;
const byte CS = 10;
LedControl lc = LedControl(DIN, CLK, CS, 2); // 2 = Anzahl Matrizen
//Anschluss - Steuerung
const byte btnRight = 7; //Rechts
const byte btnLeft = 6; //Links
const byte btnUp = 5; //Auf
const byte btnDown = 4; //Ab
//Anschluss - Lautsprecher
const byte speaker = 9;
///////////////////////////////////////////////////////////////////
//Variablen
int Snake[8][16] = {0};
byte xSnake = 4;
byte ySnake = 5;
byte xFood = 0;
byte yFood = 0;
byte Direction = 0;
int Step = 1;
long TimeChange = 0;
int Speed = 500;
byte Score = 0;
boolean Eat = false;
boolean Sound = true;
boolean Collision = false;
///////////////////////////////////////////////////////////////////
//Definition Melodie
//noteDurations = Notendauer
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
//Definition Zahlen
int Numbers[11][7] = {
{B00000000,B00000000,B00000000,B00000000,B00000001,B01111111,B00100001}, //1
{B00000000,B00000000,B00110001,B01001001,B01000101,B00100011}, //2
{B00000000,B00000000,B01000110,B01101001,B01010001,B01000001,B01000010}, //3
{B00000000,B00001000,B11111111,B01001000,B00101000,B00011000,B00001000}, //4
{B00000000,B00000000,B00111110,B01000101,B01001001,B01010001,B00111110}, //5
{B00000000,B00000000,B00000110,B01001001,B01001001,B00101001,B00011110}, //6
{B00000000,B00000000,B01110000,B01001000,B01000111,B01000000,B01100000}, //7
{B00000000,B00000000,B00110110,B01001001,B01001001,B01001001,B00110110}, //8
{B00000000,B00000000,B00111100,B01001010,B01001001,B01001001,B00110000}, //9
{B00000000,B00000000,B00111110,B01010001,B01001001,B01000101,B00111110}, //0
{B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000}, //_
};
void setup() {
Serial.begin(9600);
Serial.println("Snake Game Intialisierung");
randomSeed(analogRead(A0));
GenerateFood();
pinMode(btnLeft, INPUT_PULLUP);
pinMode(btnRight, INPUT_PULLUP);
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
pinMode(speaker, OUTPUT);
//Loading Matrix
lc.shutdown(0, false);
lc.setIntensity(0, 1);
lc.clearDisplay(0);
lc.shutdown(1, false);
lc.setIntensity(1, 1);
lc.clearDisplay(1);
}
void loop() {
if ((digitalRead(btnLeft) == false) && (Direction != 0)) Direction = 1;
if ((digitalRead(btnRight) == false) && (Direction != 1)) Direction = 0;
if ((digitalRead(btnUp) == false) && (Direction != 3)) Direction = 2;
if ((digitalRead(btnDown) == false) && (Direction != 2)) Direction = 3;
if ((millis() - TimeChange) > Speed) {
TimeChange = millis();
ClearSnake(Snake);
Scores();
Move();
if (xSnake == xFood && ySnake == yFood) Eat = true;
if (Snake[xSnake][ySnake] != 0) GameOver();
Step++;
Snake[xSnake][ySnake] = Step;
}
Drawing(Snake, xFood, yFood);
}
///////////////////////////////////////////////////////////////////
//Funktionen
void Scores() {
if (Eat == true) {
Eat = false;
GenerateFood();
Score++;
tone(speaker, NOTE_C5, 200);
Speed -= 20;
Serial.print ("Score is: ");
Serial.println (Score);
}
}
void Drawing(int Matrix[8][16], byte x, byte y)
{for (int i = 0; i < 8; i++)
{for (int j = 0; j < 16; j++)
{if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(m, i, j, true); else lc.setLed(m, i, j, false);
if (j<8) byte m=0;
if (j>8) byte m=1;
}
}
}
void ClearSnake(int Matrix[8][16]) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 16; j++) {
if (Matrix[i][j] == (Step - Score)) Matrix[i][j] = 0;
}
}
}
void Move() {
switch (Direction) {
case 0:
xSnake++;
break;
case 1:
xSnake--;
break;
case 2:
ySnake++;
break;
case 3:
ySnake--;
break;
}
if (xSnake == 8) xSnake = 0;
if (ySnake == 16) ySnake = 0;
if (xSnake == 255) xSnake = 7;
if (ySnake == 255) ySnake = 15;
}
void GenerateFood() {
lc.setLed(0, xFood, yFood, false);
do {
xFood = random(0, 8);
yFood = random(0, 16);
} while (Snake[xFood][yFood] != 0 );
}
void DrawingNumbers(byte Number){
if(Number ==0) Number = 10;
for (int col=0; col<7; col++) {
lc.setRow(0, col, Numbers[Number-1][col]);
}
}
void GameOver() {
Serial.println ("Game over");
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (Sound == true) {
int noteDuration = 1000 / noteDurations[col];
tone(speaker, melody[col], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(speaker);
}
lc.setLed(0,col,row, true);
delay(25);
}
Sound = false;
}
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
lc.setLed(0, col, row, false);
delay(25);
}
}
do {
DrawingNumbers((Score/10)%10);
delay(200);
DrawingNumbers(Score%10);
delay(200);
DrawingNumbers(11);
delay(1000);
} while (true);
}
Das Problem liegt hier:
void Drawing(int Matrix[8][16], byte x, byte y)
{for (int i = 0; i < 8; i++)
{for (int j = 0; j < 16; j++)
{if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(m, i, j, true); else lc.setLed(m, i, j, false);
if (j<8) byte m=0;
if (j>8) byte m=1;
}
}
}
Wenn y bzw. j kleiner als 8 ist, sollte m den Wert 0 und wenn haben und wenn y bzw. j grösser als 8 ist, sollte m den Wert 1 haben, damit die richtige Matrix angesprochen wird.