- 最美丽老师
 - @ 2018-01-21 21:48:43
 
#include <iostream>
#include<windows.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include <cstdlib>
#include <cstdio>
using namespace std;  
/**定位光标*/
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x,int y){
    coord.X=y;
    coord.Y=x;
    SetConsoleCursorPosition(hout,coord);
}  
/**隐藏光标*/
void hide(){
    CONSOLE_CURSOR_INFO cursor_info = {1,0};
    SetConsoleCursorInfo(hout,&cursor_info);
}  
/**生成随机数*/
double random(double start,double end){
    return start+(end-start)*rand()/(RAND_MAX+1.0);  
}
/**地图的长宽,蛇的坐标,长度,方向,食物的位置*/
int m,n;
struct node{
    int x;
    int y;
}snake[1000];
int snake_length,dir;
node food;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};  
/**输出墙*/
void print_wall(int n,int m){
    cout<<" ";
    for(int i=0;i<n;i++){
        cout<<"-";
    }
    cout<<" "<<endl;
    for(int j=0;j<m;j++){
        cout<<"|";
        for(int k=0;k<n;k++){
            cout<<" ";
        }
        cout<<"|"<<endl;
    }
    cout<<" ";
    for(int i=0;i<n;i++){
        cout<<"-";
    }
}  
/**首次输出蛇*/
void print_snake(){
    locate(snake[0].x,snake[0].y);
    cout<<"@";
    for(int i=1;i<=snake_length-1;i++){
            locate(snake[i].x,snake[i].y);
        cout<<"*";
    }
}  
/**判断自撞以及撞墙*/
bool is_correct(){
    if(snake[0].x==0||snake[0].y==0||snake[0].x==21||snake[0].y==61){
        return false;
    }
    for(int i=1;i<=snake_length-1;i++){
        if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y){
            return false;
        }
    }
    return true;
}  
/**随机生成并输出食物*/
bool print_food(){
    srand((unsigned)time(0));
    bool e;
    while(1){
            e=true;
        int i=(int)random(0,20)+1,j=(int)random(0,60)+1;
        food.x=i;food.y=j;
        for(int k=0;k<=snake_length-1;k++){
            if(food.x==snake[k].x&&food.y==snake[k].y){
                e= false;
                break;
            }
        }
        if(e){
            break;
        }
    }
    locate(food.x,food.y);
    cout<<"$";
    return true;
}  
void print_smileface(){
    locate(8,64);
     cout<<"  ********  "<<endl;
     locate(9,64);
    cout<<"*          "<<endl;
    locate(10,64);
    cout<<"  ^    ^  "<<endl;
    locate(11,64);
    cout<<"          "<<endl;
    locate(12,64);
    cout<<"    ˇ    *"<<endl;
    locate(13,64);
    cout<<"  ********  "<<endl;
    locate(14,66);
    cout<<"吃到了~"<<endl;
}
void print_face(){
  locate(8,64);
     cout<<"  ********  "<<endl;
     locate(9,64);
    cout<<"*          "<<endl;
    locate(10,64);
    cout<<"  *    *  "<<endl;
    locate(11,64);
    cout<<"          "<<endl;
    locate(12,64);
    cout<<"    *     *"<<endl;
    locate(13,64);
    cout<<"  ********  "<<endl;
    locate(14,66);  
cout<<"小心哦~"<<endl;  
}  
/**前进*/  
bool go_ahead(){  
    node temp;  
    temp = snake[snake_length-1];  
    bool e = false;  
    for(int i=snake_length-1;i>=1;i--){
        snake[i]=snake[i-1];
    }
    snake[0].x=snake[0].x+direct[dir][0];
    snake[0].y=snake[0].y+direct[dir][1];
    locate(snake[1].x,snake[1].y);
    cout<<"*";
    /*如果吃到食物了*/
    if(snake[0].x==food.x&&snake[0].y==food.y){
        snake_length++;
        e = true;
        snake[snake_length-1]=temp;
    }
    /*输出此时蛇的状态*/
    if(!e){
        locate(temp.x,temp.y);
        cout<<" ";
        print_face();
    }
    else{
        print_food();
        print_smileface();
    }
    locate(snake[0].x,snake[0].y);
    cout<<"@";
    /*如果自撞或者撞墙了*/
    if(!is_correct()){
        system("cls");
        cout<<"你输了"<<endl;
        cout<<"最终长度是:"<<snake_length;
        return false;
    }
    return true;
}
void print_logo(){
    cout<<"**********   **       **        *        **           **********"<<endl;
    cout<<"**********   ***      **       ***       **      **   **********"<<endl;
    cout<<"**           ****     **      ** **      **    **     **        "<<endl;
    cout<<"**           ** **    **     **   **     **  **       **        "<<endl;
    cout<<"**********   **  **   **    **     **    ****         **********"<<endl;
    cout<<"**********   **   **  **    *********    ***          **********"<<endl;
    cout<<"        **   **    ** **    **     **    ** **        **        "<<endl;
    cout<<"        **   **     ****    **     **    **   **      **        "<<endl;
    cout<<"**********   **      ***   **       **   **     **    **********"<<endl;
    cout<<"**********   **       **  **         **  **       **  **********"<<endl;
    for(int i=1;i<65;i++){
        cout<<"-";
    }
cout<<endl;
    cout<<" * ** * "<<endl;
    cout<<"* .  . "<<endl;
    cout<<"      "<<endl;
    cout<<"   -  *"<<endl;
    cout<<" *    *     * *"<<endl;
    cout<<"   * *    * * * *        *"<<endl;
    cout<<"    * * * *    * * * * ** "<<endl;
    cout<<"     * * *       * * * *"<<endl;
    cout<<endl;
    locate(12,30);
    cout<<"游戏规则:按上下左右键控制蛇的移动"<<endl;
    locate(13,40);
    cout<<"每吃掉一个食物身体长长一节"<<endl;
    locate(14,40);
    cout<<"撞到墙或者自己的身体则游戏失败"<<endl;
    locate(15,40);
    cout<<"我们的目标是变大变长!"<<endl;
    locate(16,40);
    cout<<"请输入难度(1~100)开始游戏吧!"<<endl;
    cout<<endl;
}  
/**主函数**/
int main(){
    print_logo();
    int hard;
    locate(17,40);
    cin>>hard;
    if(hard<1||hard>100){
        cout<<"游戏难度不得小于1不得大于100,请重新开始游戏"<<endl;
        system("pause");
        return 0;
    }
    snake_length=5;
    for(int i=0;i<5;i++){
        snake[i].x=1;
        snake[i].y=5-i;
    }
    dir=3;
    system("cls");
    hide();
    print_wall(60,20);
    print_food();
    print_snake();
    locate(3,62);
    cout<<"当前长度:";  
clock_t a,b;
    char ch;
    double hard_len;  
/**开始游戏*/
    while(1){
        hard_len=(double)snake_length/(double)(60*20);
        a=clock();
        while(1){
            b=clock();
            if(b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))){
                break;
            }
        }
        /**键入上下左右*/
        if(kbhit()){
            ch=getch();
            if(ch==-32){
                ch=getch();
                switch(ch){
                case 72://上
                    if(dir==2||dir==3){
                        dir=0;
                    }
                    break;
                case 80:
                    if(dir==2||dir==3){
                        dir=1;
                    }
                    break;
                case 75:
                    if(dir==0||dir==1){
                        dir=2;
                    }
                    break;
                case 77:
                    if(dir==0||dir==1){
                        dir=3;
                    }
                    break;
                }
            }
        }
        if(!go_ahead()){
            break;
        }
        locate (3,73);
        cout<<snake_length<<endl;
    }
    system("pause");
    return 0;
}  
0 条评论
信息
- 难度
 - 9
 - 分类
 - (无)
 - 标签
 - (无)
 - 递交数
 - 13
 - 已通过
 - 3
 - 通过率
 - 23%
 - 上传者