Tony041010

Snake

Jun 10th, 2021 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. //新增功能:有計分,並且每吃3顆蘋果就會快100毫秒,最多會快到200毫秒刷新一次
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <time.h>
  7. #include <windows.h>
  8. using namespace std;
  9.  
  10. //define the direction button.
  11. #define KEY_UP 72
  12. #define KEY_DOWN 80
  13. #define KEY_LEFT 75
  14. #define KEY_RIGHT 77
  15.  
  16. //record the map, 1 for walls, 2 for snake, 3 for apple. 4 for blocks.
  17. int length;
  18. int wide;
  19. int map[1000]={0};
  20. int s_direction;  //record the direction of the snake.
  21. int s_head ,s_tail; //record the coordinate of the head and the tail of the snake.
  22. vector<int>s_body; //record the body (every joint) of the snake.
  23. int apple; //record the coordinate of the apple.
  24. int block ;
  25. int score=0; //record the score you get.
  26. int cut_time=0; //record the shrinked time.
  27. int count_add=0; //record how many times the time shrink.
  28.  
  29.  
  30. /*int rmap[1000];
  31. void gotoxy(int xpos, int ypos)
  32. {
  33.   COORD scrn;
  34.   HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  35.   scrn.X = xpos; scrn.Y = ypos;
  36.   SetConsoleCursorPosition(hOuput,scrn);
  37. } */
  38.  
  39. void set_size(){
  40.     cout<<"Please type the length of the map that you want to play: "<<'\n';
  41.     cin>>length;
  42.     cout<<"Please type the wide of the map that you want to play: "<<'\n';
  43.     cin>>wide;
  44.     return;
  45. }
  46.  
  47. void setpoint(int l , int w){
  48.     for( int s = 1 ; s <= l*w ; s++ ){
  49.         //cerr << s << endl;
  50.         if(map[s] == 0 && map[s+1] == 0 && map[s+2] == 0
  51.         && map[s-1] == 0 && map[s+3] == 0
  52.         && map[s-l] == 0 && map[s+l] == 0
  53.         && map[s-l+1] == 0 && map[s+l+1] == 0
  54.         && map[s-l+2] == 0 && map[s+l+2] == 0 ){
  55.            
  56.             //set up the initial position of the snake.
  57.             map[s]=2;
  58.             map[s+1]=2;
  59.             map[s+2]=2;
  60.            
  61.             //set up the coordinate of the head.
  62.             s_body.push_back(s);
  63.             s_body.push_back(s+1);
  64.             s_body.push_back(s+2);
  65.             s_head=s+2;
  66.             //cerr << "snake positioned at " << s << endl;
  67.             return;
  68.         }
  69.     }
  70.     cout<<"cannot play,please change the size of the map!";
  71. }
  72. //draw maps with □■●.
  73. void draw_map(int l , int w){
  74.     system ( "cls" ); //cls means clear screen.
  75.     cout<<"Scores: "<<score<<'\n';
  76.     for( int d = 0 ; d < l*w ; d++ ){
  77.  
  78.         /*if (map[d] == rmap[d]) continue;
  79.         rmap[d] = map[d];
  80.         gotoxy(d%l, d/l+1);*/
  81.         if( map[d] == 0 ){
  82.             cout << "  ";
  83.         }
  84.         else if( map[d] == 3){
  85.             cout << "●";
  86.         }
  87.         else if( map[d] == 4 ){
  88.             cout << "□";
  89.         }
  90.        
  91.         else{
  92.             cout << "■";
  93.         }
  94.         if( d % l == l-1 ){
  95.             cout << '\n';
  96.         }
  97.     }
  98. }
  99. //initialize the map.
  100. void init_map( int l , int w ){
  101.     /*for( int i = 0 ; i <= l*w-1 ; i++ ){
  102.         rmap[i] = -1;
  103.     } */
  104.    
  105.     // build up the walls.
  106.     for( int i = 0 ; i <= l-1 ; i++ ){  //up
  107.         map[i] = 1;
  108.     }
  109.     for(int i = 0 ; i <= l*(w-1) ; i+=l ){ //left
  110.         map[i] = 1;
  111.     }
  112.     for(int i = l-1 ; i <= l*w-1 ; i+=l ){ //right
  113.         map[i] = 1;
  114.     }
  115.     for(int i = l*(w-1) ; i <= l*w-1 ; i++ ){ //down
  116.         map[i] = 1;
  117.     }
  118.    
  119.     //set up the direction of the snake.
  120.     s_direction = 1;
  121.    
  122. //  draw_map(l,w); //draw the initial map.
  123. }
  124.  
  125. void new_apple(int l,int w){
  126.     int x;
  127.     while(1){
  128.         x=rand()%(l*w); //choose a random square to put in a apple.
  129.         if( map[x] == 0 ){ //the apple can only be put in the empty aquare.
  130.             break;
  131.         }
  132.     }
  133.     map[x] = 3; //set up the coordinate of the apple.
  134.    
  135.     if(count_add%3==0 && count_add!=0){
  136.         if(cut_time>300){  //check if time reachs the limit of shrinked time.
  137.             if(cut_time==450){
  138.                 return ;
  139.             }
  140.             cut_time += 50;
  141.         }
  142.         cut_time += 100; //if not, add more 100 minisecond in shrinked time.
  143.     }
  144. }
  145.  
  146. void new_block(int l , int w ){
  147.     int y;
  148.     while(1){
  149.         y = rand() % ( l * w );
  150.         if( map[y] == 0  && y !=  ( s_head + s_direction )){
  151.             break;
  152.         }
  153.     }
  154.     map[y]=4;
  155. }
  156.  
  157.  
  158. int main(void){
  159.     srand( (int)time(NULL) ); //reset the random funtion in new_apple().
  160.     int k; //record what button  you press.
  161.     set_size();
  162.     init_map(length,wide);
  163.     setpoint(length,wide);
  164.     new_apple(length,wide);
  165.     draw_map(length,wide);
  166.    
  167.     do{  //Let the snake move:create a new head, and cut the tail.
  168.    
  169.         while(!kbhit()){ //while we don't press the botton.
  170.             s_head = s_head + s_direction; // create a new head.
  171.            
  172.             //check if the snakes hits the wall.
  173.             if(map[s_head]==1){
  174.                 cout<<"hit wall, game over";
  175.                 return 0;
  176.             }
  177.            
  178.            
  179.             /*vector<int>::iterator it = find(s_body.begin(),s_body.end(),s_head+s_direction);
  180.             if(it !=s_body.end()){
  181.                 cout<<"bite self, game over";
  182.                 return 0;
  183.             }*/
  184.            
  185.             //check if the snake bite itself.
  186.             if(map[s_head+s_direction] == 2){
  187.                 cout<<"bite self, game over";
  188.                 return 0;
  189.             }
  190.            
  191.             if(map[s_head+s_direction ] ==4){
  192.                 cout<<"hit a block, game over";
  193.                 return 0;
  194.             }
  195.                
  196.             //chenk if the snake eats the apple.
  197.             if(map[s_head]==3){
  198.                 new_apple( length , wide ); //if it does, put a new apple in the map.
  199.                 if(length*wide>=400 || score>=5){
  200.                     new_block( length , wide );
  201.                     new_block( length , wide );
  202.                 }
  203.                 else{
  204.                     new_block( length , wide );
  205.                 }
  206.                 score++; //get one point.
  207.                 map[s_head]=2; //change the coordinate of the apple.
  208.             }
  209.             else{
  210.                 s_tail = s_body.front(); //take out the tail from the vector.
  211.            
  212.                 s_body.erase ( s_body.begin () ); //cut the tail.
  213.            
  214.                 map[s_tail] = 0;  //format the square of the old tail.
  215.             }
  216.            
  217.             s_body.push_back( s_head ); // push the coordinate of the new head into the vector.
  218.            
  219.             map[s_head] = 2; //format the square of the new head.
  220.            
  221.             draw_map(length,wide) ; //redraw the map.
  222.            
  223.             Sleep( 200 - cut_time ); //stop for half a second to let our eyes be more comfortable.
  224.         }
  225.        
  226.         k=_getch();
  227.         //cout<<k , just for testing;
  228.        
  229.         if(k==224){ //that means we press the direction button.
  230.             k=_getch(); //check which direction button we press.
  231.             if(k==KEY_UP){
  232.                 s_direction=-length;
  233.             }
  234.             else if(k==KEY_DOWN){
  235.                 s_direction=length;
  236.             }
  237.             else if(k==KEY_LEFT){
  238.                 s_direction=-1;
  239.             }
  240.             else if(k==KEY_RIGHT){
  241.                 s_direction=1;
  242.             }
  243.         }
  244.        
  245.     } while( 1 ); //we can press "esc" button to stop this loop.
  246.    
  247.     return 0;
  248. }
Add Comment
Please, Sign In to add comment