diff --git a/asteroids.cpp b/asteroids.cpp new file mode 100644 index 0000000..132bab4 --- /dev/null +++ b/asteroids.cpp @@ -0,0 +1,92 @@ +#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; }; +Model model[20]; +Bullet bullet[20]; +SDL_FPoint wl[10]; + +int main(int argc, char* argv[]) { + srand(time(NULL)); + SDL_Init(SDL_INIT_EVERYTHING); + SDL_Window* sdlWindow = SDL_CreateWindow("ASTEROIDS", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN); + 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].ttl = 100; + } + } + } + const Uint8* keys = SDL_GetKeyboardState(NULL); + if (keys[SDL_SCANCODE_RIGHT]) model[0].angle-=10; + 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--; + } + 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); + } + SDL_RenderPresent(sdlRenderer); + } + SDL_Quit(); + return 0; +} \ No newline at end of file