Bonjour a tous,
J'ai découvert l'Arduino, l’électronique et le code, il y a moins d'un ans.
Je viens de réaliser le jeu Snake sur une matrice de led 8x8 trois couleurs (Adafruit).
Tout marche nickel.
N'ayant aucune expérience, j'aimerai avoir l'avis d'expert pour mon code, clarté, simplicité, bonne pratique etc... il y aurait il des volontaires
N’hésitez pas, je cherche a progresser et a faire le mieux possible.
J'aimerai enchainer sur un casse brique et j'aimerai savoir si je suis sur la bonne voie.
MERCI
//***************************** LIBRARIES ***
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
//****************************** VARIALBE ***
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
const int switchPinTurnLeft=8;
const int switchPinTurnRight=5;
int switchValueTurnLeft=0;
int switchValueTurnRight=0;
int headPositionX=1;
int headPositionY=1;
int snakeDirection=0;
int tailPositionMemoryX[40];
int tailPositionMemoryY[40];
int tailLength=0;
int beaconPositionX=0;
int beaconPositionY=0;
int flagBeaconOnSnake=0;
int flagOneTimePerStep=0;
int gameSpeed=1000;
unsigned long nextStepTime=0;
//**************************** VOID SETUP ***
void setup(){
Serial.begin(9600);
matrix.begin(0x70);
pinMode(switchPinTurnLeft, INPUT);
pinMode(switchPinTurnRight, INPUT);
randomSeed(analogRead(0));
beaconNewPosition();
}
//***************************** VOID LOOP ***
void loop(){
if (nextStepTime<=millis()){
nextStepTime=nextStepTime+gameSpeed;
flagOneTimePerStep=0;
snakeDisplacement();
checkSnakeItSelf();
snakeDisplay();
checkEatBeacon();
}
checkButtons();
}
//******************************* DISPLAY ***
void snakeDisplay(){
matrix.clear();
//HEAD
matrix.drawPixel(headPositionX, headPositionY, LED_GREEN);
//TAIL
for (int i=2; i<=tailLength+1 ;i++){
matrix.drawPixel(tailPositionMemoryX[i], tailPositionMemoryY[i], LED_GREEN);
}
//BEACON
matrix.drawPixel(beaconPositionX, beaconPositionY, LED_YELLOW);
matrix.writeDisplay();
}
//************************* CHECK BUTTONS ***
void checkButtons(){
switchValueTurnLeft=digitalRead(switchPinTurnLeft);
switchValueTurnRight=digitalRead(switchPinTurnRight);
if (flagOneTimePerStep==0){
if (switchValueTurnLeft==1){
flagOneTimePerStep=1;
snakeDirection=snakeDirection-1;
}
else if (switchValueTurnRight==1){
flagOneTimePerStep=1;
snakeDirection=snakeDirection+1;
}
delay(100);
}
}
//********************** CHECK EAT BEACON ***
void checkEatBeacon(){
if (headPositionX==beaconPositionX && headPositionY==beaconPositionY){
tailLength=tailLength+1;
beaconNewPosition();
if (gameSpeed>=280){
//gameSpeed=gameSpeed-80;
}
}
}
//******************** SNAKE DISPLACEMENT ***
void snakeDisplacement(){
if (snakeDirection==4){
snakeDirection=0;
}
if (snakeDirection==-1){
snakeDirection=3;
}
//HEAD DISPLACEMENT
switch (snakeDirection){
case 0:
headPositionX++;
break;
case 1:
headPositionY++;
break;
case 2:
headPositionX--;
break;
case 3:
headPositionY--;
}
checkBorderLimit();
//TAIL DISPLACEMENT
for (int i=tailLength+1; i>1; i--){
tailPositionMemoryX[i]=tailPositionMemoryX[i-1];
tailPositionMemoryY[i]=tailPositionMemoryY[i-1];
}
tailPositionMemoryX[1]=headPositionX;
tailPositionMemoryY[1]=headPositionY;
}
//******************** CHECK BORDER LIMIT ***
void checkBorderLimit(){
//CHECK LEFT BORDER
if (headPositionX==8){
headPositionX=7;
if (headPositionY==7){
headPositionY--;
snakeDirection=3;
}
else {
headPositionY++;
snakeDirection=1;
}
}
//CHECK RIGHT BORDER
else if (headPositionX==-1){
headPositionX=0;
if (headPositionY==0){
headPositionY++;
snakeDirection=1;
}
else {
headPositionY--;
snakeDirection=3;
}
}
//CHECK TOP BORDER
else if (headPositionY==8){
headPositionY=7;
if (headPositionX==0){
headPositionX++;
snakeDirection=0;
}
else {
headPositionX--;
snakeDirection=2;
}
}
//CHECK BOTTOM BORDER
else if (headPositionY==-1){
headPositionY=0;
if (headPositionX==7){
headPositionX--;
snakeDirection=2;
}
else {
headPositionX++;
snakeDirection=0;
}
}
}
//************************** SNAKE ITSELF ***
void checkSnakeItSelf(){
for (int i=2; i<tailLength+2; i++){
if (headPositionX==tailPositionMemoryX[i] && headPositionY==tailPositionMemoryY[i]){
gameOver();
}
}
}
//******************* BEACON NEW POSITION ***
void beaconNewPosition(){
//CHECK BEACON NEW POSITION NOT ON SNAKE
do {
flagBeaconOnSnake=0;
beaconPositionX=random(0,8);
beaconPositionY=random(0,8);
for (int i=2; i<tailLength+2; i++){
if (beaconPositionX==tailPositionMemoryX[i] && beaconPositionY==tailPositionMemoryY[i] || beaconPositionX==headPositionX && beaconPositionY==headPositionY){
flagBeaconOnSnake=1;
}
}
}
while (flagBeaconOnSnake==1);
}
//***************************** GAME OVER ***
void gameOver(){
delay(1200);
for (int i=0; i<4; i++){
matrix.drawRect(0+i,0+i, 8-(i*2),8-(i*2), LED_RED);
matrix.writeDisplay();
delay(800);
}
delay(1500);
matrix.setTextWrap(false);
matrix.setTextSize(1);
matrix.setTextColor(LED_GREEN);
matrix.setRotation(1);
for (int iii=1; iii<3; iii++){
for (int ii=1; ii<3; ii++){
for (int i=7; i>=-30; i--) {
matrix.clear();
matrix.setCursor(i,0);
switch (ii){
case 1:
matrix.print("Score");
break;
case 2 :
matrix.print(tailLength);
}
matrix.writeDisplay();
delay(130);
}
}
}
delay(10000);
//RAZ NEW GAME
tailLength=0;
headPositionX=1;
headPositionY=1;
gameSpeed=1000;
nextStepTime=0;
matrix.clear();
matrix.writeDisplay();
beaconNewPosition();
}