/******************************************************************************************* * * * ********************************************************************************************/ #include "raylib.h" #include #define MAXASTEROIDS 3 #define MAXSHIPS 3 // Variables for storing and restoring the current game to and from memory bool savedactive[MAXASTEROIDS]; Vector2 savedposition[MAXASTEROIDS]; float savedangle[MAXASTEROIDS]; float savedthrust[MAXASTEROIDS]; float savedthrustdec[MAXASTEROIDS]; int savedradius[MAXASTEROIDS]; float savedrotation[MAXASTEROIDS]; float savedrotspeed[MAXASTEROIDS]; bool shipssavedactive[MAXSHIPS]; Vector2 shipssavedposition[MAXSHIPS]; float shipssavedangle[MAXSHIPS]; float shipssavedthrust[MAXSHIPS]; float shipssavedthrustdec[MAXSHIPS]; int shipssavedradius[MAXSHIPS]; // Regular variables. typedef struct asteroid{ bool active; Vector2 position; float angle; float thrust; float thrustdec; int radius; float rotation; float rotspeed; }asteroid; static struct asteroid ast[MAXASTEROIDS]; typedef struct ship{ bool active; Vector2 position; float angle; float thrust; float thrustdec; int radius; }ship; static struct ship shp[MAXSHIPS]; Texture2D sprites; Texture2D rocks; const int screenWidth = 800; const int screenHeight = 450; void drawship(int x, int y, int ship,int rotation); void drawrock(int x, int y, int rock, int rotation); void updateasteroids(); void drawasteroids(); void iniasteroids(); void simulate(); void savestate(); void restorestate(); void updateships(); void drawships(); void updateships(); void iniships(); // Our rectsoverlap function. Returns true/false. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2); int main(void) { // Initialization //-------------------------------------------------------------------------------------- InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); sprites = LoadTexture("resources/spaceships.png"); rocks = LoadTexture("resources/spacerocks.png"); SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- iniasteroids(); iniships(); // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- if(IsKeyPressed(KEY_SPACE)){ simulate(); } updateasteroids(); updateships(); for(int i=0;i0)return; } savestate(); bool collided=false; float rthrust[MAXSHIPS]; for(int j=0;j0){ ast[i].thrust -= ast[i].thrustdec; }else{ ast[i].thrust = 0; } ast[i].rotation+=ast[i].rotspeed;// if(ast[i].rotation>PI*2.0f)ast[i].rotation=0; if(ast[i].rotation<0.0f)ast[i].rotation=PI*2; // if(ast[i].position.y>screenHeight+32)ast[i].position.y=-32; if(ast[i].position.y<-32)ast[i].position.y=screenHeight+32; if(ast[i].position.x<-32)ast[i].position.x=screenWidth+32; if(ast[i].position.x>screenWidth+32)ast[i].position.x=-32; } } void drawships(){ for(int i=0;i0){ shp[i].thrust -= shp[i].thrustdec; }else{ shp[i].thrust = 0; } // if(shp[i].position.y>screenHeight+32)shp[i].position.y=-32; if(shp[i].position.y<-32)shp[i].position.y=screenHeight+32; if(shp[i].position.x<-32)shp[i].position.x=screenWidth+32; if(shp[i].position.x>screenWidth+32)shp[i].position.x=-32; } } void drawship(int x, int y, int ship, int rotation){ // Get the tile x , y position. int ty = ship / 20; int tx = ship-(ty*20);; DrawTexturePro(sprites, (Rectangle){tx*16,ty*16,16,16},// the -96 (-)means mirror on x axis (Rectangle){x,y,32,32}, (Vector2){16,16},rotation,WHITE); } void drawrock(int x, int y, int rock, int rotation){ // Get the tile x , y position. int ty = rock / 20; int tx = rock-(ty*20);; DrawTexturePro(rocks, (Rectangle){tx*16,ty*16,16,16},// the -96 (-)means mirror on x axis (Rectangle){x,y,32,32}, (Vector2){16,16},rotation,WHITE); } // save and restore state are to be used to save the screen and activity before the simulation starts. // when the simulation ends the restorestate can be used to restore to the last saved state. void savestate(){ for(int i=0;i= (x2 + w2) || (x1 + w1) <= x2) return false; if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false; return true; } void iniships(){ for(int i=0;i