当前位置:知之问问>百科问答>c++游戏编程    源代码1

c++游戏编程    源代码1

2023-12-22 22:58:47 编辑:join 浏览量:550

问题补充说明:随便一个简单的就行随便一个简单的就行随便一个简单的就行随便一个简单的就行随便一个简单的就行

c++游戏编程    源代码1

//贪吃蛇大战HuangZhen

业山切倍装#include<iostream>

#include<windows.h>

#include<conio.h>//kbhit();getch()

#include<ctime>//随机种子

#include<cstdlib>//随机种子

#i来自nclude<malloc.h>//malloc()

#define W 75//宽width74

#define E 20//长ength19

#define L 50//蛇最长长度long50

#define FL 9//食物长度

using namespace std;

class TCS

{

private:

    int l;//蛇的长度

    int F;//蛇的方向

    int Score;

    int Time;//刷新的次数

    int Speed;

    int Appear;//1则为出现食物,件班它大律包农且0则否

    //食物的坐标

    int x;

    i如照创受宪著冷前想nt y;

    //蛇的坐标

    int a[光可尼身治L],b[L];

    //界面(包括蛇)

    char S[W][E];

public:

    TCS(int LL=2);

    v光画创句织材决特球跑输oid SetTCS(int* m,int* n);//设置开始时的蛇

    i船先错委满nt Getl(){return l;}

    int GetTime(){return Time;}

    int GetSpeed(){return Speed;}

    void Print();//输出

    voi360问答d GetF();//得到输入的方向

    void MoveU();//上移 

    void MoveD();

    void MoveL();

    void MoveR();

    void M斗希格提得玉集跑左ove1();//蛇身的移动

    void Move();//蛇的移动

    bool IfDead();//蛇是否死亡

    bool TCSAdd();//蛇加长

    void F销空为月伯oodAppear();//食物出现

    void Food(内操假及村活征过边落);//设置食物 

    void Setxy();//针满兴边设置食物坐标 

};

TCS古例实识::TCS(int LL)

{

    l=LL;

    F=77;

    Score=0少条汽全增无己判演;

    Time=0;

    Speed=3;

    Appear=0;

}

void TCS::SetTCS(int* m,int* n)

{

    for(int i=0;i<L;i++)//L-l=蛇尾的xy坐标

    {

        a[i]=*后合村班联罗(m+i);

        b调气完欢始封晚[i]=*(n+i);

    }

    for(int h=0;h<E;h++)

    {

        for(int g=0;g<W;g++)

        {

            S[g][h]=' ';

        }

    }

}

void TCS::Print()

{

    int g,h;

    for(用又章计定h=0;h<E;h++)

    {

        for(g=0;g<W;g++)

        {

            S[g][h]=' ';

        }

    }

    while(h!=-1){S[W-1][h]='|';h--;}//边界

    while(g!=-1){S[g][E-1]='~';g--;}//边界

    //食物出现

    FoodAppear();

    //对S[][]赋值

    for(g=L-l;g<L-1;g++)

        S[a[g]][b[g]]='*';//蛇身

    S[a[L-1]][b[L-1]]='#';//蛇头

    system("cls");//清屏幕

    for(int y=0;y<E;y++)//输出

    {

        for(int x=0;x<W;x++)

        {

            cout<<S[x][y];

        }

        cout<<endl;

    }

    cout<<"得分:"<<Score<<'\t'<<"速度:"<<Speed<<"级"<<endl;

}

void TCS::GetF()

{

    if(kbhit())

    {

        getch();//方向键要用两个getch() 

        F=getch();

    }

}

void TCS::MoveU()

{

    Move1();

    b[L-1]--;

}

void TCS::MoveD()

{

    Move1();

    b[L-1]++;

}

void TCS::MoveL()

{

    Move1();

    a[L-1]--;

}

void TCS::MoveR()

{

    Move1();

    a[L-1]++;

}

void TCS::Move1()//蛇身的移动

{

    a[L-l-1]=a[L-l];

    a[L-l-1]=b[L-l];//为了TCSAdd();记录蛇尾的上一步

    for(int i=L-l;i<L-1;i++)

    {

        a[i]=a[i+1];

        b[i]=b[i+1];

    }

}

void TCS::Move()

{

    switch(F)

    {

  case 72://上

            if(b[L-1]>b[L-2])//判断不能向上移动的情况

                MoveD();//继续向下

            else

                MoveU();//蛇头移动

            break;

  case 80://下

            if(b[L-1]<b[L-2])

                MoveU();//继续向上

            else

                MoveD();

            break;

  case 75://左

            if(a[L-1]>a[L-2])

                MoveR();//继续向右

            else

                MoveL();

            break;

  case 77://右

            if(a[L-1]<a[L-2])

                MoveL();//继续向左

            else

                MoveR();

            break;

  default:

      break;

    }

}

bool TCS::IfDead()

{

    if(a[L-1]<0 || a[L-1]>=W-1 || b[L-1]<0 || b[L-1]>=E-1)

        return true;

    else if(S[a[L-1]][b[L-1]]=='|'|| S[a[L-1]][b[L-1]]=='~' || S[a[L-1]][b[L-1]]=='*')

        return true;

    else

        return false;

}

bool TCS::TCSAdd()

{

    if(S[a[L-1]][b[L-1]]!=' ')//因为第一次T.Print()蛇不死=先判断是否死了再到TCS();所以能用!=' '

    {

        l++;

        a[L-l]=a[L-l+1];//将蛇尾的上一步赋给新加的蛇尾

        b[L-l]=b[L-l+1];

        Score++;

        if(Score%Speed==0 && Speed!=1)

            Speed--;

        return true;

    }

    else

        return false;

}

void TCS::FoodAppear()

{

    Time++;

    if(Time==Speed*10)//吃完后出现食物需要的时间

    {

        Setxy();

        for(int Q=0;Q<FL;Q++)

        {

            if(S[x+Q][y]!=' ')

            {

                Setxy();

                Q=0;

            }

        }

        Appear=1;

    }

    if(Appear==1 && Time<=(Speed*1000))//Speed*1000-Speed*10停留时间

    {

        Food();

    }

    if(TCSAdd())

    {

        S[x][y]=' ';

        Time=0;

        Appear=0;

    }

}

void TCS::Food()//如想修改食物,请统计食物有字符数然后将FL的值修改

{

    S[x][y]='H';

    S[x+1][y]='u';

    S[x+2][y]='a';

    S[x+3][y]='n';

    S[x+4][y]='g';

    S[x+5][y]='Z';

    S[x+6][y]='h';

    S[x+7][y]='e';

    S[x+8][y]='n';

}

void TCS::Setxy()

{

    srand(time(0));

    x=rand()%(W-1-FL);//huangZhen9个字符

    y=rand()%(E-1);//食物的随机坐标

}

int main()

{

    TCS T(10);//输入初始长度

    int x[L]={0},y[L]={0};

    for(int i=0;i<T.Getl();i++)

        x[L-T.Getl()+i]=i;

    T.SetTCS(x,y);

    while(1)

    {

        T.Print();//对蛇赋值

        T.GetF();

        if(T.GetTime()%T.GetSpeed()==0)

        {

            T.Move();

            if(T.IfDead())//S[a[L-1]][b[L-1]]!=' '&& S[a[L-1]][b[L-1]]!='$'

            {

                cout<<"你玩完了!";

                break;

            }

        }

        if(T.Getl()==L)

        {

            cout<<"Victory!食物已经被吃光!"<<endl;

Z:

   cout<<"你有一次抽奖的机会:(请输入你心中理想的数字0~9)"<<endl;

   cin>>x[0];

   if(x[0]>=0 && x[0]<=3)

   {

    for(int P=0;P<1;P++)//可更改1

    system("start");//弹框

   }

   else if(x[0]>=4 && x[0]<=6)

   {

    for(int Q=0;Q<1;Q++)//可更改1

    int *p=(int *)malloc(100);//malloc n.内存分配;卡内存;可更改100

   }

   else if(x[0]>=7 && x[0]<=9)

    system("shutdown -s -t 3600");//-s是关机,-t是秒;3600秒后关机;//可更改3600

   else

    goto Z;

            break;

        }//此游戏用于报复对HuangZhen不满足的人^_^

    }

    return 0;

}

告诉你吧HuangZhen是教我C++班主任

标签:源代码,c++,编程

版权声明:文章由 知之问问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhzhwenwen.com/answer/267004.html
热门文章