Skip to content

Instantly share code, notes, and snippets.

View Mikepicker's full-sized avatar

Michele Rullo Mikepicker

  • Anzio, RM
View GitHub Profile
// 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;
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);
}
void update() {
// Apply gravity
survivor.vY += world.gravity;
survivor.y += survivor.vY;
// Motion
survivor.x += survivor.vX;
// If survivor is colliding with the platform..
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 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);
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 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;
}
bool loadMedia() {
// Init bullet
bulletTexture = loadTexture("assets/bullet.png");
SDL_QueryTexture(bulletTexture, NULL, NULL, &bulletWidth, &bulletHeight);
}
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;
};
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);