Skip to content

Instantly share code, notes, and snippets.

View Mikepicker's full-sized avatar

Michele Rullo Mikepicker

  • Anzio, RM
View GitHub Profile
// Font
TTF_Font* gFont = NULL;
// Score
int score = 0;
SDL_Texture* scoreTexture;
int scoreWidth, scoreHeight;
// Highscore
Sint32 highScore;
// 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;
}
// 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
// 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) {
// 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;
}
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;
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 };
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++) {
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);
#include <stdlib.h>
#include <time.h>
...
void loadMedia() {
// Seed random number generator
srand(time(NULL));