This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 loadMedia() { | |
// Init bullet | |
bulletTexture = loadTexture("assets/bullet.png"); | |
SDL_QueryTexture(bulletTexture, NULL, NULL, &bulletWidth, &bulletHeight); | |
} |
This file contains hidden or 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 BULLET_COUNT = 10; | |
SDL_Texture* bulletTexture; | |
int bulletWidth, bulletHeight; | |
int bulletSpeed = 20; | |
struct Bullet { | |
int x, y; | |
int dir = 1; | |
bool alive = false; | |
}; |
This file contains hidden or 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 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 = survivor.x, .y = 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); |