This commit is contained in:
2025-10-22 17:44:42 +02:00
parent 0e29919fd2
commit 0335a726c9
5 changed files with 53 additions and 1 deletions

View File

@@ -1,15 +1,28 @@
#include "ball.hpp"
#include <algorithm>
#include <ranges>
#include <iostream>
inline int clamp_int(int v, int lo, int hi) {
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
ball::ball()
{
std::cout << "Ball erzeugt!"<< std::endl;
r_dir = 0;
g_dir = -1;
b_dir = 1;
x = 50;
y = 50;
rect = {20,20,20,20};
color = {0,255,0,255};
color = {255,0,0,255};
}
ball::~ball()
@@ -28,3 +41,37 @@ void ball::Draw(SDL_Renderer* renderer){
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderFillRect(renderer, &rect);
}
void ball::Update(){
color.r += r_dir;
color.g += g_dir;
color.b += b_dir;
//=================ColorCycle======================
// Clamp values between 1 and 254
color.g = clamp_int(color.g, 1, 254);
color.b = clamp_int(color.b, 1, 254);
color.r = clamp_int(color.r, 1, 254);
// Transition logic
if (r_dir != 0 && (color.r == 254 || color.r == 1)) {
r_dir = 0;
g_dir = (color.r == 254) ? 1 : -1;
b_dir = 0;
}
else if (g_dir != 0 && (color.g == 254 || color.g == 1)) {
g_dir = 0;
b_dir = (color.g == 254) ? 1 : -1;
r_dir = (color.g == 254) ? -1 : 1;
}
else if (b_dir != 0 && (color.b == 254 || color.b == 1)) {
b_dir = 0;
r_dir = 0;
g_dir = (color.b == 254) ? -1 : 1;
}
//=================Movement======================
x+=10.0;
y+=10.0;
}