This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Font | |
TTF_Font* gFont = NULL; | |
// Score | |
int score = 0; | |
SDL_Texture* scoreTexture; | |
int scoreWidth, scoreHeight; | |
// Highscore | |
Sint32 highScore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each alive bullet.. | |
for (int i = 0; i < BULLET_COUNT; i++) { | |
if (bullets[i].alive) { | |
bullets[i].x += bulletSpeed * bullets[i].dir; | |
// Out of screen -> dead | |
if (bullets[i].x + bullets[i].w < 0 || bullets[i].x > SCREEN_WIDTH) { | |
bullets[i].alive = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each alive zombie.. | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (zombies[i].alive) { | |
// Out of screen -> dead | |
if (zombies[i].y > SCREEN_HEIGHT) { | |
zombies[i].alive = false; | |
} | |
// Apply gravity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If survivor is alive.. | |
if (survivor.state != "state_dead") { | |
... | |
// If survivor is stabbing.. | |
if (survivor.state == "state_stab") { | |
// If we're at the third animation frame, and attack has not been delivered yet.. | |
if (survivor.frameX / survivor.animSpeed == 2 && !survivor.stab) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check collision between two boxes (http://lazyfoo.net/tutorials/SDL/27_collision_detection/index.php) | |
bool collision(float xA, float xB, float yA, float yB, int wA, int wB, int hA, int hB) { | |
if (yA + hA <= yB) { | |
return false; | |
} | |
if (yA >= yB + hB) { | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int ZOMBIE_COUNT = 20; | |
const int SPAWN_FREQ = 3; | |
unsigned int lastSpawnTime = 0, currentTime; | |
int zombieAnimSpeed = 8; | |
int zombieSpeed = 3; | |
SDL_Texture* zombieTexture; | |
struct Zombie { | |
float x, y; | |
int w = 64; // Width and height have been moved here for a cleaner code | |
int h = 64; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void render() { | |
// Clear screen | |
SDL_RenderClear(gRenderer); | |
// Render zombies | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (zombies[i].alive) { | |
SDL_Rect srcZombie = { .x = (zombies[i].frameX / zombieAnimSpeed) * zombieWidth, .y = zombies[i].frameY * zombieHeight, .w = zombieWidth, .h = zombieHeight }; | |
SDL_Rect dstZombie = { .x = (int)zombies[i].x, .y = (int)zombies[i].y, .w = zombieWidth, .h = zombieHeight }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool platformCollision(float x, float y, int w, int h) { | |
return y + h > platform.y && | |
x + 48 >= platform.x && | |
x + w - 48 <= platform.x + platform.w; | |
} | |
void update() { | |
// Zombies | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int randInRange(int min, int max) { | |
return rand() % (max + 1 - min) + min; | |
} | |
void spawnZombie() { | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (!zombies[i].alive) { | |
zombies[i].frameX = 0; | |
zombies[i].frameY = 0; | |
zombies[i].x = randInRange(platform.x, platform.x + platform.w - zombieWidth); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <time.h> | |
... | |
void loadMedia() { | |
// Seed random number generator | |
srand(time(NULL)); | |