Abdulg

Simple Snake [SDL]

Sep 8th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include "SDL/SDL_ttf.h"
  3. #include "stdlib.h"
  4.  
  5. #define SCREENWIDTH 640
  6.  
  7. SDL_Surface *Screen = NULL;
  8. SDL_Surface *Head = NULL;
  9. SDL_Surface *Segment = NULL;
  10. SDL_Surface *Food = NULL;
  11.  
  12. SDL_Color White = {255, 255, 255};
  13.  
  14. TTF_Font *Font = NULL;
  15.  
  16. SDL_Event Event;
  17.  
  18. short unsigned int SegmentArray[3200];
  19. int Segments = 0;
  20.  
  21. int Initiate();
  22. int Inbetween(int Min, int Value, int Max);
  23. int CheckSnakeCollision(int X, int Y);
  24. void RenderToWindow(int X, int Y, SDL_Surface *Char);
  25.  
  26. int main(int argc, char * argv[])
  27. {
  28.     if (Initiate() != 1) return 1;
  29.    
  30.     int XDir = 1;
  31.     int YDir = 0;
  32.     int HeadX = 20;
  33.     int HeadY = 20;
  34.     int FoodX = 10;
  35.     int FoodY = 10;
  36.     int SnakeIncrementer = 0;
  37.  
  38.     SDL_Rect Rect;
  39.     Rect.x = 0;
  40.     Rect.y = 0;
  41.     Rect.w = SCREENWIDTH;
  42.     Rect.h = SCREENWIDTH;
  43.  
  44.     while (!CheckSnakeCollision(HeadX, HeadY) && Inbetween(0, HeadX, 39) && Inbetween(0, HeadY, 39))
  45.     {
  46.         HeadX += XDir;
  47.         HeadY += YDir;
  48.  
  49.         if (HeadX == FoodX && HeadY == FoodY)
  50.         {
  51.             SegmentArray[Segments * 2] = HeadX - XDir;
  52.             SegmentArray[Segments * 2 + 1] = HeadY - YDir;
  53.             Segments++;
  54.  
  55.             do
  56.             {
  57.                 FoodX = rand() % 39 + 1;
  58.                 FoodY = rand() % 39 + 1;
  59.             } while(CheckSnakeCollision(FoodX, FoodY));
  60.         }
  61.  
  62.         if (Segments != 0)
  63.         {
  64.             SegmentArray[SnakeIncrementer] = HeadX - XDir;
  65.             SegmentArray[SnakeIncrementer + 1] = HeadY - YDir;
  66.  
  67.             SnakeIncrementer += 2;
  68.             if (SnakeIncrementer == Segments * 2) SnakeIncrementer = 0;
  69.         }
  70.        
  71.         while(SDL_PollEvent(&Event))
  72.         {
  73.             if (Event.type == SDL_KEYDOWN)
  74.             {
  75.                 if (Event.key.keysym.sym == SDLK_LEFT)
  76.                 {
  77.                     YDir = 0;
  78.                     XDir = -1;
  79.                 }
  80.                 else if (Event.key.keysym.sym == SDLK_RIGHT)
  81.                 {
  82.                     YDir = 0;
  83.                     XDir = 1;
  84.                 }
  85.                
  86.                 if (Event.key.keysym.sym == SDLK_UP)
  87.                 {
  88.                     XDir = 0;
  89.                     YDir = -1;
  90.                 }
  91.                 else if (Event.key.keysym.sym == SDLK_DOWN)
  92.                 {
  93.                     XDir = 0;
  94.                     YDir = 1;
  95.                 }
  96.             }
  97.  
  98.             else if (Event.type == SDL_QUIT) HeadX = 100;
  99.         }
  100.  
  101.         SDL_FillRect(Screen, &Rect, 0x000000);
  102.  
  103.         RenderToWindow(HeadX, HeadY, Head);
  104.         RenderToWindow(FoodX, FoodY, Food);
  105.  
  106.         int i;
  107.         for (i = 0; i < Segments * 2; i += 2)
  108.             RenderToWindow(SegmentArray[i], SegmentArray[i + 1], Segment);
  109.        
  110.         SDL_Flip(Screen);
  111.         SDL_Delay(100);
  112.     }
  113.    
  114.     printf("Score: %d\n", Segments);
  115.  
  116.     return 0;
  117. }
  118.  
  119. int Inbetween(int Min, int Value, int Max)
  120. {
  121.     return (Value > Min && Value < Max);
  122. }
  123.  
  124. int Initiate()
  125. {
  126.     if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
  127.     {
  128.         printf("Couldn't initialise SDL: SDL_GetError() returns %s\n", SDL_GetError());
  129.         return 0;
  130.     }
  131.  
  132.     Screen = SDL_SetVideoMode(SCREENWIDTH, SCREENWIDTH, 32, SDL_SWSURFACE);
  133.     if (Screen == NULL) return 0;
  134.  
  135.     if (TTF_Init() == -1) return 0;
  136.  
  137.     SDL_WM_SetCaption("Snake", NULL);
  138.    
  139.     Font = TTF_OpenFont("DigitalDream.ttf", 16);
  140.     if (Font == NULL) return 0;
  141.  
  142.     Head = TTF_RenderText_Solid(Font, "H", White);
  143.     Segment = TTF_RenderText_Solid(Font, "S", White);
  144.     Food = TTF_RenderText_Solid(Font, "F", White);
  145.    
  146.     return 1;
  147. }
  148.  
  149. int CheckSnakeCollision(int X, int Y)
  150. {
  151.     int i;
  152.     for (i = 0; i < Segments * 2; i += 2)
  153.         if (X == SegmentArray[i] && Y == SegmentArray[i + 1]) return 1;
  154.  
  155.     return 0;
  156. }
  157.  
  158. void RenderToWindow(int X, int Y, SDL_Surface *Char)
  159. {
  160.     SDL_Rect Rect;
  161.     Rect.x = X * 16;
  162.     Rect.y = Y * 16;
  163.     Rect.w = Char->w;
  164.     Rect.h = Char->h;
  165.    
  166.     SDL_BlitSurface(Char, NULL, Screen, &Rect);
  167. }
  168.  
  169. /* Compiled using GCC
  170. abdul@computer [~/Programming/Snake]:
  171. make
  172. gcc -I /usr/include/SDL -o Snake main.c -Wall -g -lSDL -lSDL_ttf
  173. abdul@computer [~/Programming/Snake]:
  174. ./Snake
  175. Score: 8
  176. */
Advertisement
Add Comment
Please, Sign In to add comment