Snake 8x16 mit zwei Matrizen MAX7219

Hallo zusammen

Ich komme nicht weiter. Ich habe ein Snake-Spiel mit einer Max7219 Matrix und wollte dieses erweitern. Natürlich funktioniert es nicht und ich wäre deshalb um jede Hilfe dankbar.

Das Original mit einer Matrix lautet:

void Drawing(int Matrix[8][8], byte x, byte y) {
 for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
     if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(0, i, j, true);
     else lc.setLed(0, i, j, false);
   }
 }
}

void ClearSnake(int Matrix[8][8]) {
 for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
     if (Matrix[i][j] == (Step - Score)) Matrix[i][j] = 0;
   }
 }
}

für die zweite Matrix wollte ich den Code so erweitern:

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 ((y <=8 ) && (Matrix[i][j] >= 1)  || ((i == x) && (j == y))) lc.setLed(1, i, j, true);
     else lc.setLed(1, i, j, false);

     if ((y >=8 ) && (Matrix[i][j] >= 8 ) || ((i == x) && (j == y)))  lc.setLed(0, i, j, true);
     else lc.setLed(0, i, j, false);
}      
  
 }
 }


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;
   }
 }
}

Habe bis jetzt noch nicht herausgefunden was es mit

(Matrix[i][j] >= 1) 

und

((i == x) && (j == y)

auf sich hat. Wäre deshalb auch um eine Erklärung diesbezüglich dankbar.

Danke für eure Hilfe

Bitte setze Deinen Sketch in Codetags (oben links </> im Foreneditor oder [code] davor und [/code] dahinter - ohne die *).
Das kannst Du auch noch nachträglich durch Editieren tun. Bitte mach das, der Sketch ist besser zu lesen, besonders auf mobilen Geräten.

Gruß Tommy

Der ganze Code mit einer Matrix lautet:

#include "LedControl.h" 
#include "Pitches.h" 
 
//Anschluss - 8x8 Matrix
const byte DIN = 6;
const byte CLK = 4;
const byte CS = 5;
LedControl lc = LedControl(DIN, CLK, CS, 1); // 1 = Anzahl Matrizen
 
//Anschluss - Steuerung
const byte btnRight = 9; //Rechts
const byte btnLeft = 12;  //Links
const byte btnUp = 11;    //Auf
const byte btnDown = 10;   //Ab
 
//Anschluss - Lautsprecher
const byte speaker = 7;
 
///////////////////////////////////////////////////////////////////

//Variablen

int Snake[8][8] = {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);
}
 
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][8], byte x, byte y) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(0, i, j, true);
      else lc.setLed(0, i, j, false);
    }
  }
}
 
void ClearSnake(int Matrix[8][8]) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; 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 == 8) ySnake = 0;
  if (xSnake == 255) xSnake = 7;
  if (ySnake == 255) ySnake = 7;
}
 
void GenerateFood() {
  lc.setLed(0, xFood, yFood, false);
  do {
    xFood = random(0, 8);
    yFood = random(0, 8);
  } 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);
}

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.

Dann solltest Du m berechnen, bevor Du es in der Ausgabe nutzt und nicht hinterher.

Einfacher wäre m = j / 8;

Gruß Tommy

Danke.

Aber m darf nur 0 oder 1 sein um die entsprechende Matrix zu aktivieren.

0 = 1. Matrix

1 = 2. Matrix

Habe schon versucht, m vorgängig zu berechnen. Dann hat er Mühe da er "j" nicht kennt.

Gibt es eine Möglichkeit die Definition mittels if/else in der besagten Schlaufe einzufügen?

Kennst Du Deine eigenen Postings nicht mehr?

decpad:
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.

Natürlich kennt er hier j. Er führt j ja in der for-Schleife von 0 bis 15 und in diesem Bereich ist j/8 entweder 0 oder 1 und damit das, was Du für m brauchst.
Wenn Du es nicht glaubst, Bau eine Schleife aus und lass Dir j und m ausgeben.

Es bringt nur nichts, wenn Du m erst nach der Verwendung bestimmst.

Gruß Tommy