Compare commits
2 Commits
dbcc7faeba
...
1883032be5
| Author | SHA1 | Date | |
|---|---|---|---|
| 1883032be5 | |||
| af5bc7dcd6 |
@@ -115,4 +115,10 @@ void Ball::modVel(float vx, float vy)
|
||||
this->vy = this->vy + vy;
|
||||
onFloor = false;
|
||||
stopped = false;
|
||||
}
|
||||
|
||||
// Cambia la gravedad
|
||||
void Ball::switchGravity()
|
||||
{
|
||||
g = g == 0.0f ? GRAVITY : 0.0f;
|
||||
}
|
||||
@@ -34,4 +34,7 @@ public:
|
||||
|
||||
// Modifica la velocidad
|
||||
void modVel(float vx, float vy);
|
||||
|
||||
// Cambia la gravedad
|
||||
void switchGravity();
|
||||
};
|
||||
@@ -6,8 +6,8 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
SDL_Event *event;
|
||||
Texture *texture = nullptr;
|
||||
std::vector<Ball *> balls;
|
||||
@@ -47,6 +47,7 @@ void initBalls(int value)
|
||||
Ball *b = new Ball(x, vx, vy, color, texture);
|
||||
balls.push_back(b);
|
||||
}
|
||||
setText();
|
||||
}
|
||||
|
||||
void pushUpBalls()
|
||||
@@ -60,6 +61,18 @@ void pushUpBalls()
|
||||
}
|
||||
}
|
||||
|
||||
void switchBallsGravity()
|
||||
{
|
||||
for (auto ball : balls)
|
||||
{
|
||||
if (ball)
|
||||
{
|
||||
ball->switchGravity();
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void deleteBalls()
|
||||
{
|
||||
for (auto ball : balls)
|
||||
@@ -88,7 +101,7 @@ bool init()
|
||||
{
|
||||
// Create window
|
||||
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
|
||||
if (window == NULL)
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
@@ -97,7 +110,7 @@ bool init()
|
||||
{
|
||||
// Create renderer for window
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (renderer == NULL)
|
||||
if (renderer == nullptr)
|
||||
{
|
||||
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
@@ -117,10 +130,9 @@ bool init()
|
||||
|
||||
texture = new Texture(renderer, "resources/pelota.png");
|
||||
ticks = SDL_GetTicks();
|
||||
srand(time(NULL));
|
||||
srand(time(nullptr));
|
||||
dbg_init(renderer);
|
||||
initBalls(index);
|
||||
setText();
|
||||
|
||||
return success;
|
||||
}
|
||||
@@ -165,60 +177,82 @@ void checkEvents()
|
||||
|
||||
if (event->type == SDL_KEYDOWN && event->key.repeat == 0)
|
||||
{
|
||||
if (event->key.keysym.sym == SDLK_SPACE)
|
||||
switch (event->key.keysym.sym)
|
||||
{
|
||||
pushUpBalls();
|
||||
case SDLK_ESCAPE:
|
||||
{
|
||||
shouldExit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_1)
|
||||
case SDLK_SPACE:
|
||||
{
|
||||
pushUpBalls();
|
||||
break;
|
||||
}
|
||||
|
||||
case SDLK_g:
|
||||
{
|
||||
switchBallsGravity();
|
||||
break;
|
||||
}
|
||||
|
||||
case SDLK_1:
|
||||
{
|
||||
index = 0;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_2)
|
||||
case SDLK_2:
|
||||
{
|
||||
index = 1;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_3)
|
||||
case SDLK_3:
|
||||
{
|
||||
index = 2;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_4)
|
||||
case SDLK_4:
|
||||
{
|
||||
index = 3;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_5)
|
||||
case SDLK_5:
|
||||
{
|
||||
index = 4;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_6)
|
||||
case SDLK_6:
|
||||
{
|
||||
index = 5;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_7)
|
||||
case SDLK_7:
|
||||
{
|
||||
index = 6;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
|
||||
if (event->key.keysym.sym == SDLK_8)
|
||||
case SDLK_8:
|
||||
{
|
||||
index = 7;
|
||||
initBalls(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
setText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user