Guest User

snake

a guest
Aug 12th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_gfxPrimitives.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #define WIDTH 1280
  7. #define HEIGHT 1024
  8. #define BLOCK_SIZE 16
  9. #define BWIDTH (WIDTH/BLOCK_SIZE)
  10. #define BHEIGHT (HEIGHT/BLOCK_SIZE)
  11. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  12. #define RMASK 0xff000000
  13. #define GMASK 0x00ff0000
  14. #define BMASK 0x0000ff00
  15. #define AMASK 0x000000ff
  16. #else
  17. #define RMASK 0x000000ff
  18. #define GMASK 0x0000ff00
  19. #define BMASK 0x00ff0000
  20. #define AMASK 0xff000000
  21. #endif
  22.  
  23. SDL_Surface *screen, *background;
  24. short int foodx, foody;
  25. bool **walls;
  26.  
  27. enum directions{left, right, up, down};
  28.  
  29. struct block{
  30.     short int x;
  31.     short int y;
  32.     block *next;
  33. };
  34.  
  35. void draw_block(short int x, short int y, Uint32 color){
  36.     boxColor(screen, x*BLOCK_SIZE, y*BLOCK_SIZE, (x+1)*BLOCK_SIZE, (y+1)*BLOCK_SIZE, color);
  37. }
  38.  
  39. class Snake{
  40. private:
  41.     block *last, *tmp;
  42.     short int length, maxlength;
  43.     directions direction, lastdir;
  44.     void check_collisions(block* a){
  45.         if(a->x >= BWIDTH || a->x < 0 || a->y >=BHEIGHT || a->y <0 || walls[a->x][a->y]) dead = true;
  46.         for(tmp=last; tmp->next!=NULL; tmp=tmp->next)
  47.             if(a->x == tmp->x && a->y == tmp->y){
  48.                 dead=true;
  49.                 break;
  50.             }
  51.         if(a->x == foodx && a->y ==foody){
  52.             maxlength+=5;
  53.             foodx = rand() % BWIDTH;
  54.             foody = rand() % BHEIGHT;
  55.             food_set:
  56.             for(tmp=last; tmp!=NULL; tmp=tmp->next){
  57.                 if((foodx == tmp->x && foody == tmp->y) || walls[foodx][foody]){
  58.                     foodx = rand() % BWIDTH;
  59.                     foody = rand() % BHEIGHT;
  60.                     goto food_set;
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     void push(){
  66.         for(tmp=last; tmp->next; tmp = tmp->next);
  67.         tmp->next = new block;
  68.         switch(direction){
  69.         case right:
  70.             tmp->next->x=tmp->x+1;
  71.             tmp->next->y=tmp->y;
  72.             break;
  73.         case left:
  74.             tmp->next->x=tmp->x-1;
  75.             tmp->next->y=tmp->y;
  76.             break;
  77.         case up:
  78.             tmp->next->x=tmp->x;
  79.             tmp->next->y=tmp->y-1;
  80.             break;
  81.         case down:
  82.             tmp->next->x=tmp->x;
  83.             tmp->next->y=tmp->y+1;
  84.             break;
  85.         }
  86.         tmp->next->next=NULL;
  87.         check_collisions(tmp->next);
  88.         length++;
  89.     }
  90.     void pop(){
  91.         tmp=last;
  92.         last=last->next;
  93.         delete tmp;
  94.         length--;
  95.     }
  96. public:
  97.     bool dead;
  98.     Snake(){
  99.         dead = false;
  100.         last = new block;
  101.         last->x = BWIDTH/2;
  102.         last->y = BHEIGHT/2;
  103.         last->next = NULL;
  104.         maxlength = 6;
  105.         length = 1;
  106.         lastdir = direction = right;
  107.     }
  108.     ~Snake(){
  109.         while(last->next){
  110.             tmp=last;
  111.             last=last->next;
  112.             delete tmp;
  113.         }
  114.     }
  115.     void process_input(SDLKey sym){
  116.     switch(sym){
  117.     case SDLK_LEFT:
  118.         if(lastdir!=right)direction = left;
  119.         break;
  120.     case SDLK_RIGHT:
  121.         if(lastdir!=left)direction = right;
  122.         break;
  123.     case SDLK_UP:
  124.         if(lastdir!=down)direction = up;
  125.         break;
  126.     case SDLK_DOWN:
  127.         if(lastdir!=up)direction = down;
  128.         break;
  129.     default:
  130.         break;
  131.         }
  132.     }
  133.     void process(){
  134.         if(length>maxlength) pop();
  135.         push();
  136.         lastdir = direction;
  137.     }
  138.     void print(){
  139.         for(tmp=last; tmp!=NULL; tmp=tmp->next) draw_block(tmp->x, tmp->y, 0xFFFFFFFF);
  140.     }
  141. };
  142.  
  143. int init(){
  144.     atexit(SDL_Quit);
  145.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
  146.         printf( "Unable to init SDL: %s\n", SDL_GetError() );
  147.         return 1;
  148.     }
  149.     screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
  150.     if ( !screen ){
  151.         printf("Unable to set video mode: %s\n", SDL_GetError());
  152.         return 1;
  153.     }
  154.     background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32, RMASK, GMASK, BMASK, AMASK);
  155.     //code
  156.     srand(time(NULL));
  157.     foodx = rand() % BWIDTH;
  158.     foody = rand() % BHEIGHT;
  159.     return 0;
  160. }
  161.  
  162. void init_walls(){    
  163.     walls = new bool* [BWIDTH];
  164.     for(int i=0; i<BWIDTH; i++){
  165.         walls[i] = new bool [BHEIGHT];
  166.         for(int j=0; j<BHEIGHT; j++) walls[i][j]=false;
  167.     }
  168.     for(int i=2; i<BHEIGHT-3; i++)
  169.         walls[4][i] = walls[5][i] = walls [BWIDTH-5][i] = walls [BWIDTH-6][i] = true;
  170.     boxColor(background, 0, 0, WIDTH, HEIGHT, 0x000000FF);
  171.     for(int i=0; i<BWIDTH; i++)
  172.     for(int j=0; j<BHEIGHT; j++)
  173.         if(walls[i][j]) boxColor(background, i*BLOCK_SIZE, j*BLOCK_SIZE, (i+1)*BLOCK_SIZE, (j+1)*BLOCK_SIZE, 0x0000AAFF);
  174. }
  175.  
  176. int main ( int argc, char** argv ){
  177.     long int frame=0;
  178.     bool done;
  179.     SDL_Event event;
  180.     Snake snake;
  181.     init();
  182.     init_walls();
  183.     while (!done){
  184.         while(SDL_PollEvent(&event) )
  185.             switch(event.type){
  186.             case SDL_QUIT:
  187.                 done = true;
  188.                 break;
  189.             case SDL_KEYDOWN:
  190.                 if(event.key.keysym.sym == 'q') done = true;
  191.                 snake.process_input(event.key.keysym.sym);
  192.             default:
  193.                 break;
  194.             }
  195.         SDL_Delay(20);
  196.         if(frame%2==0){
  197.             snake.process();
  198.             if(snake.dead) done = true;
  199.             SDL_BlitSurface(background, NULL, screen, NULL);
  200.             draw_block(foodx, foody, 0xFF0000FF);
  201.             snake.print();
  202.             SDL_Flip(screen);
  203.         }
  204.         frame++;
  205.     }
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment