TETRIS: Add restart option to pause menu

This commit is contained in:
zvon 2020-08-24 18:21:35 +02:00
parent 93d47d9883
commit c853e925bd

View File

@ -27,7 +27,7 @@
bool pause = false; bool pause = false;
int pause_select = 0; int pause_select = 0;
int pause_max = 1; int pause_max = 2;
int ticks_till_fall = TICKS_TILL_FALL; int ticks_till_fall = TICKS_TILL_FALL;
int ticks_till_descend = TICKS_TILL_DESCEND; int ticks_till_descend = TICKS_TILL_DESCEND;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options; std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
@ -397,29 +397,34 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )(
void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) { void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r, auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#101090FF", true ); "#101090FF", true );
bg->setPermanent( true ); bg->setPermanent();
scene.addObject( bg ); scene.addObject( bg );
auto left_barrier = std::make_shared< SDLPP::RectangleRender >( auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true ); LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true );
left_barrier->centerX(); left_barrier->centerX();
left_barrier->setStatic();
scene.addObject( left_barrier ); scene.addObject( left_barrier );
auto right_barrier = std::make_shared< SDLPP::RectangleRender >( auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true ); RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true );
right_barrier->centerX(); right_barrier->centerX();
right_barrier->setStatic();
scene.addObject( right_barrier ); scene.addObject( right_barrier );
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >( auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 0.02, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER + 0.04, LEFT_BORDER - 0.02, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER + 0.04,
0.02, r, "#FF000080", true ); 0.02, r, "#FF000080", true );
bottom_barrier->centerX(); bottom_barrier->centerX();
bottom_barrier->setStatic();
scene.addObject( bottom_barrier ); scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >( auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 ); 0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 );
tetris->centerX(); tetris->centerX();
tetris->setStatic();
scene.addObject( tetris ); scene.addObject( tetris );
auto next = std::make_shared< SDLPP::TextRenderer >( auto next = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF", RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF",
"000000", 5, SDLPP_TEXT_CENTER ); "000000", 5, SDLPP_TEXT_CENTER );
next->centerX(); next->centerX();
next->setStatic();
scene.addObject( next ); scene.addObject( next );
double posy = 1; double posy = 1;
auto gameover = std::make_shared< SDLPP::RectangleRender >( auto gameover = std::make_shared< SDLPP::RectangleRender >(
@ -429,17 +434,20 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
gameover->addCollision( gameover_collision ); gameover->addCollision( gameover_collision );
gameover->setId( GAME_OVER ); gameover->setId( GAME_OVER );
gameover->setColiderColor( "FF0000" ); gameover->setColiderColor( "FF0000" );
gameover->setStatic();
scene.addObject( gameover ); scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >( auto score_text = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF",
"#000000", 5, SDLPP_TEXT_CENTER ); "#000000", 5, SDLPP_TEXT_CENTER );
score_text->centerX(); score_text->centerX();
score_text->setStatic();
scene.addObject( score_text ); scene.addObject( score_text );
score_texture = std::make_shared< SDLPP::TextRenderer >( score_texture = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, std::to_string( score ), RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, std::to_string( score ),
"FFFFFF", "000000", 5, SDLPP_TEXT_TOP ); "FFFFFF", "000000", 5, SDLPP_TEXT_TOP );
score_texture->centerX(); score_texture->centerX();
score_texture->setStatic();
scene.addObject( score_texture ); scene.addObject( score_texture );
for ( int i = 0; i < 20; i++ ) { for ( int i = 0; i < 20; i++ ) {
posy -= BLOCK_SIZE; posy -= BLOCK_SIZE;
@ -449,6 +457,7 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
colider_colider.setInfinite(); colider_colider.setInfinite();
colider->addCollision( colider_colider ); colider->addCollision( colider_colider );
colider->setId( COLIDER_ID ); colider->setId( COLIDER_ID );
colider->setStatic();
line_coliders.push_back( colider ); line_coliders.push_back( colider );
scene.addObject( colider ); scene.addObject( colider );
} }
@ -471,14 +480,20 @@ void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
y->centerX(); y->centerX();
scene.addObject( y ); scene.addObject( y );
auto resume = auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.51, 0.2, 0.08, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 ); resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" ); resume->setColor( "#FFFFFF40" );
resume->centerX(); resume->centerX();
scene.addObject( resume ); scene.addObject( resume );
pause_options.push_back( resume ); pause_options.push_back( resume );
auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.61, 0.2, 0.08, r );
restart->setText( *font, "Restart", "#FFFFFF", "#000000", 5 );
restart->centerX();
scene.addObject( restart );
pause_options.push_back( restart );
auto quit = auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.71, 0.2, 0.08, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 ); quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX(); quit->centerX();
scene.addObject( quit ); scene.addObject( quit );
@ -673,6 +688,22 @@ void handleKeyDownPause( SDL_Keycode key ) {
inputThread.detach(); inputThread.detach();
} break; } break;
case 1: case 1:
{
pause = false;
cur_object.reset();
checked_line = true;
next_object.reset();
active_scene->resetScene();
active_scene->setPrevTicks( SDL_GetTicks() );
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
active_scene->getRendererShared(), active_scene );
next_object->setPos( 0.9, 0.5 );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
}
break;
case 2:
quitGame(); quitGame();
default: default:
break; break;
@ -830,6 +861,7 @@ int main() {
active_scene = main_scene; active_scene = main_scene;
font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 ); font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
addStuff( *main_scene, renderer ); addStuff( *main_scene, renderer );
main_scene->saveScene();
pause_scene = std::make_shared< SDLPP::Scene >( renderer ); pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause( *pause_scene, renderer ); addPause( *pause_scene, renderer );
@ -848,6 +880,7 @@ int main() {
next_object->setPos( 0.9, 0.5 ); next_object->setPos( 0.9, 0.5 );
while ( !quit ) { while ( !quit ) {
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
std::cout << "cur_object: " << (cur_object ? "TRUE" : "FALSE") << ", checked_line: " << (checked_line ? "TRUE" : "FALSE") << std::endl;
if ( !cur_object && checked_line ) { if ( !cur_object && checked_line ) {
std::lock_guard< std::mutex > guard( movement_mutex ); std::lock_guard< std::mutex > guard( movement_mutex );
cur_object = next_object; cur_object = next_object;