diff --git a/resources/soccer_ball.aseprite b/resources/soccer_ball.aseprite new file mode 100644 index 0000000..3a418fd Binary files /dev/null and b/resources/soccer_ball.aseprite differ diff --git a/resources/soccer_ball.png b/resources/soccer_ball.png new file mode 100644 index 0000000..e04c69e Binary files /dev/null and b/resources/soccer_ball.png differ diff --git a/source/ball.cpp b/source/ball.cpp index 2bed89e..75c2a28 100644 --- a/source/ball.cpp +++ b/source/ball.cpp @@ -1,5 +1,4 @@ #include "ball.h" -#include "defines.h" // Constructor Ball::Ball(int x, int y, int w, int h, int vx, int vy, Texture *texture) @@ -13,8 +12,8 @@ Ball::Ball(int x, int y, int w, int h, int vx, int vy, Texture *texture) sprite = new Sprite(texture); sprite->setPos({x, y}); sprite->setSize(w, h); - sprite->setClip({0, 0, 24, 24}); - color = {255, 255, 255}; + sprite->setClip({0, 0, w, h}); + color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; } // Destructor @@ -69,7 +68,7 @@ void Ball::update() if (collision) { - color = {rand() % 255, rand() % 255, rand() % 255}; + sprite->setAnimationSpeed((rand() % 15) + 5); } // Actualiza la posición del sprite diff --git a/source/defines.h b/source/defines.h index e6a118d..666a295 100644 --- a/source/defines.h +++ b/source/defines.h @@ -1,10 +1,19 @@ #pragma once -#define SCREEN_WIDTH 320 -#define SCREEN_HEIGHT 240 +#include "SDL2/SDL.h" +#include /* srand, rand */ + +#define SCREEN_WIDTH 480 +#define SCREEN_HEIGHT 360 #define NUM_BALLS 100 +#define BALL_SIZE 36 +#define NUM_FRAMES 4 +#define BG_R 96 +#define BG_G 96 +#define BG_B 96 +#define TEXTURE_FILE "resources/soccer_ball.png" struct color_t { - int r,g,b; + int r, g, b; }; \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index c5c9798..910e1bc 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,3 @@ -#include #include "texture.h" #include "ball.h" #include "defines.h" @@ -54,13 +53,19 @@ bool init() event = new SDL_Event(); - texture = new Texture(renderer, "resources/pelota.png"); + texture = new Texture(renderer, TEXTURE_FILE); for (int i = 0; i < NUM_BALLS; ++i) { - ball[i] = new Ball(rand() % SCREEN_WIDTH, rand() % SCREEN_HEIGHT, 24, 24, 1, 1, texture); + const int vx = rand() % 2 == 0 ? 1 : -1; + const int vy = rand() % 2 == 0 ? 1 : -1; + const int x = rand() % SCREEN_WIDTH; + const int y = rand() % SCREEN_HEIGHT; + const int size = BALL_SIZE; + ball[i] = new Ball(x, y, size, size, vx, vy, texture); } ticks = SDL_GetTicks(); + srand(time(NULL)); return success; } @@ -106,6 +111,19 @@ void checkEvents() shouldExit = true; break; } + + if (event->type == SDL_KEYDOWN && event->key.repeat == 0) + { + switch (event->key.keysym.sym) + { + case SDLK_ESCAPE: + shouldExit = true; + break; + + default: + break; + } + } } } @@ -124,7 +142,7 @@ void update() void render() { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255); SDL_RenderClear(renderer); for (int i = 0; i < NUM_BALLS; ++i) diff --git a/source/sprite.cpp b/source/sprite.cpp index d1d4aa5..5d00818 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -7,7 +7,7 @@ Sprite::Sprite(Texture *texture) pos = {0, 0, 0, 0}; clip = {0, 0, 0, 0}; frame = 0; - numFrames = 3; + numFrames = NUM_FRAMES; animationSpeed = 20; animationCounter = 0; } @@ -64,4 +64,11 @@ void Sprite::animate() void Sprite::setColor(int r, int g, int b) { texture->setColor(r, g, b); +} + +// Cambia la velocidad de la animación +void Sprite::setAnimationSpeed(int value) +{ + animationSpeed = value; + animationCounter = 0; } \ No newline at end of file diff --git a/source/sprite.h b/source/sprite.h index 761dc45..d57dfd6 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -1,7 +1,7 @@ #pragma once -#include #include "texture.h" +#include "defines.h" class Sprite { @@ -41,4 +41,7 @@ public: // Modulación de color void setColor(int r, int g, int b); + + // Cambia la velocidad de la animación + void setAnimationSpeed(int value); }; \ No newline at end of file