Advertisement
Guest User

shitty snake

a guest
Jan 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_ttf.h>
  5. #include <time.h>
  6.  
  7. #define UP 0
  8. #define DOWN 1
  9. #define RIGHT 2
  10. #define LEFT 3
  11.  
  12. int WIDTH, HEIGHT, TILE_SIZE;
  13.  
  14. void moveSnakeParts(int snake[WIDTH*HEIGHT][2], int snakeSize);
  15. int notInSnake(int snake[WIDTH*HEIGHT][2], int snakeSize, int fruit[2]);
  16. int biteSnake(int snake[WIDTH*HEIGHT][2], int snakeSize, int position[2]);
  17.  
  18. int main(int argc, char **argv) {
  19.     if(argc == 1) {
  20.         WIDTH = 20;
  21.         HEIGHT = 20;
  22.         TILE_SIZE = 20;
  23.     }
  24.     else if(argc == 3) {
  25.         WIDTH = atoi(argv[1]);
  26.         HEIGHT = atoi(argv[2]);
  27.         TILE_SIZE = 20;
  28.     }
  29.     else if(argc == 4) {
  30.         WIDTH = atoi(argv[1]);
  31.         HEIGHT = atoi(argv[2]);
  32.         TILE_SIZE = atoi(argv[3]);
  33.     }
  34.  
  35.  
  36.  
  37.     // init SDL2
  38.     if(SDL_Init(SDL_INIT_VIDEO) <0) {
  39.         printf("Couldn't init SDL : %s\n", SDL_GetError());
  40.         exit(EXIT_FAILURE);
  41.     }
  42.     // init TTF
  43.     if(TTF_Init()==-1) {
  44.         printf("could not init TTF : %s\n", TTF_GetError());
  45.         exit(EXIT_FAILURE);
  46.     }
  47.  
  48.  
  49.     // create a window and renderer
  50.     SDL_Window *window =  SDL_CreateWindow("snake", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH*TILE_SIZE, HEIGHT*TILE_SIZE, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
  51.     SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
  52.  
  53.     SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255);
  54.     SDL_RenderFillRect( renderer, NULL);
  55.  
  56.     //check renderer was created, else exit
  57.     if(renderer == NULL) {
  58.         printf("Error creating the renderer, exiting now.\n");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.    
  62.     // create two textures and load a picture on it (lazy mode : a color)
  63.     // one is the snake body, one is an empty place on the map
  64.     SDL_Texture *snake_text;
  65.     SDL_Texture *empty_text;
  66.     SDL_Texture *fruit_text;
  67.  
  68.     SDL_Surface *temp = SDL_CreateRGBSurface(0, TILE_SIZE, TILE_SIZE, 32, 0, 0, 0, 0);
  69.     if(temp == NULL) {
  70.         printf("Error creating the temp. surface, existing now.\n");
  71.         exit(EXIT_FAILURE);
  72.     }
  73.    
  74.     SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 240, 230, 240));
  75.  
  76.     empty_text = SDL_CreateTextureFromSurface(renderer, temp);
  77.  
  78.     SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 240, 0, 0));
  79.  
  80.     snake_text = SDL_CreateTextureFromSurface(renderer, temp);
  81.  
  82.     SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 0,0,255));
  83.  
  84.     fruit_text = SDL_CreateTextureFromSurface(renderer, temp);
  85.  
  86.     SDL_FreeSurface(temp);
  87.  
  88.  
  89.     //graphical stuff is loaded.
  90.     // Now create the variables that will be used on the project   
  91.    
  92.     int snake[WIDTH * HEIGHT][2];
  93.     int snakeSize = 3;
  94.     snake[0][0] = 2;snake[0][1]=0;
  95.     snake[1][0] = 1;snake[1][1]=0;
  96.     snake[2][0] = 0;snake[2][1]=0;
  97.  
  98.     int i, j;
  99.  
  100.     SDL_Rect position;
  101.     position.w = TILE_SIZE;
  102.     position.h = TILE_SIZE;
  103.     position.x=0;
  104.     position.y=0;
  105.  
  106.     // fill the screen with empty tiles
  107.  
  108.     for(i=0 ; i<WIDTH ; i++) {
  109.         position.x = i*TILE_SIZE;
  110.         for(j=0 ; j<HEIGHT ; j++) {
  111.             position.y = j*TILE_SIZE;
  112.             SDL_RenderCopy(renderer, empty_text, NULL, &position);
  113.         }
  114.     }
  115.  
  116.     // put the snake on the field
  117.  
  118.     for(i=0 ; i< snakeSize ; i++) {
  119.         position.x = snake[i][0]*TILE_SIZE;
  120.         position.y = snake[i][1]*TILE_SIZE;
  121.  
  122.         SDL_RenderCopy(renderer, snake_text, NULL, &position);
  123.     }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.     srand(time(NULL));
  130.  
  131.  
  132.     SDL_Event event;
  133.     int quit=0, direction = RIGHT;
  134.     int fruit[2];
  135.     int nbTurnFruit[128];
  136.     int nbFruits=0;
  137.  
  138.  
  139.     fruit[0] = rand() % WIDTH;
  140.     fruit[1] = rand() % HEIGHT;
  141.  
  142.  
  143.     position.x = fruit[0] * TILE_SIZE;
  144.     position.y = fruit[1] * TILE_SIZE;
  145.  
  146.     SDL_RenderCopy(renderer, fruit_text, NULL, &position);
  147.  
  148.     SDL_RenderPresent(renderer);
  149.    
  150.     do {
  151.         SDL_PollEvent(&event);
  152.  
  153.         if(event.type == SDL_QUIT) {
  154.             quit=1;
  155.         }
  156.         else if(event.type == SDL_KEYDOWN) {
  157.             switch(event.key.keysym.scancode) {
  158.                 case SDL_SCANCODE_ESCAPE :
  159.                     quit=1;
  160.                     break;
  161.  
  162.                 case SDL_SCANCODE_LEFT :
  163.                     if(direction != RIGHT) {
  164.                         direction = LEFT;
  165.                     }
  166.                     break;
  167.  
  168.                 case SDL_SCANCODE_RIGHT :
  169.                     if(direction != LEFT) {
  170.                         direction = RIGHT;
  171.                     }
  172.                     break;
  173.  
  174.                 case SDL_SCANCODE_UP :
  175.                     if(direction != DOWN) {
  176.                         direction = UP;
  177.                     }
  178.                     break;
  179.  
  180.                 case SDL_SCANCODE_DOWN :
  181.                     if(direction != UP) {
  182.                         direction = DOWN;
  183.                     }
  184.                     break;
  185.  
  186.  
  187.                 default :
  188.                     break;
  189.  
  190.             }
  191.  
  192.         }
  193.  
  194.         // events end.
  195.  
  196.        
  197.        
  198.         // first we clear the tail
  199.         position.x= snake[snakeSize-1][0] * TILE_SIZE;
  200.         position.y= snake[snakeSize-1][1] * TILE_SIZE;
  201.         SDL_RenderCopy(renderer, empty_text, NULL, &position);
  202.        
  203.         // add fruit to the list if needed
  204.         for(i=0 ; i<nbFruits ; i++) {
  205.             nbTurnFruit[i]--;
  206.             if(nbTurnFruit[i] == 0) {
  207.                 snake[snakeSize][0] = 0;
  208.                 snake[snakeSize][1] = 0;
  209.                 snakeSize++;
  210.                 SDL_RenderCopy(renderer, snake_text, NULL, &position);
  211.  
  212.             }
  213.         }
  214.  
  215.         switch(direction) {
  216.             case RIGHT :
  217.                 moveSnakeParts(snake, snakeSize);
  218.                 snake[0][0]++;
  219.                
  220.                 break;
  221.  
  222.  
  223.             case LEFT :
  224.                 moveSnakeParts(snake, snakeSize);
  225.                 snake[0][0]--;
  226.  
  227.                 break;
  228.  
  229.  
  230.             case UP :
  231.                 moveSnakeParts(snake, snakeSize);
  232.                 snake[0][1]--;
  233.  
  234.                 break;
  235.  
  236.  
  237.             case DOWN :
  238.                 moveSnakeParts(snake, snakeSize);
  239.                 snake[0][1]++;
  240.  
  241.                 break;
  242.  
  243.  
  244.             default :
  245.                 break;
  246.         }
  247.         // test that snake isn't out of the field
  248.        
  249.         if(snake[0][0] < 0) {
  250.             snake[0][0] = WIDTH - 1;
  251.         }
  252.         else if(snake[0][0] == WIDTH) {
  253.             snake[0][0] = 0;
  254.         }
  255.         else if(snake[0][1] < 0) {
  256.             snake[0][1] = HEIGHT - 1;
  257.         }
  258.         else if(snake[0][1] == HEIGHT) {
  259.             snake[0][1] = 0;
  260.         }
  261.  
  262.         // print the head
  263.         position.x= snake[0][0] * TILE_SIZE;
  264.         position.y= snake[0][1] * TILE_SIZE;
  265.         SDL_RenderCopy(renderer, snake_text, NULL, &position);
  266.  
  267.        
  268.         // if head is on the fruit
  269.         if(snake[0][0] == fruit[0] && snake[0][1] == fruit[1]) {
  270.             nbTurnFruit[nbFruits] = snakeSize;
  271.             nbFruits++;
  272.             do {
  273.                 fruit[0] = rand() % WIDTH;
  274.                 fruit[1] = rand() % HEIGHT;
  275.             }while (notInSnake(snake, snakeSize, fruit));
  276.  
  277.             position.x = fruit[0] * TILE_SIZE;
  278.             position.y = fruit[1] * TILE_SIZE;
  279.  
  280.             SDL_RenderCopy(renderer, fruit_text, NULL, &position);
  281.  
  282.         }
  283.  
  284.         if(biteSnake(snake, snakeSize, snake[0])) {
  285.             quit = 1;
  286.         }
  287.  
  288.  
  289.         SDL_RenderPresent(renderer);
  290.         SDL_Delay(60);
  291.  
  292.  
  293.  
  294.     }while(!quit);
  295.  
  296.    
  297.     SDL_Texture *text_score;
  298.     SDL_Rect score_position;
  299.     TTF_Font *font;
  300.     char str_score[256];
  301.  
  302.     // load the font
  303.     SDL_Color color= {100,0,0};
  304.     font = TTF_OpenFont("/usr/share/fonts/TTF/EXH.TTF", 32);
  305.     if(!font) {
  306.         printf("Impossible to open font : %s\n", TTF_GetError());;
  307.         exit(EXIT_FAILURE);
  308.     }
  309.  
  310.     // write some text with the font on a temp surface
  311.     //temp = SDL_CreateRGBSurface(0, 50, 30, 32, 0, 0, 0, 0);
  312.     //SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 250, 0, 0));
  313.  
  314.  
  315.     sprintf(str_score, "you did %d", snakeSize-3);
  316.     temp = TTF_RenderText_Blended(font, str_score,color);
  317.     if(temp == NULL) {
  318.         printf("Impossible to render the score on surface : %s\n", TTF_GetError());;
  319.         exit(EXIT_FAILURE);
  320.     }
  321.  
  322.     //create the texture from this surface, set its position and print it
  323.     text_score = SDL_CreateTextureFromSurface(renderer, temp);
  324.    
  325.     score_position.x = (WIDTH*TILE_SIZE)/2 - temp->w/2;
  326.     score_position.y = (HEIGHT*TILE_SIZE)/2 - temp->h/2;
  327.     score_position.w = temp->w;
  328.     score_position.h = temp->h;
  329.  
  330.     SDL_RenderClear(renderer);
  331.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  332.     SDL_RenderFillRect(renderer, NULL);
  333.     SDL_RenderCopy(renderer, text_score, NULL, &score_position);
  334.  
  335.     SDL_RenderPresent(renderer);
  336.    
  337.     quit=0;
  338.     do {
  339.         SDL_WaitEvent(&event);
  340.         if(event.type == SDL_QUIT) {
  341.             quit=1;
  342.         }
  343.         else if(event.type == SDL_KEYDOWN) {
  344.             switch(event.key.keysym.scancode) {
  345.                 case SDL_SCANCODE_ESCAPE :
  346.                     quit=1;
  347.                     break;
  348.                 case SDL_SCANCODE_RETURN :
  349.                     quit=1;
  350.                     break;
  351.                 default :
  352.                     break;
  353.             }
  354.         }
  355.  
  356.     }while(!quit);
  357.  
  358.  
  359.     SDL_FreeSurface(temp);
  360.     SDL_DestroyTexture(text_score);
  361.     TTF_CloseFont(font);
  362.     // free the memory before exiting the game.
  363.  
  364.     SDL_DestroyTexture(empty_text);
  365.     SDL_DestroyTexture(snake_text);
  366.     SDL_DestroyRenderer(renderer);
  367.     SDL_DestroyWindow(window);
  368.     SDL_Quit();
  369.  
  370.     return 0;
  371. }
  372.  
  373.  
  374.  
  375. void moveSnakeParts(int snake[WIDTH*HEIGHT][2], int snakeSize) {
  376.     int i;
  377.     for(i=snakeSize-1 ; i>0 ; i--) {
  378.         snake[i][0] = snake[i-1][0];
  379.         snake[i][1] = snake[i-1][1];
  380.     }
  381.  
  382. }
  383.  
  384. int notInSnake(int snake[WIDTH*HEIGHT][2], int snakeSize, int fruit[2]) {
  385.     int i;
  386.     for(i=0 ; i<snakeSize ; i++) {
  387.         if(snake[i][0] == fruit[0] && snake[i][1] == fruit[1]) {
  388.             return 1;
  389.         }
  390.     }
  391.  
  392.     return 0;
  393. }
  394. int biteSnake(int snake[WIDTH*HEIGHT][2], int snakeSize, int position[2]) {
  395.     int i;
  396.     for(i=1 ; i<snakeSize ; i++) {
  397.         if(snake[i][0] == position[0] && snake[i][1] == position[1]) {
  398.             return 1;
  399.         }
  400.     }
  401.  
  402.     return 0;
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement