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. |
— 2D Santa game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction Let It Snow! Let It Snow! Let It Snow! the song goes. So, that's what we're going to do. Our game is more or less complete, but is a bit barren to look at. As we observed in the last part, it should at least be snowing. In this part, that's exactly what we're going to add. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./santa13 to run the code. Press Space to play. Play the game as normal, while enjoying the fresh snowfall. When you're finished, close the window to exit. Inspecting the code Implementing our snow is an extremely simple thing. Our snowflakes are simply squares that move randomly around. Let's start first by taking a look at structs.h:
We've added in a Snow struct, to represent our snokeflakes. `x` and `y` are the positions on screen, `dx` and `dy` are the movement deltas, thinkTime is how long the snowflake will continue at its current velocities before changing direction, and `size` is the size of the snakeflake we want to render. Easy enough. Over now to snow.c, which contains the bulk of the updates in this part. This file contains just a handful of functions, and is very easy to understand. Starting first with initSnow:
We're looping through all the snow in our snow array (static within snow.c), and setting up each item. Each snow object is randomly positioned on screen (`x` and `y`), and given a random `dx` velocity between -2.25 and 2.25. A random `dy` is also assigned, between 1.0 and 2.9. A random thinkTime is assigned, of just below 1 second (FPS), and the `size` is randomly set between 1 and 3. So, we end up with 500 snow objects (MAX_SNOW) that are placed randomly on the screen, and can move left, right, and down. Note that we don't set a negative `dy`, as we don't want our snow to ever move up the screen. Now let's look at the doSnow function, which will drive the logic of our snow:
Once again, we're looping through all our snow objects, and updating their `x` by their `dx`, to make them move. Next, we're testing if any of them have moved off the left or right hand sides of the screen, and if so teleporting them to the other side. The small size and sheer amount of snowflakes means that we don't notice any "popping" that might occur. Next, we add the snowflake's `dy` to its `y`, to make it travel down screen. Once it hits the ground (y >= GROUND_Y) we move it back to the top of the screen (taking into account the snowflake's `size`). Once again, we don't notice any popping. Finally, we reduce the snowflake's thinkTime. Once it hits 0 or less, we update its `dx` value, assigning it a new value between -2.25 and 2.25, as we did to begin with. We then add FPS to the thinkTime. This means that our snowflakes will randomly change direction and velocity once a second. Once more, due to the sheer number of snowflakes falling, this results in a rather pleasant effect. The small `dx` values means direction changes aren't too intense either. I'm sure you'll agree it looks quite convincing. Now for our rendering section, drawSnow:
This couldn't get easier. We're looping through all our snow, and calling drawRect for each, using the snow's `x`, `y`, and `size`, to render a white square on screen at its current position. That's it for snow.c! We need only make some updates to stage.c to setup, drive, and draw our snow, and we'll be all done. Let's start with initStage:
We've added the call to initSnow. Next up is doStage:
Here, we're calling doSnow, just after doEntites. Finally, we've updated drawStage:
Right after drawEntities, we're calling drawSnow. And that's that. Adding snow to our game couldn't have been easier. SDL2 Santa is now much more pleasant and seasonal to look at. But we could still add more. What about some snowy hills in the background, to further enhance the wintery landscape? Well, in the penultimate part, we're going to add in some hills that move from right to left, using a small parallax effect. We'll dot some trees around for extra good measure. Purchase The source code for all parts of this tutorial (including assets) is available for purchase, as part of the SDL2 tutorials bundle: From itch.io |