PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (43) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (17) • water-closet (4) Books The Attribute of the Strong (Battle for the Solar System, #3) The Pandoran War is nearing its end... and the Senate's Mistake have all but won. Leaving a galaxy in ruin behind them, they set their sights on Sol and prepare to finish their twelve year Mission. All seems lost. But in the final forty-eight hours, while hunting for the elusive Zackaria, the White Knights make a discovery in the former Mitikas Empire that could herald one last chance at victory. |
Basic Tutorials Basic Game Tutorial #9 - Starfields
Introduction This tutorial demonstrates how to display a moving starfield on the screen. Compile and run tutorial09. The stars on the screen will move from right to left at various speeds. Pressing Escape or closing the window will exit the program. An in-depth look Creating a moving starfield is very simple and there are only a few files of note for this tutorial. First, we define a structure for our starfield in structs.h: typedef struct Star { int x, y, speed; } Star;The x and y variables define the star's location on the screen and the speed defines its movement speed. Next we will look at the stars.c file. stars.c contains 4 functions: void resetStars() { int i; for (i=0;i<MAX_STARS;i++) { star[i].x = rand() % SCREEN_WIDTH; star[i].y = rand() % SCREEN_HEIGHT; star[i].speed = 1 + (rand() % 12); } }resetStars randomly places all of the stars on the screen with a random speed. We need to do this before entering our main loop otherwise all of the stars might suddenly all appear on the right hand side of the screen which wouldlook wrong. void doStars() { int i; for (i=0;i<MAX_STARS;i++) { star[i].x -= star[i].speed; if (star[i].x < 0) { star[i].x = SCREEN_WIDTH + rand() % 20; star[i].y = rand() % SCREEN_HEIGHT; star[i].speed = 1 + (rand() % 12); } } }doStars loops though all of the stars and moves them across the screen from right to left by subtracting their speed from their current x position. If a star's x position becomes negative then it has moved off the screen, so we randomly set its x position to be the screen's width plus a random amount to stop it appearing immediately and also set its y and speed. void drawStars() { int i; SDL_Rect rect; for (i=0;i<MAX_STARS;i++) { if (star[i].x < SCREEN_WIDTH) { rect.x = star[i].x; rect.y = star[i].y; rect.w = 1; rect.h = 1; SDL_FillRect(screen, &rect, getStarColor(star[i].speed)); } } }drawStars is another simple function. We loop through all the stars and, if their x position is on the screen, we draw it using SDL_FillRect. We determine its colour by calling the function getStarColor which is described below. int getStarColor(int speed) { SDL_Color color; switch (speed) { case 1: case 2: case 3: color.r = 159; color.g = 159; color.b = 159; break; case 4: case 5: case 6: color.r = 191; color.g = 191; color.b = 191; break; case 7: case 8: case 9: color.r = 223; color.g = 223; color.b = 223; break; default: color.r = 255; color.g = 255; color.b = 255; break; } return SDL_MapRGB(screen->format, color.r, color.g, color.b); }getStarColor takes a simple parameter, the speed we wish to test. We then switch this speed and set the RGB values of the SDL_Color. The lower the speed, the darker the star will be, since it's further away. Finally, we use SDL_MapRGB to get the correct value for the colour we've chosen and return it. In main.c we animate the stars as follows: /* Initialise the stars */ resetStars(); /* Loop indefinitely for messages */ while (go == 1) { getInput(); /* Update the stars */ doStars(); /* Blank the screen */ SDL_FillRect(screen, NULL, 0); /* Draw the stars */ drawStars(); /* Swap the buffers */ SDL_Flip(screen); /* Sleep briefly to stop sucking up all the CPU time */ SDL_Delay(1); delay(frameLimit); frameLimit = SDL_GetTicks() + 16; }First we position the stars before entering the main loop by calling resetStars. Then, in our main loop, we call doStars to update their positions and drawStars to draw them. That's really all there is to it. Conclusion Now that we can accept user input, perform collision detection, use SDL_TTF to render text to the screen and animate sprites, we can put this all together to make a small game. We will do this in the next tutorial. Downloads |