From 1883032be5ec0765125202051e751a1a00a48a07 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 22 Aug 2024 20:26:06 +0200 Subject: [PATCH] =?UTF-8?q?Bot=C3=B3n=20para=20anular/activar=20la=20grave?= =?UTF-8?q?dad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/ball.cpp | 6 ++++++ source/ball.h | 3 +++ source/main.cpp | 51 ++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/source/ball.cpp b/source/ball.cpp index 61d9cdc..e731b23 100644 --- a/source/ball.cpp +++ b/source/ball.cpp @@ -115,4 +115,10 @@ void Ball::modVel(float vx, float vy) this->vy = this->vy + vy; onFloor = false; stopped = false; +} + +// Cambia la gravedad +void Ball::switchGravity() +{ + g = g == 0.0f ? GRAVITY : 0.0f; } \ No newline at end of file diff --git a/source/ball.h b/source/ball.h index a8eefcc..49c3464 100644 --- a/source/ball.h +++ b/source/ball.h @@ -34,4 +34,7 @@ public: // Modifica la velocidad void modVel(float vx, float vy); + + // Cambia la gravedad + void switchGravity(); }; \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index b69b752..20a19bb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -61,6 +61,18 @@ void pushUpBalls() } } +void switchBallsGravity() +{ + for (auto ball : balls) + { + if (ball) + { + ball->switchGravity(); + ; + } + } +} + void deleteBalls() { for (auto ball : balls) @@ -165,64 +177,81 @@ void checkEvents() if (event->type == SDL_KEYDOWN && event->key.repeat == 0) { - if (event->key.keysym.sym == SDLK_ESCAPE) + switch (event->key.keysym.sym) + { + case SDLK_ESCAPE: { shouldExit = true; break; } - - else if (event->key.keysym.sym == SDLK_SPACE) + + case SDLK_SPACE: { pushUpBalls(); break; } - else if (event->key.keysym.sym == SDLK_1) + case SDLK_g: + { + switchBallsGravity(); + break; + } + + case SDLK_1: { index = 0; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_2) + case SDLK_2: { index = 1; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_3) + case SDLK_3: { index = 2; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_4) + case SDLK_4: { index = 3; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_5) + case SDLK_5: { index = 4; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_6) + case SDLK_6: { index = 5; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_7) + case SDLK_7: { index = 6; initBalls(index); + break; } - else if (event->key.keysym.sym == SDLK_8) + case SDLK_8: { index = 7; initBalls(index); + break; + } } } }