Skip to content

Instantly share code, notes, and snippets.

@rogerallen
Created January 7, 2020 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerallen/e6990b2a9e8d471db3ebac52f857d502 to your computer and use it in GitHub Desktop.
Save rogerallen/e6990b2a9e8d471db3ebac52f857d502 to your computer and use it in GitHub Desktop.
Snake came coded with Stuart from https://www.youtube.com/watch?v=ETJUyTZpQCM
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <cstdlib>
const int TAIL_MAX = 255;
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 800;
const int BLOCK_SIZE = 16;
struct Food {
int x, y;
void move() {
x = rand() % (WINDOW_WIDTH / BLOCK_SIZE);
y = rand() % (WINDOW_HEIGHT / BLOCK_SIZE);
}
void draw(SDL_Renderer* renderer) {
SDL_Rect r{ BLOCK_SIZE * x, BLOCK_SIZE * y, BLOCK_SIZE, BLOCK_SIZE };
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &r);
}
};
struct V2 {
int x, y;
};
V2 operator+ (const V2 &a, const V2 &b) {
return V2{ a.x + b.x, a.y + b.y };
}
void operator+= (V2 &a, const V2 &b) {
a.x += b.x;
a.y += b.y;
}
bool operator== (const V2 &a, const V2 &b) {
return a.x == b.x && a.y == b.y;
}
struct Snake {
V2 pos;
V2 vel;
V2 tail[TAIL_MAX];
int tail_start, tail_end;
int tail_len;
uint32_t accumulator;
void update(uint32_t delta_time, Food& food) {
accumulator += delta_time;
if (accumulator > 100) { // update every 100ms
accumulator = 0;
tail_start++;
tail[tail_end] = pos;
tail_end++;
pos += vel;
if (pos.x < 0) {
pos.x = (WINDOW_WIDTH / BLOCK_SIZE) - 1;
}
if (pos.y < 0) {
pos.y = (WINDOW_HEIGHT / BLOCK_SIZE) - 1;
}
if (pos.x >= (WINDOW_WIDTH / BLOCK_SIZE)) {
pos.x = 0;
}
if (pos.y >= (WINDOW_HEIGHT / BLOCK_SIZE)) {
pos.y = 0;
}
if (pos.x == food.x && pos.y == food.y) {
tail_len += 1;
tail_start--;
food.move();
}
for (int i = 0; i < tail_len; i++) {
V2& tail_pos = tail[(tail_start + i) % TAIL_MAX];
if (tail_pos == pos) {
// we iz dead
tail_len = 0;
tail_start = tail_end;
}
}
tail_start %= TAIL_MAX;
tail_end %= TAIL_MAX;
}
}
void draw(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
for (int i = 0; i < tail_len; i++) {
V2& tail_pos = tail[(tail_start + i) % TAIL_MAX];
SDL_Rect r{ BLOCK_SIZE * tail_pos.x, BLOCK_SIZE * tail_pos.y, BLOCK_SIZE, BLOCK_SIZE };
SDL_RenderFillRect(renderer, &r);
}
SDL_Rect r{ BLOCK_SIZE * pos.x, BLOCK_SIZE * pos.y, BLOCK_SIZE, BLOCK_SIZE };
SDL_RenderFillRect(renderer, &r);
}
};
int WinMain(int, int, char*, int) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Snake",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event e;
bool running = true;
Snake snake = {};
Food food = {};
food.move();
uint32_t current_time = 0, previous_time, delta_time;
while (running) {
previous_time = current_time;
current_time = SDL_GetTicks();
delta_time = current_time - previous_time;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
running = false;
}
else if(e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
running = false;
break;
case SDLK_w:
case SDLK_UP:
if (snake.vel.y != 1) {
snake.vel.y = -1;
snake.vel.x = 0;
}
break;
case SDLK_s:
case SDLK_DOWN:
if (snake.vel.y != -1) {
snake.vel.y = 1;
snake.vel.x = 0;
}
break;
case SDLK_a:
case SDLK_LEFT:
if (snake.vel.x != 1) {
snake.vel.y = 0;
snake.vel.x = -1;
}
break;
case SDLK_d:
case SDLK_RIGHT:
if (snake.vel.x != -1) {
snake.vel.y = 0;
snake.vel.x = 1;
}
break;
}
}
}
snake.update(delta_time, food);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
snake.draw(renderer);
food.draw(renderer);
SDL_RenderPresent(renderer);
}
return 0;
}
@rogerallen
Copy link
Author

Hey @stuartallen here is the code we worked on from the youtube video for the snake game.

@stuartallen
Copy link

stuartallen commented Jan 9, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment