Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include <stdio.h>
- #define TILE_WIDTH 12
- #define TILE_HEIGHT 12
- #define SCREEN_WIDTH 960
- #define SCREEN_HEIGHT 720
- #define FEILD_WIDTH (SCREEN_WIDTH / TILE_WIDTH)
- #define FEILD_HEIGHT (SCREEN_HEIGHT / TILE_HEIGHT)
- #define FPS 30
- SDL_Window* window;
- SDL_Renderer* renderer;
- SDL_Event current_event;
- void cleanup(int, const char* , ...);
- int** tiles;
- int snake_x = FEILD_WIDTH / 2;
- int snake_y = FEILD_HEIGHT / 2;
- int snake_length = 4;
- unsigned int counter = 0;
- int snake_counter = 0;
- int snake_speed = FPS*2;
- int snake_grow = 10;
- #define SNAKE_DIRECTION_NONE 0
- #define SNAKE_DIRECTION_UP 1
- #define SNAKE_DIRECTION_DOWN 2
- #define SNAKE_DIRECTION_LEFT 3
- #define SNAKE_DIRECTION_RIGHT 4
- int snake_direction = SNAKE_DIRECTION_NONE;
- int next_direction = SNAKE_DIRECTION_LEFT;
- #include <stdlib.h>
- void spawn_new_food() {
- int food_x = 0;
- int food_y = 0;
- do {
- food_x = rand() % FEILD_WIDTH;
- food_y = rand() % FEILD_HEIGHT;
- } while (tiles[food_y][food_x] != 0);
- tiles[food_y][food_x] = -1;
- }
- void update_game() {
- snake_counter--;
- if (snake_counter <= 0) {
- snake_counter += snake_speed;
- if (next_direction != snake_direction) {
- snake_direction = next_direction;
- }
- switch (snake_direction) {
- case SNAKE_DIRECTION_UP:
- snake_y--;
- break;
- case SNAKE_DIRECTION_DOWN:
- snake_y++;
- break;
- case SNAKE_DIRECTION_LEFT:
- snake_x--;
- break;
- case SNAKE_DIRECTION_RIGHT:
- snake_x++;
- break;
- }
- if (snake_x < 0 || snake_y < 0 || snake_x >= FEILD_WIDTH || snake_y >= FEILD_HEIGHT) {
- cleanup(0, "You died\n");
- }
- for (int y = 0; y < FEILD_HEIGHT; y++) {
- for (int x = 0; x < FEILD_WIDTH; x++) {
- if (tiles[y][x] > 0) tiles[y][x]--;
- }
- }
- int tile = tiles[snake_y][snake_x];
- if (tile == -1) {
- snake_length += snake_grow;
- spawn_new_food();
- }
- if (tile > 0) {
- cleanup(0, "You died\n");
- }
- tiles[snake_y][snake_x] = snake_length;
- }
- }
- void draw_game() {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- SDL_SetRenderDrawColor(renderer, 192, 192, 0, 255);
- for (int y = 0; y < FEILD_HEIGHT; y++) {
- for (int x = 0; x < FEILD_WIDTH; x++) {
- int tile = tiles[y][x];
- if (tile > 0) {
- SDL_Rect r = { x * TILE_WIDTH,y * TILE_HEIGHT,TILE_WIDTH,TILE_HEIGHT };
- SDL_RenderFillRect(renderer, &r);
- }
- else if (tile == -1) {
- SDL_SetRenderDrawColor(renderer, 192, 128, 128, 255);
- SDL_Rect r = { x * TILE_WIDTH,y * TILE_HEIGHT,TILE_WIDTH,TILE_HEIGHT };
- SDL_RenderFillRect(renderer, &r);
- SDL_SetRenderDrawColor(renderer, 192, 192, 0, 255);
- }
- }
- }
- SDL_RenderPresent(renderer);
- }
- Uint32 tick(Uint32 interval, void* name) {
- counter++;
- return 1;
- }
- void setup()
- {
- window = SDL_CreateWindow("Snake Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
- if (window == NULL) cleanup(1, "Could not init window\n");
- renderer = SDL_CreateRenderer(window, -1, 0);
- if (renderer == NULL) cleanup(1,"Could not init renderer\n");
- SDL_AddTimer(1000. / FPS, &tick, NULL);
- //allocate the play field
- tiles = (int**)SDL_malloc(sizeof(int*) * FEILD_HEIGHT);
- if (tiles == NULL) cleanup(1, "Ran out of memory allocating tiles\n");
- for (int y = 0; y < FEILD_HEIGHT; y++) {
- tiles[y] = (int *)SDL_malloc(sizeof(int) * FEILD_WIDTH);
- if (tiles[y] == NULL) cleanup(1, "Ran out of memory allocating tiles\n");
- for (int x = 0; x < FEILD_WIDTH; x++) {
- tiles[y][x] = 0;
- }
- }
- spawn_new_food();
- }
- void cleanup(int code = 0, const char *message = NULL, ... ) {
- if (message != NULL) {
- va_list args;
- va_start(args, message);
- vprintf(message, args);
- va_end(args);
- }
- //Unallocate our play field
- if (tiles != NULL) {
- for (int y = 0; y < FEILD_HEIGHT; y++) {
- if (tiles[y] != NULL) SDL_free(tiles[y]);
- }
- SDL_free(tiles);
- }
- if (renderer != NULL) SDL_DestroyRenderer(renderer);
- if (window != NULL) SDL_DestroyWindow(window);
- exit(code);
- }
- void game_loop() {
- while (true) {
- if (SDL_PollEvent(¤t_event)) {
- switch (current_event.type) {
- case SDL_QUIT:
- cleanup(0);
- break;
- case SDL_KEYDOWN:
- switch (current_event.key.keysym.sym) {
- case SDLK_UP:
- if (snake_direction != SNAKE_DIRECTION_DOWN) {
- next_direction = SNAKE_DIRECTION_UP;
- }
- break;
- case SDLK_DOWN:
- if (snake_direction != SNAKE_DIRECTION_UP) {
- next_direction = SNAKE_DIRECTION_DOWN;
- }
- break;
- case SDLK_LEFT:
- if (snake_direction != SNAKE_DIRECTION_RIGHT) {
- next_direction = SNAKE_DIRECTION_LEFT;
- }
- break;
- case SDLK_RIGHT:
- if (snake_direction != SNAKE_DIRECTION_LEFT) {
- next_direction = SNAKE_DIRECTION_RIGHT;
- }
- break;
- }
- break;
- }
- }
- while (counter > 0) {
- update_game();
- counter--;
- }
- draw_game();
- }
- }
- int main(int argc, char* argv[])
- {
- setup();
- game_loop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement