/*
MODDER: red90s
TITLE: BREAKOUT
//////////////////
/////00000000/////
/////00000000/////
/////00000000/////
///// /////
///// /////
/////0 /////
///// /////
///// 000 /////
//////////////////
*/
/*
#include <MD_MAX72xx.h>
#define MAX_DEVICES 4 //Cantidad de displays 8x8
//maxY o maxX dependera de la posicion y uso de los displays
const int maxX = MAX_DEVICES * 8 - 1;
const int maxY = 7;
#define CLK_PIN 6
#define DATA_PIN 23
#define CS_PIN 22
//BUTTONS
#define der 5
#define izq 16
#define sel 17
//JOYSTICK
/*
#define VERT_PIN A1
#define HORZ_PIN A0
#define SEL_PIN 2
*/
/*
MD_MAX72XX mx = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN, MAX_DEVICES);
int x = 0;
int y = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
*/
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "Breakout.h"
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
Breakout breakout;
int count = 0;
void setup() {
Serial.begin(9600);
Serial.println("8x8 LED Matrix Test");
pinMode(L_PIN, INPUT);
pinMode(R_PIN, INPUT);
matrix.begin(0x70); // pass in the address
matrix.setBrightness(0);
}
void loop() {
// action depends on game state - idealy the state should be a part of the Breakout class or a seperate logic class
switch(state){
// setup game
case SETUP:
breakout.restart();
breakout.speed = START_SPEED;
state = START;
break;
case START:
// ready to start the game, changed to PLAY if the action button is pushed.
// Change here if paddle should be able to move before you shoot the first ball and start the game.
checkActions();
if(actions == SEL){
state = PLAY;
}
break;
case LOST:
// game lost
breakout.lost();
reDraw();
blink(10, 100);
breakout.restart();
breakout.speed = START_SPEED;
state = START;
break;
case WON:
// game won
breakout.won();
reDraw();
blink(10, 100);
breakout.restart();
state = START;
break;
case PLAY:
// game in play
{
if(count > breakout.speed){
checkControls();
breakout.play(controls);
count = 0;
if(breakout.bricks == 0){
state = WON;
}
if(breakout.ball.y == 0)
state = LOST;
}
delay(20);
count++;
}
}
reDraw();
}
void reDraw(){
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(breakout.level[i][8-j-1] != 'E'){
matrix.drawPixel(i,j,1);
}
else
matrix.drawPixel(i,j,0);
}
}
matrix.writeDisplay();
}
void checkActions(){
int count = 0;
while(count < 5 && digitalRead(SELECT)){
count++;
}
if(count > 4){
actions = SEL;
return;
}
actions = NO;
}
void checkControls(){
int count = 0;
while(count < 5 && digitalRead(L_PIN)){
count++;
}
if(count > 4){
controls = LEFT;
return;
}
count = 0;
while(count < 5 && digitalRead(R_PIN)){
count++;
}
if(count > 4){
controls = RIGHT;
return;
}
controls = NONE;
}
void blink(int times, int wait){
for(int i = 0; i < times; i++){
matrix.setBrightness(2);
matrix.writeDisplay();
delay(wait);
matrix.setBrightness(0);
matrix.writeDisplay();
delay(wait);
}
}