Generic Zombie Game is a simple top-down zombie survival shooter built with C++ and SFML. The gameplay consists of surviving against indefinitely spawning zombies with a choice of 3 weapons – a pistol, assault rifle and a knife. The game also features a simple day and night cycle, and you can press Q to equip the flashlight during night time. Be careful though, you can’t defend yourself with the flashlight!
This project is intended as an implementation of linear algebra used in 2D graphics.
The player moves around the world using the WASD keys and uses the mouse to both aim and fire. The player’s sprite also points towards the direction of the cursor with the following steps:
- Convert the mouse cursor’s screen space position in to world space
- Calculate the vector from the player’s world position to the cursor’s world position (cursorWorld – playerWorld)
- Calculate the angle of this vector using arc-tangent and apply this angle as a rotation on the player sprite
The same vector is also used to position the camera, giving the appearance of the camera “following” the cursor.
The game makes use of animated sprites for the player and zombies. These work by loading spritesheets (an image with all frames of the animation stored in one) alongside a text file which provides information on where each frame of the animation is placed and how large they are. These values are loaded in to an array and a sub-section of the associated sprite sheet is rendered to the screen depending on the current time and frame.
The day and night cycle – as well as the flashlight effect – works using SFML’s blend mode wrapper around OpenGL blending:
- Draw a solid rectangle to a different render target that covers the entire screen
- Set the alpha channel of the rectangle to some value based on sin(runtime * small_number) to simulate sun rise and set
- Draw the flashlight sprite (which is just a white spot with transparency) over the rectangle using Additive blending
- Convert the result to a texture and draw it over the gameplay screen with Multiply blending