botó per a donar energiaa les pilotes

This commit is contained in:
2024-08-21 13:12:52 +02:00
parent cdd6e7bc5d
commit e5fb176ef6
3 changed files with 37 additions and 4 deletions

View File

@@ -67,6 +67,7 @@ void Ball::update()
{
y = 0;
vy = -vy;
vy = -vy * loss;
}
// Comprueba las colisiones con la parte inferior
@@ -104,3 +105,12 @@ 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;
}

View File

@@ -31,4 +31,7 @@ public:
// Pinta la clase
void render();
// Modifica la velocidad
void modVel(float vx, float vy);
};

View File

@@ -20,7 +20,8 @@ void initBalls(int value)
{
for (int i = 0; i < test[value]; ++i)
{
const int sign = ((rand() % 2) * 2) - 1;;
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;
@@ -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
@@ -139,6 +151,14 @@ void checkEvents()
shouldExit = true;
break;
}
if (event->type == SDL_KEYDOWN)
{
if (event->key.keysym.sym == SDLK_SPACE)
{
pushUpBalls();
}
}
}
}