diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | snek.c | 101 |
3 files changed, 104 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b9cfe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +snek diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0f86f66 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + gcc snek.c -lSDL2 -o snek @@ -0,0 +1,101 @@ +#include <SDL2/SDL.h> +#include <stdlib.h> + +#define UP 0 +#define DOWN 1 +#define LEFT 2 +#define RIGHT 3 + +int pos(int x, int y, int width, int height) { + return (x % width) + (y % height) * width; +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + SDL_Window *win = SDL_CreateWindow("Snek automata", 0, 0, 800, 600, 0); + SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + + int width = 40; + int height = 20; + int *grid = calloc(width * height, sizeof(int)); + int direction = RIGHT; + int snake_length = 3; + grid[pos(10, 10, width, height)] = 1; + grid[pos(15, 15, width, height)] = 1 << 15; + + SDL_Event e = {0}; + int quit = 0; + while (e.type != SDL_QUIT && !quit) { + SDL_PollEvent(&e); + int state_len = -1; + const Uint8 *state = SDL_GetKeyboardState(&state_len); + if (state[SDL_SCANCODE_UP] && direction != DOWN) { + direction = UP; + } + if (state[SDL_SCANCODE_DOWN] && direction != UP) { + direction = DOWN; + } + if (state[SDL_SCANCODE_LEFT] && direction != RIGHT) { + direction = LEFT; + } + if (state[SDL_SCANCODE_RIGHT] && direction != LEFT) { + direction = RIGHT; + } + SDL_SetRenderDrawColor(ren, 20, 20, 20, 255); + SDL_RenderClear(ren); + for (int i = 0; i < width * height; i++) { + int cell = grid[i] & 0xffff; + if (cell == (1 << 15) || cell == 0) { + int x = 0; + int y = 0; + switch (direction) { + case UP: y++; break; + case DOWN: y+=height-1; break; + case LEFT: x++; break; + case RIGHT: x+=width-1; break; + } + int peek = grid[pos(i % width + x, i / width + y, width, height)] & 0xffff; + grid[i] = (cell << 16) | cell; + if (peek == 1) { + grid[i] = (1 << 16) | cell; + if (cell == (1 << 15)) { + snake_length++; + int new_pos = pos(rand(), rand(), width, height); + while (grid[new_pos] & 0xffff != 0) { + new_pos = pos(rand(), rand(), width, height); + } + grid[new_pos] |= ((1 << 15) << 16) | (1 << 15); + } + } + } + if (cell > 0 && cell < (1 << 15)) { + if (cell <= snake_length) { + grid[i] = ((cell + 1) << 16) | cell; + } + } + } + for (int i = 0; i < width * height; i++) { + grid[i] = grid[i] >> 16; + + int cell = grid[i] & 0xffff; + if (cell == (1 << 15)) { + SDL_SetRenderDrawColor(ren, 200, 200, 0, 255); + } else if (cell < (1 << 15) && cell > 0) { + SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); + } else { + SDL_SetRenderDrawColor(ren, 40, 40, 40, 255); + } + int y = (i / width) * 11; + int x = (i % width) * 11; + int w = 10; + int h = 10; + SDL_Rect r = { x, y, w, h }; + SDL_RenderFillRect(ren, &r); + } + + SDL_RenderPresent(ren); + SDL_Delay(50); + } + SDL_Quit(); + return 0; +} |