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

@@ -116,3 +116,9 @@ void Ball::modVel(float vx, float vy)
onFloor = 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
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()
{
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;
}
}
}
}