[娱乐]一款浅陋的C++控制台贪吃蛇小游戏

因为时间有限,没能尽可能完善这一款贪吃蛇,不过能做出来还是很有意思的

  1 #include<bits/stdc++.h>
  2 #include<windows.h>
  3 #include<conio.h>
  4 #include<stdlib.h>
  5 using namespace std;
  6 const int dy[5]={0,-1,1,0,0};
  7 const int dx[5]={0,0,0,1,-1};
  8 int n=50,m=30,Score,Speed;
  9 int kind[101][31];
 10 struct point{
 11     int x,y,kind;
 12 };
 13 HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄
 14 void Setup(){
 15     system("color 0F");//设置控制台界面背景颜色和前景颜色
 16     system("mode con cols=50 lines=31");//初始化缓冲区大小
 17     SetConsoleTitle("Greddy Snake");//设置控制台窗口标题
 18 }
 19 void Move(int x,int y){//移动到指定坐标
 20     COORD posPoint={x,y};
 21     SetConsoleCursorPosition(hOut,posPoint);
 22 }
 23 void Home(){//显示首页
 24     system("CLS");
 25     Move(0,0);
 26     printf("移动 : [A] [S] [D] [W]\n");
 27     printf("暂停 : [P]\n");
 28     printf("确定 : [Space]\n");
 29     system("pause");
 30 }
 31 
 32 void Addapp(){//放置苹果 
 33     srand(time(0));
 34     if (40<Score++) Speed=100;
 35     int x,y;
 36     do{
 37         x=rand()%n;
 38         y=rand()%m;
 39     }while (kind[x][y]);
 40     kind[x][y]=2;
 41     Move(x,y);printf("#");
 42 }
 43 
 44 bool Update(queue<point>& que,int now){//更新蛇体
 45     Move(12,30);printf("%d",Score);
 46     point s=que.back();
 47     s.x+=dx[now],s.y+=dy[now],s.kind=now;
 48     s.x=(s.x+n)%n,s.y=(s.y+m)%m;
 49     if (kind[s.x][s.y]==1) return false;
 50     if (kind[s.x][s.y]==2) Addapp();//吃到苹果 
 51     else{
 52         point ss=que.front();
 53         Move(ss.x,ss.y);printf(" ");
 54         que.pop();kind[ss.x][ss.y]=0;
 55     }
 56     Move(s.x,s.y);printf("*");
 57     que.push(s);kind[s.x][s.y]=1;
 58     return true;
 59 }
 60 bool flag=true;
 61 void Play(){
 62     system("CLS");
 63     Move(0,30);printf("当前得分:    |----------<0xfaner>----------|",Score);
 64     memset(kind,0,sizeof(kind));
 65     kind[0][0]=1;
 66     queue<point> que;
 67     point a={0,0,1};que.push(a);
 68     Speed=200;Score=0;Addapp();
 69     int now=3;
 70     while (true){
 71         char x=' ';
 72         if (_kbhit()) x=getch();
 73         if ('a'<=x&&x<='z') x+='A'-'a';
 74         switch (x){
 75             case 'W':if (now>2) now=1;    break;
 76             case 'S':if (now>2) now=2;    break;
 77             case 'D':if (now<3) now=3;    break;
 78             case 'A':if (now<3) now=4;    break;
 79             case 'P':{
 80                 while (true){
 81                     if (_kbhit()) x=getch();
 82                     if (x!='P') break;
 83                 }
 84                 break;
 85             }
 86         }
 87         if (!Update(que,now)){
 88             Sleep(1000);
 89             system("CLS");
 90             Move(18,12);
 91             printf("You Lose");
 92             Move(11,13);
 93             printf("Press [R] to play again");
 94             Move(11,14);
 95             printf("Press [E] to exit game");
 96             do{
 97                 char x=getch();
 98                 if ('a'<=x&&x<='z') x+='A'-'a';
 99                 if (x=='E') flag=false;
100                 if (x=='E'||x=='R') break;
101             }while (true);
102             break;
103         }
104         Sleep(Speed);
105     }
106 }
107 int main(){
108     Setup();
109     Home();
110     while (flag) Play();
111     return 0;
112 }
Greedy_Snake

之后有时间我会继续完善这一款小游戏的(咕咕咕 

计划清明节之前用多线程重写一下(咕咕咕 

转载于:https://www.cnblogs.com/liyian/p/10639312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值