自学C++写的一个简单贪吃小河游戏

Posted G.Z.Bing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自学C++写的一个简单贪吃小河游戏相关的知识,希望对你有一定的参考价值。

娱乐而已,菜鸡一只,技术有限

  1 /************************************
  2     作 者:G.Z.Bing
  3     时 间:2018.2.16
  4     邮 箱:cn_gzb@126.com
  5     项目名称:
  6 *************************************/
  7 
  8 #include<iostream>
  9 #include<windows.h>
 10 #include<ctime>                //  一些用于处理日期和时间的类型和函数
 11 #include<conio.h>            //定义键盘产生的对应操作
 12 
 13 #define   H    20
 14 #define   W    20   //定义游戏界面高和宽
 15 
 16 using namespace std;
 17 class game_interface
 18 {
 19 public:
 20     char qp[H][W];
 21     int i,j,x1,y1;
 22     game_interface();
 23     void food();
 24     void prt(int grade,int score,int gamespeed);
 25  };
 26 game_interface::game_interface()
 27 {
 28     for(i=1; i <= H-2; i ++)
 29         for(j=1; j <= W-2; j ++)
 30             qp[i][j] = \' \';
 31     for(i=0;i<=H-1;i++)
 32         qp[0][i] = qp[H-1][i] = \'#\';
 33     for(i=1;i<=H-2;i++)
 34         qp[i][0] = qp[i][W-1] = \'#\';
 35     food();
 36 }
 37 void game_interface::food()
 38 {
 39     srand(time(0));
 40     do
 41     {
 42         x1=rand()%W-2+1;
 43         y1=rand()%H-2+1;
 44     }
 45     while(qp[x1][y1]!=\' \');
 46     qp[x1][y1]=\'$\';
 47 }
 48 void game_interface::prt(int grade, int score, int gamespeed)
 49 {
 50     system("cls");
 51     cout << endl;
 52     for(i=0;i<H;i++)
 53     {
 54         cout << "\\t";
 55         for(j=0; j<W; j++)
 56             cout<<qp[i][j]<<\' \';
 57         if(i==0) cout << "\\t等级:" << grade;
 58         if(i==2) cout << "\\t吃掉个数:" << score/100;
 59         if(i==4)
 60           {
 61             cout <<"\\t对老婆的评价:";
 62              if(grade == 1)
 63                  cout <<"老婆笨笨哒" ;
 64              else if(grade == 2)
 65                  cout <<"老婆66哒" ;
 66              else if(grade == 3)
 67                  cout <<"老婆你是最棒的" ;
 68              else if(grade == 4)
 69                  cout <<"老婆爱你哟" ;
 70              else
 71                  cout <<"我老婆最厉害了,娶你哟" ;
 72         }
 73 
 74         if(i==6) cout << "\\t时间间隔(速度):" << gamespeed << "ms";
 75         cout<<endl;
 76 
 77     }
 78 }
 79 class snake : public game_interface
 80 {
 81 public:
 82     int zb[2][100];
 83     long start;
 84     int head, tail, grade, score, gamespeed, length, timeover, x, y;
 85     char direction;
 86     snake();
 87     void move();
 88  };
 89 snake::snake()
 90 {
 91     cout<<"\\n\\n\\t\\t游戏要开始了!"<<endl;
 92     for(i=3; i >= 0; i--)
 93     {
 94         start=clock();
 95         while(clock()-start<=1000);
 96         system("cls");
 97         if(i>0)
 98         cout << "\\n\\n\\t\\t倒计时:" << i << ""<< endl;
 99     }
100     for(i=1; i <= 3; i++)
101         qp[1][i]=\'*\';
102     qp[1][4]=\'@\';
103     for(i=0; i<4; i++)
104     {
105         zb[0][i] = 1;
106         zb[1][i] = i + 1;
107     }
108 }
109 void snake :: move()
110 {
111     score=0;
112     head = 3, tail = 0;
113     grade = 1, length = 4;
114     gamespeed = 500;
115     direction = 77;
116     while(1)
117     {
118         timeover = 1;
119         start = clock();
120         while((timeover=(clock() - start <= gamespeed))&&!kbhit());
121         if(timeover)
122         {
123             getch();
124             direction = getch();
125         }
126         switch(direction)
127         {
128         case 72: x= zb[0][head]-1; y= zb[1][head];break;
129         case 80: x= zb[0][head]+1; y= zb[1][head];break;
130         case 75: x= zb[0][head]; y= zb[1][head]-1;break;
131         case 77: x= zb[0][head]; y= zb[1][head]+1;break;
132         }
133         if(x==0 || x==H ||y==0 || y==W)                 //撞到边上
134         {
135             cout << "\\t\\t李雨潇是个大笨蛋,撞墙了!" << endl; break;
136         }
137         if(qp[x][y]!=\' \'&&!(x==x1&&y==y1))            // 蛇咬到自己
138         {
139             cout << "\\t"<<"\\t"<< "李雨潇是个大逗比,咬到自己!" << endl;break;
140         }
141         if(x==x1 && y==y1)
142         {
143             length ++;
144             score = score+100;
145             if(length>=8)
146             {
147                 length -= 8;
148                 grade ++;
149                 if(gamespeed >= 200)
150                     gamespeed = 550 - grade * 50;
151             }
152             qp[x][y]= \'@\';
153             qp[zb[0][head]][zb[1][head]] = \'*\';
154             head = (head+1)%100;
155             zb[0][head] = x;
156             zb[1][head] = y;
157             food();
158             prt(grade,score,gamespeed);
159         }
160         else
161         {
162             qp[zb[0][tail]][zb[1][tail]]=\' \';
163             tail=(tail+1)%100;
164             qp[zb[0][head]][zb[1][head]]=\'*\';
165             head=(head+1)%100;
166             zb[0][head]=x;
167             zb[1][head]=y;
168             qp[zb[0][head]][zb[1][head]]=\'@\';
169             prt(grade,score,gamespeed);
170         }
171     }
172 }
173 int main()
174 {
175     game_interface cb;
176     snake s;
177     s.move();
178 }

代码在codeblocks运行:

以上是关于自学C++写的一个简单贪吃小河游戏的主要内容,如果未能解决你的问题,请参考以下文章

求C++小游戏源代码啊~

简单贪吃蛇游戏js

JavaScrip写的贪吃蛇

贪吃蛇easyx版本

C语言之贪吃蛇(ncurses)

python实现贪吃蛇