cambia el numero de pelotas con las teclas de los numeros

This commit is contained in:
2024-08-21 14:07:20 +02:00
parent 0019625770
commit 47c0aec24a

View File

@@ -11,13 +11,16 @@ SDL_Renderer *renderer = NULL;
SDL_Event *event;
Texture *texture = nullptr;
std::vector<Ball *> balls;
int test[5] = {1, 10, 100, 500, 1000};
int test[6] = {1, 10, 100, 500, 1000, 10000};
bool shouldExit = false;
Uint32 ticks = 0;
void deleteBalls();
void initBalls(int value)
{
deleteBalls();
for (int i = 0; i < test[value]; ++i)
{
const int sign = ((rand() % 2) * 2) - 1;
@@ -42,6 +45,19 @@ void pushUpBalls()
}
}
void deleteBalls()
{
for (auto ball : balls)
{
if (ball)
{
delete ball;
ball = nullptr;
}
}
balls.clear();
}
bool init()
{
// Initialization flag
@@ -62,7 +78,7 @@ bool init()
}
// Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
window = SDL_CreateWindow("Demo Pelotas 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
@@ -125,15 +141,7 @@ void close()
texture = nullptr;
}
for (auto ball : balls)
{
if (ball)
{
delete ball;
ball = nullptr;
}
}
balls.clear();
deleteBalls();
// Quit SDL subsystems
IMG_Quit();
@@ -158,6 +166,36 @@ void checkEvents()
{
pushUpBalls();
}
if (event->key.keysym.sym == SDLK_1)
{
initBalls(0);
}
if (event->key.keysym.sym == SDLK_2)
{
initBalls(1);
}
if (event->key.keysym.sym == SDLK_3)
{
initBalls(2);
}
if (event->key.keysym.sym == SDLK_4)
{
initBalls(3);
}
if (event->key.keysym.sym == SDLK_5)
{
initBalls(4);
}
if (event->key.keysym.sym == SDLK_6)
{
initBalls(5);
}
}
}
}