Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include "SDL2/SDL.h"
- #define FPS 20
- #define SNAKE_UP 0
- #define SNAKE_RIGHT 1
- #define SNAKE_DOWN 2
- #define SNAKE_LEFT 3
- #define POINT_WIDTH 20
- #define POINT_HEIGHT 20
- #define GAME_HEIGHT 25
- #define GAME_WIDTH 35
- const int WINDOW_WIDTH = POINT_WIDTH * GAME_WIDTH;
- const int WINDOW_HEIGHT = POINT_HEIGHT * GAME_HEIGHT;
- const int SNAKE_MAX_LENGTH = GAME_WIDTH * GAME_HEIGHT;
- typedef struct {
- int x;
- int y;
- } Point;
- typedef struct {
- Point apple;
- Point *snake; // list of points
- int snake_len;
- int snake_direction;
- SDL_Window *window;
- SDL_Renderer *renderer;
- int exit;
- } Game;
- void handle_event(Game *game, SDL_Event *event);
- void move_snake(Game *game);
- void update(Game *game);
- void render(Game *game);
- void initialize(Game *game);
- void terminate(Game *game);
- void debug_info(Game *game);
- int main() {
- srand(time(NULL));
- Game game;
- SDL_Event event;
- initialize(&game);
- while (!game.exit) {
- update(&game);
- render(&game);
- SDL_Delay(1000 / FPS);
- }
- terminate(&game);
- return EXIT_SUCCESS;
- }
- void debug_info(Game *game) {
- printf("\n====DEBUG====\n");
- printf("Apple: (%d, %d)\n", game->apple.x, game->apple.y);
- printf("snake_len: %d\n", game->snake_len);
- printf("snake_direction: %d\n", game->snake_direction);
- printf("snake: ");
- for (int i = 0; i < game->snake_len; i++) {
- printf("(%d, %d) ", game->snake[i].x, game->snake[i].y);
- }
- printf("\n===/DEBUG====\n");
- }
- void handle_event(Game *game, SDL_Event *event) {
- switch (event->type) {
- case SDL_QUIT:
- game->exit = 1;
- return;
- case SDL_KEYDOWN:
- switch (event->key.keysym.scancode) {
- case SDL_SCANCODE_UP:
- printf("walk up\n");
- game->snake_direction = SNAKE_UP;
- break;
- case SDL_SCANCODE_RIGHT:
- printf("walk right\n");
- game->snake_direction = SNAKE_RIGHT;
- break;
- case SDL_SCANCODE_DOWN:
- printf("walk down\n");
- game->snake_direction = SNAKE_DOWN;
- break;
- case SDL_SCANCODE_LEFT:
- printf("walk left\n");
- game->snake_direction = SNAKE_LEFT;
- break;
- }
- break;
- }
- }
- void move_snake(Game *game) {
- Point next_point;
- Point snake_head = game->snake[game->snake_len - 1];
- switch (game->snake_direction) {
- case SNAKE_UP:
- next_point.x = snake_head.x;
- next_point.y = snake_head.y - 1;
- break;
- case SNAKE_RIGHT:
- next_point.x = snake_head.x + 1;
- next_point.y = snake_head.y;
- break;
- case SNAKE_DOWN:
- next_point.x = snake_head.x;
- next_point.y = snake_head.y + 1;
- break;
- case SNAKE_LEFT:
- next_point.x = snake_head.x - 1;
- next_point.y = snake_head.y;
- break;
- }
- next_point.x = (next_point.x + GAME_WIDTH) % GAME_WIDTH;
- next_point.y = (next_point.y + GAME_HEIGHT) % GAME_HEIGHT;
- if (next_point.x == game->apple.x && next_point.y == game->apple.y) {
- // snake eats apple and grows
- if (game->snake_len >= SNAKE_MAX_LENGTH) {
- printf("\n\nERROR: snake length cannot be bigger than %d\n\n", SNAKE_MAX_LENGTH);
- terminate(game);
- exit(EXIT_FAILURE);
- }
- game->snake[game->snake_len].x = next_point.x;
- game->snake[game->snake_len].y = next_point.y;
- game->snake_len++;
- } else {
- // snake remains with the same length
- for (int i = 0; i < game->snake_len - 1; i++) {
- game->snake[i].x = game->snake[i + 1].x;
- game->snake[i].y = game->snake[i + 1].y;
- }
- game->snake[game->snake_len - 1].x = next_point.x;
- game->snake[game->snake_len - 1].y = next_point.y;
- }
- }
- void update(Game* game) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- handle_event(game, &event);
- }
- debug_info(game);
- move_snake(game);
- }
- void render(Game *game) {
- SDL_Rect rect;
- rect.w = POINT_WIDTH;
- rect.h = POINT_HEIGHT;
- // draw backgrond
- SDL_SetRenderDrawColor(game->renderer, 20, 20, 20, 255);
- SDL_RenderClear(game->renderer);
- // draw snake
- SDL_SetRenderDrawColor(game->renderer, 255, 255, 255, 255);
- for (int i = 0; i < game->snake_len; i++) {
- rect.x = game->snake[i].x * POINT_WIDTH;
- rect.y = game->snake[i].y * POINT_HEIGHT;
- SDL_RenderFillRect(game->renderer, &rect);
- }
- // draw apple
- rect.x = game->apple.x * POINT_WIDTH;
- rect.y = game->apple.y * POINT_HEIGHT;
- SDL_SetRenderDrawColor(game->renderer, 255, 20, 20, 255);
- SDL_RenderFillRect(game->renderer, &rect);
- SDL_RenderPresent(game->renderer);
- }
- void initialize(Game *game) {
- SDL_Init(SDL_INIT_EVERYTHING);
- game->window = SDL_CreateWindow("Snake Game",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- WINDOW_WIDTH,
- WINDOW_HEIGHT,
- SDL_WINDOW_SHOWN);
- game->renderer = SDL_CreateRenderer(game->window, -1, 0);
- game->exit = 0;
- game->snake_len = 1;
- game->snake_direction = SNAKE_RIGHT;
- game->snake = (Point*)malloc(SNAKE_MAX_LENGTH * sizeof(Point));
- game->snake[0].x = GAME_WIDTH / 2;
- game->snake[0].y = GAME_HEIGHT / 2;
- }
- void terminate(Game *game) {
- SDL_DestroyWindow(game->window);
- SDL_Quit();
- free(game->renderer);
- game->renderer = NULL;
- free(game->snake);
- game->snake = NULL;
- }
Add Comment
Please, Sign In to add comment