Muchos pequeños cambios para estandarizar y mejorar la demo

This commit is contained in:
2024-08-22 21:35:48 +02:00
parent 22ddfcd153
commit 1eab622ba2
7 changed files with 49 additions and 13 deletions

View File

@@ -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

View File

@@ -1,10 +1,19 @@
#pragma once
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#include "SDL2/SDL.h"
#include <stdlib.h> /* 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;
};

View File

@@ -1,4 +1,3 @@
#include <SDL2/SDL.h>
#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)

View File

@@ -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;
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include <SDL2/SDL.h>
#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);
};