c++贪吃蛇小游戏

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
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;
char t; 
struct node
{
    int x,y;
}sn[1000];
 
int sn_l,d;
node f;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void pw()
{
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
    cout << endl;
    for (int j=0;j<=m-1;j++)
    {
        cout << "|";
        for (int i=1;i<=n;i++) cout << " ";
        cout << "|" << endl;
    }
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
}
void ps()
{
    locate(sn[0].x,sn[0].y);
    cout << t;
    for (int i=1;i<=sn_l-1;i++)
    {
        locate(sn[i].x,sn[i].y);
        cout << "*";
    }
}
bool ic()
{
    if (sn[0].x==0 || sn[0].y==0 || sn[0].x==m+1 || sn[0].y==n+1) return false;
    for (int i=1;i<=sn_l-1;i++)
    {
        if (sn[0].x==sn[i].x && sn[0].y==sn[i].y) return 0;
    }
    return 1;
}
bool pf()
{
    srand((unsigned)time(0));
    bool e;
    while (1)
    {
        e=true;
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
        f.x=i;f.y=j;
        for (int k=0;k<=sn_l-1;k++)
        {
            if (sn[k].x==f.x && sn[k].y==f.y)
            {
                e=false;break;
            }
        }
        if (e) break;
    }
    locate(f.x,f.y);
    cout << "$";
    return true;
}
 
bool go_ahead()
{
    node temp;
    bool e=false;
    temp=sn[sn_l-1];
    for (int i=sn_l-1;i>=1;i--)
        sn[i]=sn[i-1];
    sn[0].x+=direct[d][0];
    sn[0].y+=direct[d][1];
    locate(sn[1].x,sn[1].y);
    cout << "*";
    if (sn[0].x==f.x && sn[0].y==f.y)
    {
        sn_l++;
        e=true;
        sn[sn_l-1]=temp;
    }
    if (!e)
    {
        locate(temp.x,temp.y);
        cout << " ";
    }
    else
        pf();
    locate(sn[0].x,sn[0].y);
    cout << t;
    if (!ic())
    {
        system("cls");
        cout << "输了哦" << endl << "钱 " << sn_l << endl;
        return 0;
    }
    return 1;
}
int main()
{
    cout <<"请将窗口调大,以免发生错位"<<endl;
    cout <<"先选择难度,请在1-15中输入1个数,越大越难"<<endl;
    cout <<"输入一个头"<<endl; 
    cout <<"以方向键控制方向"<<endl;
    cout <<"不可以撞墙哦!"<<endl; 
    m=25;
    n=40; 
    if (m<10 || n<10 || m>25 || n>40)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    int h;
    cin >>h;
    cin >>t;
    if (h<=0 || h>100)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    sn_l=5;
    clock_t a,b;
    char ch;
    double hl;
    for (int i=0;i<=4;i++)
    {
        sn[i].x=1;
        sn[i].y=5-i;
    }
    d=3;
    system("cls");
    hide();
    pw();
    pf();
    ps();
    locate(m+2,0);
    cout << "现在的钱 ";
    while (1)
    {
        hl=(double)sn_l/(double) (m*n);
        a=clock();
        while (1)
        {
            b=clock();
            if (b-a>=(int)(400-30*h)*(1-sqrt(hl))) break;
        }
        if (kbhit())
        {
            ch=getch();
            if (ch==-32)
            {
                ch=getch();
                switch(ch)
                {
                case 72:
                    if (d==2 || d==3)
                        d=0;
                    break;
                case 80:
                    if (d==2 || d==3)
                        d=1;
                    break;
                case 75:
                    if (d==0 || d==1)
                        d=2;
                    break;
                case 77:
                    if (d==0 || d==1)
                        d=3;
                    break;
                }
            }
        }
        if (!go_ahead()) break;
        locate(m+2,12);
        cout << sn_l;
    }
    system("pause");
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值