diff --git a/Capter1/ax_game b/Capter1/ax_game index f8ea603..56e1b9c 100755 Binary files a/Capter1/ax_game and b/Capter1/ax_game differ diff --git a/Capter1/ax_game.cpp b/Capter1/ax_game.cpp index 320f398..112882f 100644 --- a/Capter1/ax_game.cpp +++ b/Capter1/ax_game.cpp @@ -1,4 +1,5 @@ #include "ax_game.hpp" +#include ax_game::ax_game() { @@ -7,7 +8,20 @@ ax_game::ax_game() ax_TickCounter = 0; ax_IsRunning = true; - ax_ball_one = ball(); + + srand(time(0)); + + + //ax_ball_one = ball(500, 400, 0, 0); + //ax_ball_two = ball(500, 500, 0, 0); + + for (int i = 0; i < 10; i++) + { + ax_balliste.emplace_back(ball(rand() % 1024, rand() % 768, (rand() % 200) -100 , (rand() % 200) -100)); + } + + + } ax_game::~ax_game() @@ -64,11 +78,11 @@ void ax_game::Input(){ //Tastatur auslesen const Uint8 *state = SDL_GetKeyboardState(NULL); - if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false; - if(state[SDL_SCANCODE_S]) ax_ball_one.vely += 10; - if(state[SDL_SCANCODE_W]) ax_ball_one.vely -= 10; - if(state[SDL_SCANCODE_D]) ax_ball_one.velx += 10; - if(state[SDL_SCANCODE_A]) ax_ball_one.velx -= 10; + //if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false; + //if(state[SDL_SCANCODE_S]) ax_balliste[0].vely += 10; + //if(state[SDL_SCANCODE_W]) ax_balliste[0].vely -= 10; + //if(state[SDL_SCANCODE_D]) ax_balliste[0].velx += 10; + //if(state[SDL_SCANCODE_A]) ax_balliste[0].velx -= 10; } @@ -81,9 +95,21 @@ void ax_game::Update(){ DeltaTime = 0.05f; } ax_TickCounter = SDL_GetTicks(); - + // Update Ball - ax_ball_one.Update(DeltaTime); + for (auto &n : ax_balliste){ + n.Update(DeltaTime, ax_balliste); + } + + // clean up + for (auto it = ax_balliste.begin(); it != ax_balliste.end(); ) + { + if (!it->alive) + it = ax_balliste.erase(it); // erase gibt neuen gültigen Iterator zurück + else + ++it; // nur erhöhen, wenn nichts gelöscht wurde + } + } void ax_game::Output(){ @@ -91,7 +117,12 @@ void ax_game::Output(){ SDL_RenderClear(ax_Renderer); //Puffer leeren - ax_ball_one.Draw(ax_Renderer); + //ax_ball_one.Draw(ax_Renderer); + //ax_ball_two.Draw(ax_Renderer); + + for (auto &n : ax_balliste){ + n.Draw(ax_Renderer); + } SDL_RenderPresent(ax_Renderer); } \ No newline at end of file diff --git a/Capter1/ax_game.hpp b/Capter1/ax_game.hpp index 286584f..b3b8c12 100644 --- a/Capter1/ax_game.hpp +++ b/Capter1/ax_game.hpp @@ -14,7 +14,10 @@ private: Uint32 ax_TickCounter; bool ax_IsRunning; + + ball ax_ball_one; + ball ax_ball_two; public: @@ -23,5 +26,7 @@ public: bool Init(); void RunLoop(); void Shutdown(); + + std::vector ax_balliste; }; diff --git a/Capter1/ball b/Capter1/ball index 91a36a7..3677ebf 100755 Binary files a/Capter1/ball and b/Capter1/ball differ diff --git a/Capter1/ball.cpp b/Capter1/ball.cpp index 50429d7..3c21606 100644 --- a/Capter1/ball.cpp +++ b/Capter1/ball.cpp @@ -1,25 +1,35 @@ #include "ball.hpp" #include "ax_funktions.hpp" - +#include +#include ball::ball() { - std::cout << "Ball erzeugt!"<< std::endl; + +} + + +ball::ball(int x, int y, int vx, int vy) +{ + //std::cout << "Ball erzeugt!"<< std::endl; r_dir = 0; g_dir = -1; b_dir = 1; - velx = 200; - vely = 200; + velx = vx; + vely = vy; - rect = {20,20,20,20}; + alive = true; + + rect = {x,y,20,20}; color = {255,0,0,255}; } ball::~ball() { + std::cout << "Ball gelöscht" << std::endl; } int ball::GetXPos(){ @@ -37,19 +47,24 @@ void ball::Draw(SDL_Renderer* renderer){ SDL_RenderFillRect(renderer, &rect); } -void ball::Update(float f_DeltaTime){ +void ball::Update(float f_DeltaTime, std::vector &list){ + if (this->rect.w <= 1) + { + this->alive = false; + } + color.r += r_dir; color.g += g_dir; color.b += b_dir; //=================ColorCycle====================== // Clamp values between 1 and 254 - color.g = ax::clamp_int(color.g, 1, 254); - color.b = ax::clamp_int(color.b, 1, 254); - color.r = ax::clamp_int(color.r, 1, 254); + //color.g = ax::clamp_int(color.g, 1, 254); + //color.b = ax::clamp_int(color.b, 1, 254); + //color.r = ax::clamp_int(color.r, 1, 254); // Transition logic - if (r_dir != 0 && (color.r == 254 || color.r == 1)) { + /*if (r_dir != 0 && (color.r == 254 || color.r == 1)) { r_dir = 0; g_dir = (color.r == 254) ? 1 : -1; b_dir = 0; @@ -64,14 +79,59 @@ void ball::Update(float f_DeltaTime){ r_dir = 0; g_dir = (color.b == 254) ? -1 : 1; } + */ //=================Movement====================== //Update Position rect.x +=velx * f_DeltaTime; rect.y +=vely * f_DeltaTime; + //Kollisioinstest Fenster 1024x768 - if(rect.x > (1024 -20) || rect.x < 0) velx *= -0.5; - if(rect.y > (768 - 20) || rect.y < 0) vely *= -0.5; + if(rect.x > (1024 -20) || rect.x < 0) + { + velx = (velx * -0.9) + rand() % 10; + this->rect.w -=2; + this->rect.h -=2; + } + if(rect.y > (768 - 20) || rect.y < 0){ + vely = (vely * -0.9) + rand() % 10; + this->rect.w -=2; + this->rect.h -=2; + } //std::cout << velx <<"|"<< vely <GetXPos(); + float dy = i.GetYPos() - this->GetYPos(); + float distance = sqrt(dx*dx + dy*dy); + + if (distance < this->rect.w) + { + this->rect.w /=2; + this->rect.h /=2; + + int tempx = this->velx; + int tempy = this->vely; + + this->velx = i.velx + rand() % 10; + this->vely = i.vely + rand() % 10; + + i.velx = tempx + rand() % 10; + i.vely = tempy + rand() % 10; + + // Split ball + if (list.size() < 1000) + { + list.emplace_back(ball(this->GetXPos() + 21 , this->GetYPos()+1, this->velx, (rand() % 200) -100)); + list.back().rect.w /=2; + list.back().rect.h /=2; + } + } + } + } diff --git a/Capter1/ball.hpp b/Capter1/ball.hpp index 361bc47..3f04672 100644 --- a/Capter1/ball.hpp +++ b/Capter1/ball.hpp @@ -1,5 +1,6 @@ #include "SDL2/SDL.h" #include +#include class ball { @@ -12,13 +13,17 @@ public: SDL_Color color; float velx; float vely; - + bool alive; public: ball(); ~ball(); + + //ball(int x, int y); + ball(int x, int y, int vx, int vy); + int GetXPos(); int GetYPos(); - void Update(float f_DeltaTime); + void Update(float f_DeltaTime, std::vector &list); void Draw(SDL_Renderer* renderer); }; diff --git a/Capter1/main b/Capter1/main index eea118a..e6ee5f5 100755 Binary files a/Capter1/main and b/Capter1/main differ diff --git a/Capter1/main.cpp b/Capter1/main.cpp index 4d8983e..abfcfa9 100644 --- a/Capter1/main.cpp +++ b/Capter1/main.cpp @@ -2,6 +2,7 @@ #include "ax_game.hpp" #include + int main(int argc, char** argv) { /*Game game; @@ -12,10 +13,11 @@ int main(int argc, char** argv) } game.Shutdown(); */ - int i = 0; + int i = 0; ax_game AXGAME; bool success = AXGAME.Init(); + if(success){ AXGAME.RunLoop();