Created
January 7, 2020 04:21
-
-
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
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
#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; | |
} |
stuartallen
commented
Jan 9, 2020
via email
Thanks!!
…On Mon, Jan 6, 2020 at 8:23 PM Roger Allen ***@***.***> wrote:
Hey @stuartallen <https://github.com/stuartallen> here is the code we
worked on from the youtube video for the snake game.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/e6990b2a9e8d471db3ebac52f857d502?email_source=notifications&email_token=AB3HI4GOJHZFP6SP2AWJMX3Q4P7SZA5CNFSM4KDRA2X2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF7AKY#gistcomment-3129516>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB3HI4GAZLCMJTBIBLSWRJLQ4P7SZANCNFSM4KDRA2XQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment