From e5fb176ef626eabbdc1aa5d65bd12899e4cbfd7b Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 21 Aug 2024 13:12:52 +0200 Subject: [PATCH] =?UTF-8?q?bot=C3=B3=20per=20a=20donar=20energiaa=20les=20?= =?UTF-8?q?pilotes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/ball.cpp | 12 +++++++++++- source/ball.h | 3 +++ source/main.cpp | 26 +++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/source/ball.cpp b/source/ball.cpp index 40cbc45..66372e8 100644 --- a/source/ball.cpp +++ b/source/ball.cpp @@ -67,6 +67,7 @@ void Ball::update() { y = 0; vy = -vy; + vy = -vy * loss; } // Comprueba las colisiones con la parte inferior @@ -89,7 +90,7 @@ void Ball::update() { vx = 0.0f; stopped = true; - //exit(0); + // exit(0); } } @@ -103,4 +104,13 @@ void Ball::render() { sprite->setColor(color.r, color.g, color.b); sprite->render(); +} + +// Modifica la velocidad +void Ball::modVel(float vx, float vy) +{ + this->vx = this->vx + vx; + this->vy = this->vy + vy; + onFloor = false; + stopped = false; } \ No newline at end of file diff --git a/source/ball.h b/source/ball.h index 1b2c8da..a8eefcc 100644 --- a/source/ball.h +++ b/source/ball.h @@ -31,4 +31,7 @@ public: // Pinta la clase void render(); + + // Modifica la velocidad + void modVel(float vx, float vy); }; \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 6daca38..b8ab0df 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -20,8 +20,9 @@ void initBalls(int value) { for (int i = 0; i < test[value]; ++i) { - const int sign = ((rand() % 2) * 2) - 1;; - const float x = (rand()%(SCREEN_WIDTH/2)) + (SCREEN_WIDTH / 4); + const int sign = ((rand() % 2) * 2) - 1; + ; + const float x = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); const float vx = (((rand() % 20) + 10) * 0.1f) * sign; const float vy = ((rand() % 60) - 30) * 0.1f; const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; @@ -30,6 +31,17 @@ void initBalls(int value) } } +void pushUpBalls() +{ + for (auto ball : balls) + { + const int sign = ((rand() % 2) * 2) - 1; + const float vx = (((rand() % 20) + 10) * 0.1f) * sign; + const float vy = ((rand() % 40) * 0.1f) + 5; + ball->modVel(vx, -vy); + } +} + bool init() { // Initialization flag @@ -87,7 +99,7 @@ bool init() texture = new Texture(renderer, "resources/pelota.png"); ticks = SDL_GetTicks(); - srand (time(NULL)); + srand(time(NULL)); initBalls(2); return success; @@ -139,6 +151,14 @@ void checkEvents() shouldExit = true; break; } + + if (event->type == SDL_KEYDOWN) + { + if (event->key.keysym.sym == SDLK_SPACE) + { + pushUpBalls(); + } + } } }