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;
|
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;
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
};
|
};
|
||||||
@@ -6,8 +6,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
SDL_Window *window = NULL;
|
SDL_Window *window = nullptr;
|
||||||
SDL_Renderer *renderer = NULL;
|
SDL_Renderer *renderer = nullptr;
|
||||||
SDL_Event *event;
|
SDL_Event *event;
|
||||||
Texture *texture = nullptr;
|
Texture *texture = nullptr;
|
||||||
std::vector<Ball *> balls;
|
std::vector<Ball *> balls;
|
||||||
@@ -47,6 +47,7 @@ void initBalls(int value)
|
|||||||
Ball *b = new Ball(x, vx, vy, color, texture);
|
Ball *b = new Ball(x, vx, vy, color, texture);
|
||||||
balls.push_back(b);
|
balls.push_back(b);
|
||||||
}
|
}
|
||||||
|
setText();
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushUpBalls()
|
void pushUpBalls()
|
||||||
@@ -60,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)
|
||||||
@@ -88,7 +101,7 @@ bool init()
|
|||||||
{
|
{
|
||||||
// Create window
|
// Create window
|
||||||
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN);
|
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());
|
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||||
success = false;
|
success = false;
|
||||||
@@ -97,7 +110,7 @@ bool init()
|
|||||||
{
|
{
|
||||||
// Create renderer for window
|
// Create renderer for window
|
||||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
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());
|
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
||||||
success = false;
|
success = false;
|
||||||
@@ -117,10 +130,9 @@ bool init()
|
|||||||
|
|
||||||
texture = new Texture(renderer, "resources/pelota.png");
|
texture = new Texture(renderer, "resources/pelota.png");
|
||||||
ticks = SDL_GetTicks();
|
ticks = SDL_GetTicks();
|
||||||
srand(time(NULL));
|
srand(time(nullptr));
|
||||||
dbg_init(renderer);
|
dbg_init(renderer);
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
setText();
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@@ -165,60 +177,82 @@ 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_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;
|
index = 0;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_2)
|
case SDLK_2:
|
||||||
{
|
{
|
||||||
index = 1;
|
index = 1;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_3)
|
case SDLK_3:
|
||||||
{
|
{
|
||||||
index = 2;
|
index = 2;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_4)
|
case SDLK_4:
|
||||||
{
|
{
|
||||||
index = 3;
|
index = 3;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_5)
|
case SDLK_5:
|
||||||
{
|
{
|
||||||
index = 4;
|
index = 4;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_6)
|
case SDLK_6:
|
||||||
{
|
{
|
||||||
index = 5;
|
index = 5;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_7)
|
case SDLK_7:
|
||||||
{
|
{
|
||||||
index = 6;
|
index = 6;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event->key.keysym.sym == SDLK_8)
|
case SDLK_8:
|
||||||
{
|
{
|
||||||
index = 7;
|
index = 7;
|
||||||
initBalls(index);
|
initBalls(index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setText();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user