Botón para anular/activar la gravedad

This commit is contained in:
2024-08-22 20:26:06 +02:00
parent af5bc7dcd6
commit 1883032be5
3 changed files with 49 additions and 11 deletions

View File

@@ -115,4 +115,10 @@ void Ball::modVel(float vx, float vy)
this->vy = this->vy + vy; this->vy = this->vy + vy;
onFloor = false; onFloor = false;
stopped = false; stopped = false;
}
// Cambia la gravedad
void Ball::switchGravity()
{
g = g == 0.0f ? GRAVITY : 0.0f;
} }

View File

@@ -34,4 +34,7 @@ public:
// Modifica la velocidad // Modifica la velocidad
void modVel(float vx, float vy); void modVel(float vx, float vy);
// Cambia la gravedad
void switchGravity();
}; };

View File

@@ -61,6 +61,18 @@ void pushUpBalls()
} }
} }
void switchBallsGravity()
{
for (auto ball : balls)
{
if (ball)
{
ball->switchGravity();
;
}
}
}
void deleteBalls() void deleteBalls()
{ {
for (auto ball : balls) for (auto ball : balls)
@@ -165,64 +177,81 @@ void checkEvents()
if (event->type == SDL_KEYDOWN && event->key.repeat == 0) 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; shouldExit = true;
break; break;
} }
else if (event->key.keysym.sym == SDLK_SPACE) case SDLK_SPACE:
{ {
pushUpBalls(); pushUpBalls();
break; break;
} }
else if (event->key.keysym.sym == SDLK_1) case SDLK_g:
{
switchBallsGravity();
break;
}
case SDLK_1:
{ {
index = 0; index = 0;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_2) case SDLK_2:
{ {
index = 1; index = 1;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_3) case SDLK_3:
{ {
index = 2; index = 2;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_4) case SDLK_4:
{ {
index = 3; index = 3;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_5) case SDLK_5:
{ {
index = 4; index = 4;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_6) case SDLK_6:
{ {
index = 5; index = 5;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_7) case SDLK_7:
{ {
index = 6; index = 6;
initBalls(index); initBalls(index);
break;
} }
else if (event->key.keysym.sym == SDLK_8) case SDLK_8:
{ {
index = 7; index = 7;
initBalls(index); initBalls(index);
break;
}
} }
} }
} }