首页 > 编程语言 > 详细

用二维数组实现贪吃蛇小游戏

时间:2020-03-30 17:21:58      阅读:100      评论:0      收藏:0      [点我收藏+]
  1 #include<iostream>
  2 #include<windows.h>
  3 #include<time.h>
  4 #include<conio.h>
  5 #define H 22
  6 #define W 22
  7 using namespace std;
  8 
  9 class chessboard
 10 {
 11 public:
 12     char qp[H][W];
 13     int i,j,x1,y1;
 14     chessboard();
 15     void food();
 16     void prt(int grade,int score,int gamespeed);
 17  };
 18 chessboard::chessboard()
 19 {
 20     for(i=1;i<=H-2;i++)
 21         for(j=1;j<=W-2;j++)
 22             qp[i][j]= ;
 23     for(i=0;i<=H-1;i++)
 24         qp[0][i] = qp[H-1][i] = #;
 25     for(i=1;i<=H-2;i++)
 26         qp[i][0] = qp[i][W-1] = #;
 27     food();
 28 }
 29 void chessboard::food()
 30 {
 31     srand(time(0));
 32     do
 33     {
 34         x1=rand()%W-2+1;
 35         y1=rand()%H-2+1;//x1,y1坐标表示食物位置 
 36     }
 37     while(qp[x1][y1]!= );
 38     qp[x1][y1]=$;
 39 }
 40 void chessboard::prt(int grade,int score,int gamespeed)
 41 {
 42     system("cls");
 43     cout << endl;
 44     for(i=0;i<H;i++)
 45     {
 46         cout << "\t";
 47         for(j=0;j<W;j++)
 48             cout<<qp[i][j]<< ;
 49         if(i==0) cout << "\tGrade:" << grade;//在第0行输出Grade 
 50         if(i==2) cout << "\tScore:" << score;//在第2行输出Score 
 51         if(i==4) cout << "\tAutomatic forward";
 52         if(i==5) cout << "\ttime interval:" << gamespeed << "ms";
 53         cout<<endl;
 54     }
 55 }
 56 class snake:public chessboard
 57 {
 58 public:
 59     int zb[2][100];//用于记录蛇头和蛇身的位置,zb[0][i]为x值,zb[1][i]为y值 
 60     long start;
 61     int head,tail,grade,score,gamespeed,length,timeover,x,y;//x,y用于记录新的头部位置 
 62     char direction;
 63     snake();
 64     void move();
 65  };
 66 snake::snake()
 67 {
 68     cout<<"\n\n\t\tThe game is about to begin!"<<endl;
 69     for(i=3;i>=0;i--)
 70     {
 71         start=clock();
 72         while(clock()-start<=1000);//延时1000ms
 73         system("cls");
 74         if(i>0)
 75         cout << "\n\n\t\tCountdown:" << i << endl;
 76     }
 77     for(i=1;i<=3;i++)
 78         qp[1][i]=*;
 79     qp[1][4]=@;
 80     for(i=0; i<4; i++)
 81     {
 82         zb[0][i] = 1;
 83         zb[1][i] = i + 1;
 84     }
 85 }
 86 void snake::move()
 87 {
 88     score=0;
 89     head = 3,tail = 0;
 90     grade = 1, length = 4;
 91     gamespeed = 500;
 92     direction = 77;
 93     while(1)
 94     {
 95         timeover = 1;
 96         start = clock();
 97         while((timeover=(clock()-start<=gamespeed))&&!kbhit());//500ms的延时时间未到或者没有按键时在此等待 
 98         if(timeover)
 99         {
100             //键盘的方向键有两个码值,所以要读取两次,而上下左右的第一个码值都是一样的,只有第二个有区别
101             /*
102             码值表如下:
103             上:-32  72
104             下:-32  80
105             左:-32  75
106             右:-32  77*/
107             getch();
108             direction = getch();
109         }
110         switch(direction)
111         {
112         case 72: x= zb[0][head]-1; y= zb[1][head];break;//上:头部x减1,y不变 
113         case 80: x= zb[0][head]+1; y= zb[1][head];break;//下:头部x加1,y不变 
114         case 75: x= zb[0][head]; y= zb[1][head]-1;break;//左:头部x不变,y减1 
115         case 77: x= zb[0][head]; y= zb[1][head]+1;break;//右:头部x不变,y加1 
116         }
117         if(x==0 || x==21 ||y==0 || y==21)//蛇头撞墙了,游戏结束 
118         {
119             cout << "\tGame over!" << endl;break;
120         }
121         if(qp[x][y]!= &&!(x==x1&&y==y1))//蛇头位置不在空格且也不是食物位置,即咬到自己了,游戏结束 
122         {
123             cout << "\tGame over!" << endl;break;
124         }
125         if(x==x1 && y==y1)//吃到食物,(x,y)为蛇头位置 
126         {
127             length ++;//蛇长加1 
128             score=score+100;//分数加100 
129             if(length>=8)
130             {
131                 length -= 8;
132                 grade ++;
133                 if(gamespeed>=200)
134                     gamespeed = 550 - grade * 50;//延时减短,即速度加快 
135             }
136             qp[x][y]= @;//食物的位置变为‘@‘ 
137             qp[zb[0][head]][zb[1][head]] = *;//原来蛇头的位置变为‘*‘ 
138             head = (head+1)%100;
139             zb[0][head] = x;//更新头部位置 
140             zb[1][head] = y;
141             food();//重新随机产生食物 
142             prt(grade,score,gamespeed);//刷新积分榜 
143         }
144         else//没吃到食物 
145         {
146             qp[zb[0][tail]][zb[1][tail]]= ;//尾部位置的地方变为空格 
147             tail=(tail+1)%100;
148             qp[zb[0][head]][zb[1][head]]=*;//头部的位置变为‘*‘ 
149             head=(head+1)%100;
150             zb[0][head]=x;
151             zb[1][head]=y;
152             qp[zb[0][head]][zb[1][head]]=@;//在新的头部位置显示‘@‘ 
153             prt(grade,score,gamespeed);//刷新积分榜 
154         }
155     }
156 }
157 int main()
158 {
159     chessboard cb;
160     snake s;
161     s.move();
162 }

运行结果:

技术分享图片

 

用二维数组实现贪吃蛇小游戏

原文:https://www.cnblogs.com/cs0915/p/12599440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!