0% found this document useful (0 votes)
41 views9 pages

Tetris

This document defines global variables and functions for controlling LED matrices to display falling blocks in a Tetris-style game. It includes: 1) Global variables for the LED controller, button pins, block position/orientation, and previous block state. 2) Functions to translate blocks right/left/down, set individual LEDs on/off to display I and S block shapes, and get LED status. 3) Main setup() initializes the LED controller and buttons. The main loop() randomly selects a block, calls functions to move and display it, checking for collisions until the block reaches the bottom or a new game is started.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Tetris

This document defines global variables and functions for controlling LED matrices to display falling blocks in a Tetris-style game. It includes: 1) Global variables for the LED controller, button pins, block position/orientation, and previous block state. 2) Functions to translate blocks right/left/down, set individual LEDs on/off to display I and S block shapes, and get LED status. 3) Main setup() initializes the LED controller and buttons. The main loop() randomly selects a block, calls functions to move and display it, checking for collisions until the block reaches the bottom or a new game is started.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <LedControl.

h>
#include <MatrizLed.h>
//We always have to include the library
#include "LedControl.h"

//***************Global Variables and


Definitions******************************************
LedControl lc = LedControl(12, 11, 10, 1);
const int SW_pin = 2; // digital pin connected to SW
const int X_pin = A0; // analog pin connected to VRx
const int Y_pin = A1;
int boni = 3;
int bond = 4;
int bona = 5;
int start_button = 6;
float r = 7.0; // row input
int c = 3; // column input
int add = 0; // address input
int add_prev = 1000;
int r_prev = 1000;
int c_prev = 1000;
int ro = 0; // to set block orientation
int ro_prev;
int c2;
float r2;
int check1, check2, check3, check4;
int left_check1, left_check2, left_check3, left_check4;
int right_check1, right_check2, right_check3, right_check4;
int block = 0;

//*******************Right Translate Functions**************************


int Right_translate( int c1, boolean state1) {
int X = 500;
if (digitalRead(bond) == LOW) {
X = 100;
}
if (state1 == true && X < 200) {
return c--;
}
else if (state1 == false && X < 200) {
return c;
}
}

//*******************Left Translate Functions**************************


int Left_translate( int c1, boolean state1) {
//Agregar boton hacia la izquierda, leer el valor del boton tal que si está
actviado = HIGH
int X = 0;
if (digitalRead(boni) == LOW) {
X = 1000;
}

if (state1 == true) {
if (X > 800) {
return c++;
}
}
else if (state1 == false) {
if (X > 800) {
return c;
}
}
}

//*******************Down Translate Functions**************************


float Down_translate( float r1, boolean state1) {
int Y = 0;
if (digitalRead(bona) == LOW) {
Y = 1000;
}
//Serial.print(" Y_pin: ");
// Serial.print(Y);
if (state1 == true ) {
if (Y > 800) {
return r--;
}
}
else if (state1 == false) {
if (Y > 800) {
return r;
}
}
}

//************************************ I BLOCK FUNCTION


**************************************
void I_block(int add, float r, int c, int ro) {

switch (ro) {

case 0: // Horizontal
Orientation
if (ro_prev == 1) {
lc.setLed(add_prev, r_prev, c_prev, false);
lc.setLed(add_prev, r_prev - 1, c_prev, false);
lc.setLed(add_prev, r_prev - 2, c_prev, false);
lc.setLed(add_prev, r_prev - 3, c_prev, false);
}

lc.setLed(add_prev, r_prev, c_prev, false);


lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev, c_prev - 2, false);
lc.setLed(add_prev, r_prev, c_prev - 3, false);

lc.setLed(add, r, c, true);
lc.setLed(add, r, c - 1, true);
lc.setLed(add, r, c - 2, true);
lc.setLed(add, r, c - 3, true);

check1 = lc.getstatus(add, r - 1, c);


check2 = lc.getstatus(add, r - 1, c - 1);
check3 = lc.getstatus(add, r - 1, c - 2);
check4 = lc.getstatus(add, r - 1, c - 3);
left_check1 = lc.getstatus(add, r , c + 1);
right_check1 = lc.getstatus(add, r , c - 4);
add_prev = add;
r_prev = r;
c_prev = c;
ro_prev = ro;

break;

case 1: // Vertical
Orientation
if (ro_prev == 0) {
lc.setLed(add_prev, r_prev, c_prev, false);
lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev, c_prev - 2, false);
lc.setLed(add_prev, r_prev, c_prev - 3, false);
}

lc.setLed(add_prev, r_prev, c_prev, false);


lc.setLed(add_prev, r_prev - 1, c_prev, false);
lc.setLed(add_prev, r_prev - 2, c_prev, false);
lc.setLed(add_prev, r_prev - 3, c_prev, false);

lc.setLed(add, r, c, true);
lc.setLed(add, r - 1, c, true);
lc.setLed(add, r - 2, c, true);
lc.setLed(add, r - 3, c, true);

check1 = lc.getstatus(add, r - 4, c);


left_check1 = lc.getstatus(add, r , c + 1);

right_check1 = lc.getstatus(add, r , c - 1);

add_prev = add;
r_prev = r;
c_prev = c;
ro_prev = ro;

break;

//************************************ S BLOCK FUNCTION


**************************************
void S_block(int add, float r, int c, int ro) {

switch (ro) {

case 0: //
Horizontal Orientation
if (ro_prev == 1) {

lc.setLed(add_prev, r_prev, c_prev, false);


lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev - 1, c_prev - 1, false);
lc.setLed(add_prev, r_prev + 1, c_prev, false);
}
lc.setLed(add_prev, r_prev, c_prev, false);
lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev - 1, c_prev, false);
lc.setLed(add_prev, r_prev - 1, c_prev + 1, false);

lc.setLed(add, r, c, true);
lc.setLed(add, r, c - 1, true);
lc.setLed(add, r - 1, c, true);
lc.setLed(add, r - 1, c + 1, true);

check1 = lc.getstatus(add, r - 2, c);


check2 = lc.getstatus(add, r - 2, c + 1);
check3 = lc.getstatus(add, r - 1, c - 1);
left_check1 = lc.getstatus(add, r , c + 2);
right_check1 = lc.getstatus(add, r , c - 2);

add_prev = add;
r_prev = r;
c_prev = c;
ro_prev = ro;

break;

case 1: //
Vertical Orientation
if (ro_prev == 0) {
lc.setLed(add_prev, r_prev, c_prev, false);
lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev - 1, c_prev, false);
lc.setLed(add_prev, r_prev - 1, c_prev + 1, false);
}

lc.setLed(add_prev, r_prev, c_prev, false);


lc.setLed(add_prev, r_prev, c_prev - 1, false);
lc.setLed(add_prev, r_prev - 1, c_prev - 1, false);
lc.setLed(add_prev, r_prev + 1, c_prev, false);

lc.setLed(add, r, c, true);
lc.setLed(add, r, c - 1, true);
lc.setLed(add, r - 1, c - 1, true);
lc.setLed(add, r + 1, c, true);

check1 = lc.getstatus(add, r - 1, c);


check2 = lc.getstatus(add, r - 2, c - 1);
left_check1 = lc.getstatus(add, r , c + 1);
right_check1 = lc.getstatus(add, r , c - 2);

add_prev = add;
r_prev = r;
c_prev = c;
ro_prev = ro;
break;
}
}

//**************************************** MAIN SETUP


****************************
MatrizLed pantalla;
void setup() {
//we have already set the number of devices when we created the LedControl
int devices = lc.getDeviceCount();
//lc.line_break();
// int che = lc.getstatus(add, r, c);

pantalla.begin(12, 11, 10, 1); // dataPin, clkPin, csPin, numero de matrices de


8x8
pantalla.rotar(false);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
randomSeed(analogRead(0));
Serial.begin(9600);
//we have to init all devices in a loop
for (int address = 0; address < devices; address++) {
/*The MAX72XX is in power-saving mode on startup*/
lc.shutdown(address, false);
/* Set the brightness to a medium values */
lc.setIntensity(address, 8);
/* and clear the display */
lc.clearDisplay(address);
pinMode(boni, INPUT);
pinMode(bond, INPUT);
pinMode(bona, INPUT);
pinMode(start_button, INPUT);
}
}

void Gi_block() {
int Rot = digitalRead(SW_pin);
if (Rot == LOW) {
ro++;
if (ro == 2) {
ro = 0;
}
}
}

void Gs_block() {
int Rot = digitalRead(SW_pin);
if (Rot == LOW) {
ro++;
if (ro == 2) {
ro = 0;
}
}
}

int estado;
int started = 0;

void splashScreen() {
pantalla.escribirFraseScroll("TETRIS", 50);
}

void reestarted() {
lc.clearDisplay(0);
delay(100);
}

//***************************** MAIN LOOP FUNCTION ****************************


void loop() {
block = random(-1, 2); // Block selection by randomly picking value
between 0 and 1
r = 8; // Starting row for new block
c = 3; // starting column for new block
add = 0; // address is 0 if using only one 8x8 LED module

if (Serial.available() > 0) {
estado = Serial.read();
if (estado == 'S') {
started = 1;
}
if (estado == 'E') {
reestarted();
started = 0;
}
}
if (started == 0)
{
splashScreen();

if (digitalRead(start_button) == LOW)
{
started = 1;
}
} else {

switch (block) {
//*********************** I_block Horizontal ****************
case 1:
do {

if (Serial.available() > 0) {
estado = Serial.read();
if (estado == 'E') {
Gi_block();
}
}
Gi_block();
if (c >= -1 && c <= 8) {
switch (ro) {
case 0:
if (c == 2 || c == 1 || c == 0) {
c = 3;
}
if (c == 8) {
c = 7;
}
if (left_check1 == 1 && right_check1 == 0) {
c = Right_translate(c, true);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, true);
Down_translate(r, true);
}
else if (left_check1 == 1 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 0) {
c2 = Right_translate(c, true);
c2 = Left_translate(c, true);
r2 = Down_translate(r, true);
}
break;
//*************************** I block_Vertical **************
case 1:
if (c == -1) {
c = 0;
}
if (c == 8) {
c = 7;
}
if (left_check1 == 1 && right_check1 == 0) {
c = Right_translate(c, true);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, true);
Down_translate(r, true);
}
else if (left_check1 == 1 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 0) {
c2 = Right_translate(c, true);
c2 = Left_translate(c, true);
r2 = Down_translate(r, true);
}
break;
}
}
r = r - 0.125;
I_block(add, r, c, ro);
if (check1 == 1 || check2 == 1 || check3 == 1) {
r = 0.8;
}
delay(225);
} while (r > 0.9 );
add_prev = 100;
r_prev = 100;
c_prev = 100;
break;
//*****************S_Block case*****************
case 0:
do {
if (Serial.available() > 0) {
estado = Serial.read();
if (estado == 'I') {
Gs_block();
}
} Gs_block();

if (c >= 0 && c <= 7) {

switch (ro) {
case 0:
if ( c == 0) {
c = 1;
}
if (c == 7) {
c = 6;
}
if (left_check1 == 1 && right_check1 == 0) {
c = Right_translate(c, true);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, true);
Down_translate(r, true);
}
else if (left_check1 == 1 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 0) {
c2 = Right_translate(c, true);
c2 = Left_translate(c, true);
r2 = Down_translate(r, true);
}
break;
//***************************S block**************
case 1:
if (c == -1) {
c = 0;
}
if (c == 8) {
c = 7;
}
if (left_check1 == 1 && right_check1 == 0) {
c = Right_translate(c, true);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, true);
Down_translate(r, true);
}
else if (left_check1 == 1 && right_check1 == 1) {
c = Right_translate(c, false);
Left_translate(c, false);
Down_translate(r, true);
}
else if (left_check1 == 0 && right_check1 == 0) {
c2 = Right_translate(c, true);
c2 = Left_translate(c, true);
r2 = Down_translate(r, true);
}
break;
}
}
r = r - 0.125;
S_block(add, r, c, ro);
if (check1 == 1 || check2 == 1 || check3 == 1) {
r = 1.8;
}
delay(225);
} while (r > 1.9 );
add_prev = 100;
r_prev = 100;
c_prev = 100;
break;
}
}
lc.line_break(); // Line Break function
called from LED control libraries
lc.gameover(); // Game Over function
called from LED control libraries

You might also like