thecplusplusguy

Snake game (ncurses version) - snake.cpp

Jul 1st, 2011
5,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include "snake.h"
  2. using namespace std;
  3. snakepart::snakepart(int col,int row)
  4. {
  5.     x=col;
  6.     y=row;
  7. }
  8.  
  9. snakepart::snakepart()
  10. {
  11.     x=0;
  12.     y=0;
  13. }
  14. snakeclass::snakeclass()
  15. {
  16.     initscr();
  17.     nodelay(stdscr,true);           //if there wasn't any key pressed don't wait for keypress
  18.     keypad(stdscr,true);            //init the keyboard
  19.     noecho();                                   //don't write
  20.     curs_set(0);                            //cursor invisible
  21.     getmaxyx(stdscr,maxheight,maxwidth);
  22.     partchar='x';
  23.     oldalchar=(char)219;
  24.     etel='*';
  25.     food.x=0;
  26.     food.y=0;
  27.     for(int i=0;i<5;i++)
  28.         snake.push_back(snakepart(40+i,10));
  29.     points=0;
  30.     del=110000;
  31.     get=0;
  32.     direction='l';
  33.     srand(time(NULL));
  34.     putfood();
  35.     //make the game-board -- up-vertical
  36.     for(int i=0;i<maxwidth-1;i++)
  37.     {
  38.         move(0,i);
  39.         addch(oldalchar);
  40.     }
  41.     //left-horizontal
  42.     for(int i=0;i<maxheight-1;i++)
  43.     {
  44.         move(i,0);
  45.         addch(oldalchar);
  46.     }
  47.     //down-vertical
  48.     for(int i=0;i<maxwidth-1;i++)
  49.     {
  50.         move(maxheight-2,i);
  51.         addch(oldalchar);
  52.     }
  53.     //right-horizontal
  54.     for(int i=0;i<maxheight-1;i++)
  55.     {
  56.         move(i,maxwidth-2);
  57.         addch(oldalchar);
  58.     }
  59.     //draw the snake
  60.     for(int i=0;i<snake.size();i++)
  61.     {
  62.         move(snake[i].y,snake[i].x);
  63.         addch(partchar);
  64.     }
  65.     move(maxheight-1,0);
  66.     printw("%d",points);
  67.     move(food.y,food.x);
  68.     addch(etel);
  69.     refresh();
  70. }
  71.  
  72. snakeclass::~snakeclass()
  73. {
  74.     nodelay(stdscr,false);          //turn back
  75.     getch();                                        //wait until a key is pressed
  76.     endwin();
  77. }
  78.  
  79. void snakeclass::putfood()
  80. {
  81.     while(1)
  82.     {
  83.         int tmpx=rand()%maxwidth+1;
  84.         int tmpy=rand()%maxheight+1;
  85.         for(int i=0;i<snake.size();i++)
  86.             if(snake[i].x==tmpx && snake[i].y==tmpy)
  87.                 continue;
  88.         if(tmpx>=maxwidth-2 || tmpy>=maxheight-3)
  89.             continue;
  90.         food.x=tmpx;
  91.         food.y=tmpy;
  92.         break;
  93.     }
  94.     move(food.y,food.x);
  95.     addch(etel);
  96.     refresh();
  97. }
  98.  
  99. bool snakeclass::collision()
  100. {
  101.     if(snake[0].x==0 || snake[0].x==maxwidth-1 || snake[0].y==0 || snake[0].y==maxheight-2)
  102.         return true;
  103.     for(int i=2;i<snake.size();i++)
  104.     {
  105.         if(snake[0].x==snake[i].x && snake[0].y==snake[i].y)
  106.             return true;
  107.     }
  108.     //collision with the food
  109.     if(snake[0].x==food.x && snake[0].y==food.y)
  110.     {
  111.         get=true;
  112.         putfood();
  113.         points+=10;
  114.         move(maxheight-1,0);
  115.         printw("%d",points);
  116.         if((points%100)==0)
  117.             del-=10000;
  118.     }else
  119.         get=false;
  120.     return false;
  121. }
  122.  
  123.  
  124. void snakeclass::movesnake()
  125. {
  126.     //detect key
  127.     int tmp=getch();
  128.     switch(tmp)
  129.     {
  130.         case KEY_LEFT:
  131.             if(direction!='r')
  132.                 direction='l';
  133.             break;
  134.         case KEY_UP:
  135.             if(direction!='d')
  136.                 direction='u';
  137.             break;
  138.         case KEY_DOWN:
  139.             if(direction!='u')
  140.                 direction='d';
  141.             break;
  142.         case KEY_RIGHT:
  143.             if(direction!='l')
  144.                 direction='r';
  145.             break;
  146.         case KEY_BACKSPACE:
  147.             direction='q';
  148.             break;
  149.     }
  150.     //if there wasn't a collision with food
  151.     if(!get)
  152.     {
  153.         move(snake[snake.size()-1].y,snake[snake.size()-1].x);
  154.         printw(" ");
  155.         refresh();
  156.         snake.pop_back();
  157.     }
  158.     if(direction=='l')
  159.     {
  160.         snake.insert(snake.begin(),snakepart(snake[0].x-1,snake[0].y));
  161.     }else if(direction=='r'){
  162.         snake.insert(snake.begin(),snakepart(snake[0].x+1,snake[0].y));
  163.  
  164.     }else if(direction=='u'){
  165.         snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y-1));
  166.     }else if(direction=='d'){
  167.         snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y+1));
  168.     }
  169.         move(snake[0].y,snake[0].x);
  170.         addch(partchar);
  171.     refresh();
  172. }
  173.  
  174. void snakeclass::start()
  175. {
  176.     while(1)
  177.     {
  178.         if(collision())
  179.         {
  180.             move(12,36);
  181.             printw("game_over");
  182.             break;
  183.         }
  184.         movesnake();
  185.         if(direction=='q')              //exit
  186.             break;
  187.         usleep(del);            //Linux delay
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment