Starting with Asteroids
This commit is contained in:
92
asteroids.cpp
Normal file
92
asteroids.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
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<model[i].num_lines; j++) {
|
||||||
|
model[i].lines[j].x = (30+rand()%15) * SDL_cosf(da*M_PI/180);
|
||||||
|
model[i].lines[j].y = (30+rand()%15) * -SDL_sinf(da*M_PI/180);
|
||||||
|
da += (360/model[i].num_lines);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Event sdlEvent;
|
||||||
|
bool should_exit = false;
|
||||||
|
while(!should_exit) {
|
||||||
|
while(SDL_PollEvent(&sdlEvent)) {
|
||||||
|
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
|
||||||
|
if (sdlEvent.type == SDL_KEYDOWN) {
|
||||||
|
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_SPACE) {
|
||||||
|
int i = 0;
|
||||||
|
while (i<20 && bullet[i].ttl > 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<model[i].num_lines; j++) {
|
||||||
|
wl[j].x = model[i].lines[j].x * SDL_cosf(model[i].angle*M_PI/180.0f) + model[i].lines[j].y * SDL_sinf(model[i].angle*M_PI/180.0f) + model[i].pos.x;
|
||||||
|
wl[j].y = -model[i].lines[j].x * SDL_sinf(model[i].angle*M_PI/180.0f) + model[i].lines[j].y * SDL_cosf(model[i].angle*M_PI/180.0f) + model[i].pos.y;
|
||||||
|
}
|
||||||
|
wl[model[i].num_lines] = wl[0];
|
||||||
|
SDL_RenderDrawLinesF(sdlRenderer, wl, model[i].num_lines+1);
|
||||||
|
}
|
||||||
|
for (int i=0; i<20; i++) if (bullet[i].ttl > 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user