thecplusplusguy

Snake game GUI (SDL) - snake.cpp

Jul 1st, 2011
1,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include "snake.h"
  2.  
  3. /*
  4. TODO list
  5. -make a menu
  6. -toplist
  7. -colors
  8. */
  9.  
  10.  
  11. void snakeclass::drawRect(int x,int y,Uint32 color,int w,int h)
  12. {
  13.     SDL_Rect tmp = {x,y,w,h};
  14.     SDL_FillRect(screen,&tmp,color);
  15. }
  16.  
  17.  
  18. snakepart::snakepart(int col,int row)
  19. {
  20.     x=col;
  21.     y=row;
  22. }
  23.  
  24. snakepart::snakepart()
  25. {
  26.     x=0;
  27.     y=0;
  28. }
  29.  
  30. snakeclass::snakeclass()
  31. {
  32.     width=800;
  33.     height=400;
  34.     SDL_Init(SDL_INIT_EVERYTHING);
  35.     TTF_Init();
  36.     font=TTF_OpenFont("air.ttf",20);
  37.     screen=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
  38.  
  39.     for(int i=0;i<5;i++)
  40.         snake.push_back(snakepart(40+i,10));
  41.     points=0;
  42.     del=110;
  43.     get=false;
  44.     direction='l';
  45.     srand(time(0));
  46.     putfood();
  47.     //put the edges
  48.     drawRect(0,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),width,10);
  49.     drawRect(0,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),10,height-20);
  50.     drawRect(0,height-30,SDL_MapRGB(screen->format,0xff,0x00,0x00),width,10);
  51.     drawRect(width-10,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),10,height-20);
  52.     //draw the snake*/
  53.     for(int i=0;i<snake.size();i++)
  54.     {
  55.             drawRect(snake[i].x*10,snake[i].y*10,SDL_MapRGB(screen->format,0x00,0xff,0x00));
  56.     }
  57. }
  58. snakeclass::~snakeclass()
  59. {
  60.     TTF_CloseFont(font);
  61.     TTF_Quit();
  62.     SDL_Quit();
  63. }
  64.  
  65. void snakeclass::putfood()
  66. {
  67.     while(1)
  68.     {
  69.         int tmpx=rand()%width/10+1;
  70.         int tmpy=rand()%height/10+1;
  71.         for(int i=0;i<snake.size();i++)
  72.             if(snake[i].x==tmpx && snake[i].y==tmpy)
  73.                 continue;
  74.         if(tmpx>=width/10-2 || tmpy>=height/10-3)
  75.             continue;
  76.         food.x=tmpx;
  77.         food.y=tmpy;
  78.         break;
  79.     }
  80.     drawRect(food.x*10,food.y*10,SDL_MapRGB(screen->format,0xff,0x00,0xff));
  81. }
  82.  
  83. bool snakeclass::collision()
  84. {
  85.     if(snake[0].x==0 || snake[0].x==width/10-1 || snake[0].y==0 || snake[0].y==height/10-3)
  86.         return 1;
  87.    
  88.     for(int i=2;i<snake.size();i++)
  89.         if(snake[0].x==snake[i].x && snake[i].y==snake[0].y)
  90.             return true;
  91.     if(snake[0].x==food.x && snake[0].y==food.y)
  92.     {
  93.         get=true;
  94.         putfood();
  95.         points+=10;
  96.         char c[5];
  97.         sprintf(c,"%d",points);
  98.         SDL_Surface* text;
  99.         SDL_Color color = {255,255,255};
  100.         text=TTF_RenderText_Solid(font,c,color);
  101.         SDL_Rect tmp = {0,380};
  102.         drawRect(0,380,SDL_MapRGB(screen->format,0x00,0x00,0x00),100,20);
  103.         SDL_BlitSurface(text,NULL,screen,&tmp);
  104.         SDL_FreeSurface(text);
  105.         if((points%100)==0 && del>0)
  106.             del-=10;
  107.     }else
  108.         get=false;
  109.     return false;
  110. }
  111.  
  112. void snakeclass::movesnake()
  113. {
  114.     SDL_Event event;
  115.     while(SDL_PollEvent(&event))
  116.     {
  117.         if(event.type==SDL_KEYDOWN)
  118.         {
  119.             switch(event.key.keysym.sym)
  120.             {
  121.                     case SDLK_LEFT:
  122.                         if(direction!='r')
  123.                             direction='l';
  124.                         break;
  125.                     case SDLK_UP:
  126.                         if(direction='d')
  127.                             direction='u';
  128.                         break;
  129.                     case SDLK_DOWN:
  130.                         if(direction!='u')
  131.                             direction='d';
  132.                         break;
  133.                     case SDLK_RIGHT:
  134.                         if(direction!='l')
  135.                             direction='r';
  136.                         break;
  137.                     case SDLK_ESCAPE:
  138.                         direction='q';
  139.                         break;
  140.             }
  141.         }else if(event.type==SDL_QUIT)
  142.             direction='q';
  143.     }
  144.     if(!get)
  145.     {
  146.         drawRect(snake[snake.size()-1].x*10,snake[snake.size()-1].y*10,SDL_MapRGB(screen->format,0x00,0x00,0x00));
  147.         snake.pop_back();
  148.     }
  149.     if(direction=='l')
  150.         snake.insert(snake.begin(),snakepart(snake[0].x-1,snake[0].y));
  151.     else if(direction=='r')
  152.         snake.insert(snake.begin(),snakepart(snake[0].x+1,snake[0].y));
  153.     else if(direction=='u')
  154.         snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y-1));
  155.     else if(direction=='d')
  156.         snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y+1));
  157.     drawRect(snake[0].x*10,snake[0].y*10,SDL_MapRGB(screen->format,0x00,0xff,0x00));
  158. }
  159.  
  160.  
  161. void snakeclass::start()
  162. {
  163.     while(1)
  164.     {
  165.         if(collision())
  166.         {
  167.             std::cout << "game over" << std::endl;
  168.             break;
  169.         }
  170.         movesnake();
  171.         if(direction=='q')
  172.             break;
  173.         SDL_Flip(screen);
  174.         SDL_Delay(del);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment