Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
tetris/main.cpp
- Committer:
- jsanchez307
- Date:
- 2022-12-06
- Revision:
- 22:601e6f9077e4
File content as of revision 22:601e6f9077e4:
#include "mbed.h"
#include "uLCD_4DGL.h"
#include "Game.h"
#include "Nav_Switch.h"
uLCD_4DGL uLCD(p13, p14, p30);
Nav_Switch myNav(p19, p16, p17, p15, p18); // pins(up, down, left, right, fire)
int SinglePiecePoints = 50;
int LineClearPoints = 200;
int key_input = 0;
bool gameStarted = false;
void input_left() // FIX
{
if(!gameStarted)
{ gameStarted=true;
return;
}
key_input = 1;
}
void input_right()
{
if(!gameStarted)
{ gameStarted=true;
return;
}
key_input = 2;
}
void input_rotate()
{
if(!gameStarted)
{ gameStarted=true;
return;
}
key_input = 3;
}
void input_down()
{
if(!gameStarted)
{ gameStarted=true;
return;
}
key_input=4;
}
void clear_board()
{
uLCD.filled_rectangle(20,0,79,128,0);
}
void clear_next_piece()
{
uLCD.filled_rectangle(92,20,122,50,0);
}
void UpdateGameStatus(int points) // used to include int lines
{
uLCD.locate(13,12);
uLCD.printf("Score");
uLCD.locate(13,13);
uLCD.printf("%d",points);
}
/*int RandomGen(char range)
{
pc.printf("%c",range);
while(!pc.readable()) wait(0.5);
char buffer[4];
pc.gets(buffer,4);
int i = buffer[0]-'0';
return i;
}*/
int main()
{
/*while(1){
uLCD.printf("test");
}*/
/*if (myNav.up()) input_rotate();
if (myNav.down()) input_down();;
if (myNav.left()) input_left();
if (myNav.right()) input_right();;
//check mouse left button click*/ // FIXQ
//if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
uLCD.text_width(2);
uLCD.text_height(2);
uLCD.color(WHITE);
uLCD.printf("TETRIS");
wait(2);
uLCD.baudrate(3000000);
/*pc.baud(9600);
pc.format(8,SerialBase::None,1);
pc.printf("0");
while(!pc.readable()) wait(0.5);
char buffer[4];
pc.gets(buffer,4);*/
bool isGameOver = false;
int mScreenHeight = 128;
Pieces mPieces;
Board mBoard (&mPieces, mScreenHeight);
int a = 1; // RandomGen('a')
int b = 2;
int c = 3;
int d = 4;
Game mGame (&mBoard, &mPieces, mScreenHeight, &uLCD,a,b,c,d);
// ----- Main Loop -----
int prevX=0;
int prevY=0;
int prevPiece=-1;
int prevRot=0;
Timer timer;
timer.start();
key_input=0;
bool needErase = false;
uLCD.cls();
int piece = 0;
int rotate = 0;
UpdateGameStatus(mGame.GetPoints());
while (1)
{
if (myNav.up()) input_rotate();
if (myNav.down()) input_down();;
if (myNav.left()) input_left();
if (myNav.right()) input_right();;
//check mouse left button click // FIXQ
//if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
if(isGameOver)
{
wait(1);
uLCD.cls();
uLCD.text_width(2);
uLCD.text_height(2);
uLCD.printf("GAME OVER");
while(1);
}
// ----- Draw ----
if(needErase)
{
mGame.ErasePiece(prevX,prevY,prevPiece,prevRot);
needErase=false;
}
mGame.DrawScene();
prevX=mGame.mPosX;
prevY=mGame.mPosY;
prevPiece=mGame.mPiece;
prevRot=mGame.mRotation;
// ----- Input -----
switch (key_input)
{
case (2): //right
{
if (mBoard.IsPossibleMovement (mGame.mPosX + 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
{mGame.mPosX++;needErase=true;}
break;
}
case (1): //left
{
if (mBoard.IsPossibleMovement (mGame.mPosX - 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
{mGame.mPosX--;needErase=true;}
break;
}
case (4)://down
{
// Check collision from up to down
while (mBoard.IsPossibleMovement(mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation)) { mGame.mPosY++; }
needErase=true;
mBoard.StorePiece (mGame.mPosX, mGame.mPosY - 1, mGame.mPiece, mGame.mRotation);
mGame.AddPoints(SinglePiecePoints);
int linesDeleted = mBoard.DeletePossibleLines ();
if(linesDeleted>0)
{
mGame.AddClearedLines(linesDeleted);
mGame.AddPoints(LineClearPoints*linesDeleted);
//Thread t1(FlashLight);
//PlayClearSound();
clear_board();
}
UpdateGameStatus(mGame.GetPoints()); // FIX
if (mBoard.IsGameOver())
{
isGameOver=true;
uLCD.cls();
}
if(!isGameOver)
{
mGame.CreateNewPiece(piece,rotate);
clear_next_piece();
piece++;
rotate++;
if (piece==7) piece = 0;
if (rotate==4) rotate = 0;
}
break;
}
case (3)://rotate
{
if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY, mGame.mPiece, (mGame.mRotation + 1) % 4))
{mGame.mRotation = (mGame.mRotation + 1) % 4;needErase=true;}
break;
}
case (0):{break;}
}
key_input = 0;
// ----- Vertical movement -----
if(timer.read_ms()>WAIT_TIME)
{
needErase=true;
if(!isGameOver)
{
if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY + 1, mGame.mPiece, mGame.mRotation))
{
mGame.mPosY++;
}
else
{
mBoard.StorePiece (mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation);
mGame.AddPoints(SinglePiecePoints);
int linesDeleted = mBoard.DeletePossibleLines ();
if(linesDeleted>0)
{
mGame.AddClearedLines(linesDeleted);
mGame.AddPoints(LineClearPoints*linesDeleted);
//Thread t1(FlashLight);
//PlayClearSound();
clear_board();
}
UpdateGameStatus(mGame.GetPoints());
if (mBoard.IsGameOver())
{
isGameOver=true;
uLCD.cls();
}
if(!isGameOver)
{
mGame.CreateNewPiece(piece,rotate);
clear_next_piece(); // 0-6 Pieces, 0-3 rotations^^
piece++;
rotate++;
if (piece==7) piece = 0;
if (rotate==4) rotate = 0;
}
}
}
timer.reset();
}
wait(0.1);
}
}