Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //新增功能:有計分,並且每吃3顆蘋果就會快100毫秒,最多會快到200毫秒刷新一次
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- //define the direction button.
- #define KEY_UP 72
- #define KEY_DOWN 80
- #define KEY_LEFT 75
- #define KEY_RIGHT 77
- //record the map, 1 for walls, 2 for snake, 3 for apple. 4 for blocks.
- int length;
- int wide;
- int map[1000]={0};
- int s_direction; //record the direction of the snake.
- int s_head ,s_tail; //record the coordinate of the head and the tail of the snake.
- vector<int>s_body; //record the body (every joint) of the snake.
- int apple; //record the coordinate of the apple.
- int block ;
- int score=0; //record the score you get.
- int cut_time=0; //record the shrinked time.
- int count_add=0; //record how many times the time shrink.
- /*int rmap[1000];
- void gotoxy(int xpos, int ypos)
- {
- COORD scrn;
- HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
- scrn.X = xpos; scrn.Y = ypos;
- SetConsoleCursorPosition(hOuput,scrn);
- } */
- void set_size(){
- cout<<"Please type the length of the map that you want to play: "<<'\n';
- cin>>length;
- cout<<"Please type the wide of the map that you want to play: "<<'\n';
- cin>>wide;
- return;
- }
- void setpoint(int l , int w){
- for( int s = 1 ; s <= l*w ; s++ ){
- //cerr << s << endl;
- if(map[s] == 0 && map[s+1] == 0 && map[s+2] == 0
- && map[s-1] == 0 && map[s+3] == 0
- && map[s-l] == 0 && map[s+l] == 0
- && map[s-l+1] == 0 && map[s+l+1] == 0
- && map[s-l+2] == 0 && map[s+l+2] == 0 ){
- //set up the initial position of the snake.
- map[s]=2;
- map[s+1]=2;
- map[s+2]=2;
- //set up the coordinate of the head.
- s_body.push_back(s);
- s_body.push_back(s+1);
- s_body.push_back(s+2);
- s_head=s+2;
- //cerr << "snake positioned at " << s << endl;
- return;
- }
- }
- cout<<"cannot play,please change the size of the map!";
- }
- //draw maps with □■●.
- void draw_map(int l , int w){
- system ( "cls" ); //cls means clear screen.
- cout<<"Scores: "<<score<<'\n';
- for( int d = 0 ; d < l*w ; d++ ){
- /*if (map[d] == rmap[d]) continue;
- rmap[d] = map[d];
- gotoxy(d%l, d/l+1);*/
- if( map[d] == 0 ){
- cout << " ";
- }
- else if( map[d] == 3){
- cout << "●";
- }
- else if( map[d] == 4 ){
- cout << "□";
- }
- else{
- cout << "■";
- }
- if( d % l == l-1 ){
- cout << '\n';
- }
- }
- }
- //initialize the map.
- void init_map( int l , int w ){
- /*for( int i = 0 ; i <= l*w-1 ; i++ ){
- rmap[i] = -1;
- } */
- // build up the walls.
- for( int i = 0 ; i <= l-1 ; i++ ){ //up
- map[i] = 1;
- }
- for(int i = 0 ; i <= l*(w-1) ; i+=l ){ //left
- map[i] = 1;
- }
- for(int i = l-1 ; i <= l*w-1 ; i+=l ){ //right
- map[i] = 1;
- }
- for(int i = l*(w-1) ; i <= l*w-1 ; i++ ){ //down
- map[i] = 1;
- }
- //set up the direction of the snake.
- s_direction = 1;
- // draw_map(l,w); //draw the initial map.
- }
- void new_apple(int l,int w){
- int x;
- while(1){
- x=rand()%(l*w); //choose a random square to put in a apple.
- if( map[x] == 0 ){ //the apple can only be put in the empty aquare.
- break;
- }
- }
- map[x] = 3; //set up the coordinate of the apple.
- if(count_add%3==0 && count_add!=0){
- if(cut_time>300){ //check if time reachs the limit of shrinked time.
- if(cut_time==450){
- return ;
- }
- cut_time += 50;
- }
- cut_time += 100; //if not, add more 100 minisecond in shrinked time.
- }
- }
- void new_block(int l , int w ){
- int y;
- while(1){
- y = rand() % ( l * w );
- if( map[y] == 0 && y != ( s_head + s_direction )){
- break;
- }
- }
- map[y]=4;
- }
- int main(void){
- srand( (int)time(NULL) ); //reset the random funtion in new_apple().
- int k; //record what button you press.
- set_size();
- init_map(length,wide);
- setpoint(length,wide);
- new_apple(length,wide);
- draw_map(length,wide);
- do{ //Let the snake move:create a new head, and cut the tail.
- while(!kbhit()){ //while we don't press the botton.
- s_head = s_head + s_direction; // create a new head.
- //check if the snakes hits the wall.
- if(map[s_head]==1){
- cout<<"hit wall, game over";
- return 0;
- }
- /*vector<int>::iterator it = find(s_body.begin(),s_body.end(),s_head+s_direction);
- if(it !=s_body.end()){
- cout<<"bite self, game over";
- return 0;
- }*/
- //check if the snake bite itself.
- if(map[s_head+s_direction] == 2){
- cout<<"bite self, game over";
- return 0;
- }
- if(map[s_head+s_direction ] ==4){
- cout<<"hit a block, game over";
- return 0;
- }
- //chenk if the snake eats the apple.
- if(map[s_head]==3){
- new_apple( length , wide ); //if it does, put a new apple in the map.
- if(length*wide>=400 || score>=5){
- new_block( length , wide );
- new_block( length , wide );
- }
- else{
- new_block( length , wide );
- }
- score++; //get one point.
- map[s_head]=2; //change the coordinate of the apple.
- }
- else{
- s_tail = s_body.front(); //take out the tail from the vector.
- s_body.erase ( s_body.begin () ); //cut the tail.
- map[s_tail] = 0; //format the square of the old tail.
- }
- s_body.push_back( s_head ); // push the coordinate of the new head into the vector.
- map[s_head] = 2; //format the square of the new head.
- draw_map(length,wide) ; //redraw the map.
- Sleep( 200 - cut_time ); //stop for half a second to let our eyes be more comfortable.
- }
- k=_getch();
- //cout<<k , just for testing;
- if(k==224){ //that means we press the direction button.
- k=_getch(); //check which direction button we press.
- if(k==KEY_UP){
- s_direction=-length;
- }
- else if(k==KEY_DOWN){
- s_direction=length;
- }
- else if(k==KEY_LEFT){
- s_direction=-1;
- }
- else if(k==KEY_RIGHT){
- s_direction=1;
- }
- }
- } while( 1 ); //we can press "esc" button to stop this loop.
- return 0;
- }
Add Comment
Please, Sign In to add comment