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 shootBullet() { | |
for (int i = 0; i < BULLET_COUNT; i++) { | |
if (!bullets[i].alive) { | |
if (survivor.scaleX == 1) { | |
bullets[i].x = survivor.x + 48; | |
} else { | |
bullets[i].x = survivor.x + 16; | |
} |
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 update() { | |
for (int i = 0; i < BULLET_COUNT; i++) { | |
if (bullets[i].alive) { | |
bullets[i].x += bulletSpeed * bullets[i].dir; | |
if (bullets[i].x + bulletWidth < 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
void render() { | |
// Clear screen | |
SDL_RenderClear(gRenderer); | |
// Render bullets | |
for (int i = 0; i < BULLET_COUNT; i++) { | |
if (bullets[i].alive) { | |
SDL_Rect dstBullet = { .x = bullets[i].x, .y = bullets[i].y, .w = bulletWidth, .h = bulletHeight }; | |
SDL_RenderCopy(gRenderer, bulletTexture, NULL, &dstBullet); |
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
struct Survivor { | |
float x, y; | |
int w, h; | |
float vX, vY; | |
int scaleX, scaleY; | |
int frameX, frameY; | |
int animSpeed = 5; | |
int speed = 3, jumpSpeed = 10; | |
bool animCompleted = false; | |
bool shot = 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
void update() { | |
// Apply gravity | |
survivor.vY += world.gravity; | |
survivor.y += survivor.vY; | |
// Motion | |
survivor.x += survivor.vX; | |
// If survivor is colliding with the platform.. |
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() { | |
// Render survivor | |
SDL_Rect srcSurv = { .x = (survivor.frameX / survivor.animSpeed) * survivor.w, .y = survivor.frameY * survivor.h, .w = survivor.w, .h = survivor.h }; | |
SDL_Rect dstSurv = { .x = (int)survivor.x, .y = (int)survivor.y, .w = survivor.w, .h = survivor.h }; | |
SDL_RendererFlip flip = survivor.scaleX == 1 ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; | |
SDL_RenderCopyEx(gRenderer, survivor.texture, &srcSurv, &dstSurv, 0, NULL, flip); | |
} |
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
// Constants | |
const int ZOMBIE_COUNT = 20; // Pre-allocate memory for hosting 20 zombies | |
const int SPAWN_FREQ = 3; // Spawn frequency (in seconds) | |
// Timer | |
unsigned int lastSpawnTime = 0, currentTime; | |
// Global properties | |
int zombieWidth = 64; | |
int zombieHeight = 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
#include <stdlib.h> | |
#include <time.h> | |
... | |
void loadMedia() { | |
// Seed random number generator | |
srand(time(NULL)); | |
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
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++) { |