diff --git a/asteroids.cpp b/asteroids.cpp index 132bab4..93cd416 100644 --- a/asteroids.cpp +++ b/asteroids.cpp @@ -1,12 +1,34 @@ #include #include -struct Model { int num_lines; SDL_FPoint lines[10]; SDL_FPoint pos, speed; float angle; }; -struct Bullet { int ttl; SDL_FPoint pos, speed; }; +class Movable { public: SDL_FPoint pos, speed; }; +class Model : public Movable { public: int num_lines, size; SDL_FPoint lines[10]; float angle; }; +class Bullet : public Movable { public: int ttl; }; Model model[20]; Bullet bullet[20]; +Bullet debris[20]; SDL_FPoint wl[10]; +void create_rock(int size, int x = 0, int y = 0) { + int i = 0; while (i<20 && model[i].num_lines > 0) { i++; } + model[i].num_lines = 8; + model[i].size = size; //900 + if (x == 0) { model[i].pos = { 100+rand()%600, 100+rand()%400 }; } else { model[i].pos = { x, y }; } + model[i].angle = rand()%360; + model[i].speed = { (rand()%10) * (4-size) * 0.1f * SDL_cosf(model[i].angle*M_PI/180.0f), (rand()%10) * (3-size) * 0.1f * SDL_sinf(model[i].angle*M_PI/180.0f) }; + float da = 0.0f; + for (int j=0; j 820) mov.pos.x -= 840; + if (mov.pos.y < -20) mov.pos.y += 640; if (mov.pos.y > 620) mov.pos.y -= 640; +} + int main(int argc, char* argv[]) { srand(time(NULL)); SDL_Init(SDL_INIT_EVERYTHING); @@ -14,20 +36,7 @@ int main(int argc, char* argv[]) { SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC); model[0].num_lines = 3; model[0].lines[0] = {15, 0}; model[0].lines[1] = {-10, 10}; model[0].lines[2] = {-10, -10}; model[0].pos = {400, 300}; - - for (int i=1; i<4; i++) { - model[i].num_lines = 8; - model[i].pos = { 100+rand()%600, 100+rand()%400 }; - model[i].angle = rand()%360; - model[i].speed.x = (rand()%10) * 0.1f * SDL_cosf(model[i].angle*M_PI/180.0f); - model[i].speed.y = (rand()%10) * 0.1f * SDL_sinf(model[i].angle*M_PI/180.0f); - float da = 0.0f; - for (int j=0; j 0) i++; - bullet[i].pos.x = model[0].pos.x; - bullet[i].pos.y = model[0].pos.y; - bullet[i].speed.x = 8.0f * SDL_cosf(model[0].angle*M_PI/180.0f); - bullet[i].speed.y = -8.0f * SDL_sinf(model[0].angle*M_PI/180.0f); + bullet[i].pos = model[0].pos; + bullet[i].speed = { 8.0f * SDL_cosf(model[0].angle*M_PI/180.0f), -8.0f * SDL_sinf(model[0].angle*M_PI/180.0f) }; bullet[i].ttl = 100; } } @@ -51,28 +58,31 @@ int main(int argc, char* argv[]) { if (keys[SDL_SCANCODE_LEFT]) model[0].angle+=10; if (keys[SDL_SCANCODE_UP]) { model[0].speed.x += 0.1f * SDL_cosf(model[0].angle*M_PI/180.0f); model[0].speed.y -= 0.1f * SDL_sinf(model[0].angle*M_PI/180.0f); } - for(int i=0; i<20; i++) if (model[i].num_lines > 0) { - model[i].pos.x += model[i].speed.x; - model[i].pos.y += model[i].speed.y; - if (model[i].pos.x < -20) model[i].pos.x += 840; - if (model[i].pos.x > 820) model[i].pos.x -= 840; - if (model[i].pos.y < -20) model[i].pos.y += 640; - if (model[i].pos.y > 620) model[i].pos.y -= 640; - if (i > 0) model[i].angle++; - } - for (int i=0; i<20; i++) if (bullet[i].ttl > 0) { - bullet[i].pos.x += bullet[i].speed.x; - bullet[i].pos.y += bullet[i].speed.y; - if (bullet[i].pos.x < -20) bullet[i].pos.x += 840; - if (bullet[i].pos.x > 820) bullet[i].pos.x -= 840; - if (bullet[i].pos.y < -20) bullet[i].pos.y += 640; - if (bullet[i].pos.y > 620) bullet[i].pos.y -= 640; - bullet[i].ttl--; + for (int i=0; i<20; i++) { + if (model[i].num_lines > 0) { move(model[i]); if (i > 0) model[i].angle++; } + if (debris[i].ttl > 0) { move(debris[i]); debris[i].ttl--; } + if (bullet[i].ttl > 0) { + move(bullet[i]); + bullet[i].ttl--; + for(int j=1; j<20; j++) if (model[j].num_lines > 0) { + if ( (bullet[i].pos.x-model[j].pos.x)*(bullet[i].pos.x-model[j].pos.x) + (bullet[i].pos.y-model[j].pos.y)*(bullet[i].pos.y-model[j].pos.y) < (100*model[j].size*model[j].size) ) { + bullet[i].ttl = 0; + if (model[j].size-1 > 0) { create_rock(model[j].size-1, model[j].pos.x, model[j].pos.y); create_rock(model[j].size-1, model[j].pos.x, model[j].pos.y); } + model[j].num_lines = 0; + for (int k=0; k<8; k++) { + int d = 0; while (d<20 && debris[d].ttl > 0) { d++; } + debris[d].pos = model[j].pos; + int angle = rand()%360; debris[d].speed = { 1.0f * SDL_cosf(angle*M_PI/180.0f), -1.0f * SDL_sinf(angle*M_PI/180.0f) }; + debris[d].ttl = 30; + } + break; + } + } + } } SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255); SDL_RenderClear(sdlRenderer); SDL_SetRenderDrawColor(sdlRenderer, 255, 255, 255, 255); - for (int i=0; i<20; i++) if (model[i].num_lines > 0) { for (int j=0; j 0) { - SDL_Rect rect { bullet[i].pos.x, bullet[i].pos.y, 2, 2}; - SDL_RenderDrawRect(sdlRenderer, &rect); + if (bullet[i].ttl > 0) { SDL_Rect rect { bullet[i].pos.x, bullet[i].pos.y, 2, 2}; SDL_RenderDrawRect(sdlRenderer, &rect); } + if (debris[i].ttl > 0) { SDL_Rect rect { debris[i].pos.x, debris[i].pos.y, 2, 2}; SDL_RenderDrawRect(sdlRenderer, &rect); } } SDL_RenderPresent(sdlRenderer); }