Skip to content

Instantly share code, notes, and snippets.

View Mikepicker's full-sized avatar

Michele Rullo Mikepicker

  • Anzio, RM
View GitHub Profile
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;
}
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;
}
}
}
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);
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;
void update() {
// Apply gravity
survivor.vY += world.gravity;
survivor.y += survivor.vY;
// Motion
survivor.x += survivor.vX;
// If survivor is colliding with the platform..
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);
}
// 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;
#include <stdlib.h>
#include <time.h>
...
void loadMedia() {
// Seed random number generator
srand(time(NULL));
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);
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++) {