1.随机迷宫:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 31 //迷宫的高度,必须为奇数
#define Width 25 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
int map[Height+2][Width+2];
void gotoxy(int x,int y) //移动坐标
{
/* typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD, *PCOORD; */
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
/* typedef void *HANDLE; */
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
/* typedef struct _CONSOLE_CURSOR_INFO {
DWORD dwSize;
BOOL bVisible;
} CONSOLE_CURSOR_INFO *PCONSOLE_CURSOR_INFO; */
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible = 0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) //随机生成迷宫
{
int c[4][2] = {0,1,1,0,0,-1,-1,0}; //四个方向
int i,j,t;
//将方向打乱
for(i=0;i<4;i++)
{
j = rand()%4;
t = c[i][0];
c[i][0] = c[j][0];
c[j][0] = t;
t = c[i][1];
c[i][1] = c[j][1];
c[j][1] = t;
}
map[x][y] = Road;
for(i = 0;i < 4;i++)
if(map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
{
map[x + c[i][0]][y + c[i][1]] = Road;
create(x+ 2 * c[i][0],y + 2 * c[i][1]);
}
}
int get_key() //接收按键
{
char c;
while(c = getch())
{
if(c == 27)
return Esc; //Esc
if(c != -32)
continue;
c = getch();
if(c == 72)
return Up; //上
if(c == 80)
return Down; //下
if(c ==75 )
return Left; //左
if(c == 77)
return Right; //右
}
return 0;
}
void paint(int x,int y) //画迷宫
{
gotoxy(2 * y - 2,x - 1);
switch(map[x][y])
{
case Start:
printf("入");break; //画入口
case End:
printf("出");break; //画出口
case Wall:
printf("※");break; //画墙
case Road:
printf(" ");break; //画路
}
}
void game()
{
int x=2,y=1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while(1)
{
gotoxy(2 * y - 2,x - 1);
printf("☆"); //画出玩家当前位置
if(map[x][y] == End) //判断是否到达出口
{
gotoxy(30,24);
printf("到达终点,按任意键结束");
getch();
break;
}
c = get_key();
if(c == Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y] != Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+1][y] != Wall)
{
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-1] != Wall)
{
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+1] != Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
int i,j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for(i = 0;i <= Height + 1;i++)
for(j = 0;j <= Width + 1;j++)
if(i == 0 || i == Height + 1 || j == 0 || j == Width + 1) //初始化迷宫
map[i][j] = Road;
else
map[i][j] = Wall;
create(2 * (rand() % (Height / 2)+1),2 * (rand() % (Width / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for(i = 0;i <= Height+1;i++) //边界处理
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //边界处理
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //给定入口
map[Height-1][Width]=End; //给定出口
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) //画出迷宫
paint(i,j);
game(); //开始游戏
getch();
return 0;
}
2.文字游戏
#include <iostream>
using namespace std;
double shengmingli=200000000000;//定义主角初始生命力
int gongjili=150000;//定义主角初始攻击力
int fangyuli=2000000;//定义主角初始防御力
int money=2000000;//定义主角初始金钱数量
bool guoguan;//定义是否通关判定
void wuqidian();//定义武器店函数
void yaodian();//定义药店函数
void guaiwu1();//定义小怪物函数
void guaiwu2();//定义大怪物函数
int main()
{
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
int xiaozhen;//定义选择项目
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//输入5时退出游戏
{
if(shengmingli<=0)//主角生命力小于等于0时游戏结束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通关!"<<endl;
break;
}
if(xiaozhen==6)//输入6可检测自己的状态
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻击力:"<<gongjili<<endl;
cout<<"你的防御力:"<<fangyuli<<endl;
cout<<"你拥有的钱:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"请不要乱选!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出游戏……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"欢迎来到武器店!"<<endl;
cout<<"1、买小刀(1M加2攻击力)"<<endl;
cout<<"2、买短剑(2M加20攻击力)"<<endl;
cout<<"3、买大砍刀(5M加40攻击力)"<<endl;
cout<<"4、买双节棍(7M加60攻击力)"<<endl;
cout<<"5、买盾牌(2M加30防御力)"<<endl;
cout<<"6、买铠甲(5M加60防御力)"<<endl;
cout<<"7、离开武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//输入7时结束函数
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;//钱不够时返回Flase
else
cout<<"购买成功!"<<endl;//钱足够时返回True
gongjili+=2;
money-=1;
break;
case 2 : if(money<80)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=20;
money-=80;
break;
case 3 : if(money<140)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=40;
money-=140;
break;
case 4 : if(money<200)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=60;
money-=200;
break;
case 5 : if(money<60)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
fangyuli+=30;
money-=60;
break;
fangyuli+=60;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //返回main()主函数
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*
yaodian()的设置与wuqidian()相同,可参照阅读.
*/
void yaodian()
{
cout<<"欢迎来到药品店!"<<endl;
cout<<"1、买1号补血药(10M加200生命)"<<endl;
cout<<"2、买2号补血药(50M加1000生命力)"<<endl;
cout<<"3、买3号补血药(100M加2200生命力)"<<endl;
cout<<"4、离开武器店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=4)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=200;
money-=10;
break;
case 2 : if(money<50)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=1000;
money-=50;
break;
case 3 : if(money<100)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=2200;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
void guaiwu1()
{
cout<<"开始与小怪物战斗!!!"<<endl;
double* g_shengmingli=new double;//定义怪物生命
int* g_gongjili=new int;//定义怪物攻击力
int* g_fangyuli=new int;//定义怪物防御力
int* g_money=new int;//定义怪物金钱
*g_shengmingli=100;
*g_gongjili=5;
*g_fangyuli=3;
*g_money=5;
double* tongji1=new double;//用来计算主角对怪物的杀伤
double* tongji2=new double;//用来计算怪物对主角的杀伤
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定义回合数
*huihe=1;
cout<<"你开始对小怪物进行攻击!"<<endl;
int* xuanze=new int;
/*
攻击计算公式
杀伤=攻击力*2-防御力
玩家每回合可以选择攻击与逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对小怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物对你发动了攻击!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//杀死怪物后的返回
cout<<"小怪物被你杀死了!你真厉害!!!"<<endl;
money+=(*g_money);
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{//被怪物杀死后的返回
cout<<"你被小怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的返回
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
设置均与void函数guaiwu1()相同,可参照上例阅读.
*/
void guaiwu2()
{
cout<<"开始与大怪物战斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=3600;
*g_gongjili=500;
*g_fangyuli=500;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你开始对大怪物进行攻击!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对大怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"大怪物对你发动了攻击!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你杀死了!你真厉害!!!"<<endl;
guoguan=true;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
3.另一个文字游戏
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
double shanghai[20]={0.6,1.1,2,3.16,5.5,7,10,20,50,100,146.23,254.13,312,403,601,1023};
double bosshealth[20]={2,3,4,5.9,8,14,19,32,73,157,200,403,801,1200,3630,20123};
double wj_shanghai=5000,wj_health=10000000,wj_max_health=10000000,boss,wj_money=10000000000000000000000000000000;
void chushihua();
void game();
void gongji();
void goumai();
void shangdian();
void zhujiemian();
void fangyu();
void cend();
void chushou();
void print(char[]);
int bishou=0,caidao=0,jian=0,shenjian=0;
double bishou_1=5,caidao_1=17,jian_1=58,shenjian_1=124;
int hat=0,douhui=0,hudun=0,hunjia=0,shendun=0;
double hat_1=7,douhui_1=21,hudun_1=49,hunjia_1=89,shendun_1=210.4;
void cend()
{
system("cls");
print("GAME OVER");
exit(1);
}
void game()
{
int k;
chushihua();
IO:
printf("请输入对手等级 (0~15)\n");
scanf("%d",&k);
if(k>15||k<0)
{
system("cls");
goto IO;
}
boss=bosshealth[k];
system("cls");
while(wj_health>=0)
{
srand(time(NULL));
QP:
printf("1.逃跑 2.进攻\n");
char s=getch();
if(s<'1'||s>'2')
{
system("cls");
goto QP;
}
if(s=='1')
{
system("cls");
zhujiemian();
}
system("cls");
double l=shanghai[k]*((rand()%2)+1)+fabs(double(rand()%100/100-2));
printf("对手对你造成了%lf点伤害\n",l);
wj_health-=l;
printf("你当前剩余血量:%lf\n",wj_health);
if(wj_health<=0)
cend();
double o=wj_shanghai*((rand()%2)+1)+double(rand()%10/10);
boss-=o;
printf("你对对手造成了%lf点伤害\n",o);
printf("对手当前剩余血量:%lf\n\n",boss);
if(boss<=0)
{
printf("胜利!\n获得%lf金币\n\n当前剩余血量:%lf\n",shanghai[k]+3,wj_health);
wj_money+=shanghai[k]+3;
printf("\n余额:%lf\n",wj_money);
getch();
if(k==15)
{
printf("恭喜玩家!游戏胜利!\n");
getch();
exit(1);
}
system("cls");
zhujiemian();
}
}
}
void zhujiemian()
{
PO:
printf("1.商店 2.战斗 3.回血 4.状态\n");
char k=getch();
if(k>'4'||k<'1')
{
system("cls");
goto PO;
}
if(k=='1')
{
system("cls");
shangdian();
return;
}
if(k=='2')
{
system("cls");
game();
return;
}
if(k=='3')
{
system("cls");
if(wj_money>0)
{
wj_money=wj_money*4/5-1;
chushihua();
wj_health=wj_max_health;
printf("回血成功!\n");
getch();
system("cls");
goto PO;
}
else
{
printf("余额不足!\n");
getch();
system("cls");
goto PO;
}
}
if(k=='4')
{
chushihua();
system("cls");
printf("生命值:%lf\n",wj_health);
printf("最大生命值:%lf\n",wj_max_health);
printf("攻击力:%lf\n",wj_shanghai);
printf("金币:%lf\n",wj_money);
getch();
system("cls");
goto PO;
}
if(k=='5')
{
string a;
system("cls");
printf("输入密码!\n");
cin>>a;
if(a=="gu"||a=="gu")
{
wj_money+=1000;
printf("外挂生效\n");
Sleep(1000);
system("cls");
goto PO;
}
printf("外挂失败\n");
Sleep(1000);
system("cls");
goto PO;
}
}
void shangdian()
{
LK:
printf("1.购买 2.返回主界面\n");
char k=getch();
if(k!='1'&&k!='2')
{
system("cls");
goto LK;
}
if(k=='1')
{
system("cls");
goumai();
goto LK;
}
if(k=='2')
{
system("cls");
zhujiemian();
return;
}
}
void goumai()
{
ML:
printf("1.攻击 2.防御 3.返回主界面\n");
char k=getch();
if(k!='1'&&k!='2'&&k!='3')
{
system("cls");
goto ML;
}
if(k=='1')
{
system("cls");
gongji();
goto ML;
}
if(k=='3')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
fangyu();
}
}
void gongji()
{
OP:
system("cls");
printf("0.返回上界面\n");
printf("1.返回主界面\n");
printf("2.匕首 5金币\n");
printf("3.菜刀 17金币\n");
printf("4.剑 68金币\n");
printf("5.圣剑 210金币\n");
printf("提醒:金币价格与伤害成正比\n");
char k=getch();
if(k<'0'||k>'5')
{
system("cls");
goto OP;
}
if(k=='0')
{
system("cls");
goumai();
return;
}
if(k=='1')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
if(wj_money>=bishou_1)
{
chushihua();
system("cls");
wj_money-=bishou_1;
bishou++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
system("cls");
goto OP;
}
if(k=='3')
{
if(wj_money>=caidao_1)
{
chushihua();
system("cls");
wj_money-=caidao_1;
caidao++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
if(k=='4')
{
if(wj_money>=jian_1)
{
chushihua();
system("cls");
wj_money-=jian_1;
jian++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
if(k=='5')
{
if(wj_money>=shenjian_1)
{
chushihua();
system("cls");
wj_money-=shenjian_1;
shenjian++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
}
void fangyu()
{
OP:
system("cls");
printf("0.返回上界面\n");
printf("1.返回主界面\n");
printf("2.帽子 7金币\n");
printf("3.头盔 21金币\n");
printf("4.护盾 49金币\n");
printf("5.盔甲 89金币\n");
printf("6.圣盾 210金币\n");
printf("提醒:金币价格与伤害成正比\n");
char k=getch();
if(k<'0'||k>'6')
{
system("cls");
goto OP;
}
if(k=='0')
{
system("cls");
goumai();
return;
}
if(k=='1')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
if(wj_money>=hat_1)
{
chushihua();
system("cls");
wj_money-=hat_1;
hat++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
system("cls");
goto OP;
}
if(k=='3')
{
if(wj_money>=douhui_1)
{
chushihua();
system("cls");
wj_money-=douhui_1;
douhui++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
if(k=='4')
{
if(wj_money>=hudun_1)
{
chushihua();
system("cls");
wj_money-=hudun_1;
hudun++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
if(k=='5')
{
chushihua();
if(wj_money>=hunjia_1)
{
system("cls");
wj_money-=hunjia_1;
hunjia++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
if(k=='6')
{
if(wj_money>=shendun_1)
{
chushihua();
system("cls");
wj_money-=shendun_1;
shendun++;
goto OP;
}
system("cls");
printf("余额不足!\n");
getch();
goto OP;
}
}
void chushihua()
{
wj_max_health=hat*hat_1+douhui*douhui_1+hudun*hudun_1+hunjia*hunjia_1+shendun*shendun_1+10;
wj_shanghai=bishou*bishou_1+caidao*caidao_1+jian*jian_1+shenjian*shenjian_1+1;
}
void print(char a[])
{
int s=strlen(a);
for(int i=0;i<s;i++)
{
cout<<a[i];
Sleep(400);
}
getch();
system("cls");
}
int main()
{
system("title game");
print("出品:谷游");
zhujiemian();
return 0;
}
4.坦克大战
#include <iostream>
#include <time.h>
#include <windows.h>
#define W 1 //上
#define S 2 //下
#define A 3 //左
#define D 4 //右
#define L 4 // 坦克有4条命
void HideCursor() { //隐藏光标
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void GoToxy(int x, int y) { //光标移动,X、Y表示横、纵坐标
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//全局变量
int map[50][40];//地图二维数组
int B_num; //子弹编号
int Pos; //敌方坦克生成位置,-1为左边,0为中间,1为右边,2为我的坦克位置
int Speed = 7; //游戏速度
int Enemy; //还未出现的敌人
const char* Tank_Model[3][4] ={
{"◢┃ ◣","◢╦ ◣","◢╦ ◣","◢╦ ◣"},
{"╠ █ ╣","╠ █ ╣","━█ ╣","╠ █━"},
{"◥╩ ◤","◥┃ ◤","◥╩ ◤","◥╩ ◤"}
};
//坦克
class Tank{
public:
int x, y; //中心坐标
int Direction; //方向
int Model; //模型
int Revival; //复活次数
int Num; //敌方坦克编号
bool Type; //我方坦克此参数为1
bool Exist; //存活为1,不存活为0
}AI_tank[6], my_tank;
//子弹
class Bullet{
public:
int x, y; //坐标
int Direction; //方向
bool Exist; //1为存在,0不存在
bool Type; //0为敌方子弹,1为我方子弹
}bullet[50] ;
//基本函数
void GoToxy(int x, int y); //光标移动
void HideCursor(); //隐藏光标
void Key(); //键盘输入
void Init(); //初始化
void Pause(); //暂停
void Show(); //打印框架
void Print_Map(); //打印地图
void Cheak_Game(); //检测游戏胜负
void GameOver(); //游戏结束
//坦克
void Creat_AI_T(Tank* AI_tank); //建立坦克
void Creat_My_T(Tank* my_tank);
void Move_AI_T(Tank* AI_tank);//坦克移动
void Move_My_T(int turn);
void Clear_T(int x, int y); //清除坦克
void Print_T(Tank tank); //打印坦克
bool Cheak_T(Tank tank, int direction); //检测障碍,1阻碍
//子弹
void Creat_AI_B(Tank* tank); //敌方坦克发射子弹
void Creat_My_B(Tank tank);//我方坦克发射子弹
void Move_B(Bullet bullet[50]); //子弹移动
void Break_B(Bullet* bullet); //子弹碰撞
void Print_B(int x, int y);//打印子弹
void Clear_B(int x, int y); //清除子弹
int Cheak_B(int x, int y); //子弹前方情况
void Show() { //打印框架
std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";
for (int i = 0; i < 48; i++) {
std::cout << "▕ ▏\n";
}
std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";
std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
}
void Print_Map() { // 打印地图
int Map[50][40] = {
//map里的值: 0为可通过陆地,1为砖,6为墙,100~105为敌方坦克,200为我的坦克,
{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0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,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 }
};
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
map[i][j] = Map[i][j];
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
if (map[i][j] == 1) {
GoToxy(2 * j, i);
std::cout << "▓";
}
else if (map[i][j] == 6) {
GoToxy(2 * j, i);
std::cout << "■";
}
GoToxy(38, 46);
std::cout << " ◣◢";
GoToxy(38, 47);
std::cout << "◣█ ◢";
GoToxy(38, 48);
std::cout << "◢█ ◣";
}
void Cheak_Game() {
//敌人坦克全部不存活
if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist
&& !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)
GameOver();
if (my_tank.Revival >= L)//我复活次数用完
GameOver();//游戏结束
}
void GameOver() {
bool home = 1;
while (home) {
GoToxy(37, 21);
std::cout << "游戏结束!";
if (GetAsyncKeyState(0xD) & 0x8000) { //回车键
system("cls"); //清屏
Show();
Init(); //初始化
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
exit(0);
}
}
void Creat_My_T(Tank* my_tank) {//建立我的坦克
my_tank->x = 15;
my_tank->y = 47;
my_tank->Direction = 1;
// my_tank->Model = 0;
my_tank->Exist = 1;
my_tank->Type = 1;
Print_T(*my_tank); //打印我的坦克
}
void Move_My_T(int turn) {//turn为Key()函数传入的方向值
Clear_T(my_tank.x, my_tank.y);
my_tank.Direction = turn;
if (Cheak_T(my_tank, my_tank.Direction)) //我方坦克当前方向上无障碍
switch (turn) {
case W: my_tank.y--; break; //上
case S: my_tank.y++; break; //下
case A: my_tank.x--; break; //左
case D: my_tank.x++; break; //右
}
Print_T(my_tank);
}
void Print_T(Tank tank) {//打印
for (int i = 0; i < 3; i++) {
GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐标的左边,上中下三行打印
std::cout << Tank_Model[i][tank.Direction - 1]; //打印的是地址,地址既字符串
for (int j = 0; j < 3; j++)
if (tank.Type)//若为我的坦克
map[tank.y + j - 1][tank.x + i - 1] = 200;
//在map上敌方值为100~105,我方为200
else
map[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;
//这样可以通过map值读取坦克编号
}
}
void Creat_AI_T(Tank* AI_tank) {
AI_tank->x = 19 + 17 * (Pos); //pos为坦克生成位置,-1为左位置,0为中间,1为右,2为我的坦克位置
AI_tank->y = 2;
AI_tank->Direction = 2; //方向朝下
AI_tank->Revival++; //复活次数+1
AI_tank->Exist = 1;//存在
Pos++;
Enemy--;
if (Pos == 2) //循环重置(pos只能为-1,0,1)
Pos = -1;
Print_T(*AI_tank);
return;
}
void Move_AI_T(Tank* AI_tank) {
if (AI_tank->Exist) { //存在
Clear_T(AI_tank->x, AI_tank->y);
if (Cheak_T(*AI_tank, AI_tank->Direction))//前方无障碍
switch (AI_tank->Direction) {
case W: AI_tank->y--; break; //上
case S: AI_tank->y++; break; //下
case A: AI_tank->x--; break; //左
case D: AI_tank->x++; break; //右
}
else {//前方有障碍
for (int i = rand() % 4 + 1; i <= 4; i++)
if (Cheak_T(*AI_tank, i)){ //循环判断,返1可通过
AI_tank->Direction = i;
break;
}
}
Print_T(*AI_tank); //打印敌方坦克
}
}
bool Cheak_T(Tank tank, int direction) { //检测坦克前方障碍,返1为可通过
switch (direction) {
case W:
if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)
return 1;
else return 0;
case S:
if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)
return 1;
else return 0;
case A:
if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)
return 1;
else return 0;
case D:
if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)
return 1;
else return 0;
default: return 0;
}
}
void Clear_T(int x, int y) { //清除坦克
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++) {//将坦克占用的地图清零
map[y + j - 1][x + i - 1] = 0;
GoToxy(2 * x + 2 * j - 2, y + i - 1);
std::cout << " ";
}
}
//键盘输入
void Key() {
//上下左右键
if (GetAsyncKeyState('W') & 0x8000)
Move_My_T(W);
else if (GetAsyncKeyState('S') & 0x8000)
Move_My_T(S);
else if (GetAsyncKeyState('A') & 0x8000)
Move_My_T(A);
else if (GetAsyncKeyState('D') & 0x8000)
Move_My_T(D);
//子弹发射
else if (GetAsyncKeyState('P') & 0x8000) {
Creat_My_B(my_tank);
}
else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc键退出
exit(0);
else if (GetAsyncKeyState(0x20) & 0x8000)//空格暂停
Pause();
}
void Pause() { //暂停
while (1) {
if (GetAsyncKeyState(0xD) & 0x8000) { //回车键继续
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
exit(0);
}
}
void Creat_AI_B(Tank* tank){ //敌方发射子弹
if (!(rand() % 1)) { //在随后的每个游戏周期中有10分之一的可能发射子弹
Creat_My_B(*tank);
}
}
void Creat_My_B(Tank tank) {
switch (tank.Direction)
{
case W:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y - 2;
bullet[B_num].Direction = 1;//1表示向上
break;
case S:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y + 2;
bullet[B_num].Direction = 2;//2表示向下
break;
case A:
bullet[B_num].x = tank.x - 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 3;//3表示向左
break;
case D:
bullet[B_num].x = tank.x + 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 4;//4表示向右
break;
}
bullet[B_num].Exist = 1; //子弹存在
bullet[B_num].Type = tank.Type; //我方坦克发射的子弹bullet.Type=1
B_num++;
if (B_num == 50) //如果子弹编号增长到50号,那么重头开始编号
B_num = 0; //考虑到地图上不可能同时存在50颗子弹,所以数组元素设置50个
}
void Move_B(Bullet bullet[50]) { //子弹移动
for (int i = 0; i < 50; i++) {
if (bullet[i].Exist) {//如果子弹存在
if (map[bullet[i].y][bullet[i].x] == 0) {
Clear_B(bullet[i].x, bullet[i].y);//子弹当前位置无障碍,抹除子弹图形
switch (bullet[i].Direction) {//子弹变到下一个坐标
case W:(bullet[i].y)--; break;
case S:(bullet[i].y)++; break;
case A:(bullet[i].x)--; break;
case D:(bullet[i].x)++; break;
}
}
//判断子弹当前位置情况
if (map[bullet[i].y][bullet[i].x] == 0) //子弹坐标无障碍
Print_B(bullet[i].x, bullet[i].y);//打印
else Break_B(&bullet[i]); //子弹碰撞
for (int j = 0; j < 50; j++)
//子弹间的碰撞判断,若是我方子弹和敌方子弹碰撞则都删除,若为两敌方子弹则无视
if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type)
&& bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y)
{ //同样的两颗我方子弹不可能产生碰撞
bullet[j].Exist = 0;
bullet[i].Exist = 0;
Clear_B(bullet[j].x, bullet[j].y);
break;
}
}
}
}
void Break_B(Bullet* bullet) {
int x = bullet->x;
int y = bullet->y; //子弹坐标
int i;
if (map[y][x] == 1) { //子弹碰到砖块
if (bullet->Direction == A || bullet->Direction == D)
//若子弹是横向的
for (i = -1; i <= 1; i++)
if (map[y + i][x] == 1) {
map[y + i][x] = 0;
GoToxy(2 * x, y + i);
std::cout << " ";
}
if (bullet->Direction == W || bullet->Direction == S) //子弹是向上或是向下移动的
for (i = -1; i <= 1; i++)
if (map[y][x + i] == 1) { //如果子弹打中砖块两旁为砖块,则删除砖,若不是则忽略
map[y][x + i] = 0; //砖块碎
GoToxy(2 * (x + i), y);
std::cout << " ";
}
bullet->Exist = 0; //子弹不存在
}
else if (map[y][x] == 4 || map[y][x] == 6) //子弹碰到边框或者不可摧毁方块
bullet->Exist = 0;
else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子弹碰到了敌方坦克
AI_tank[(int)map[y][x] % 100].Exist = 0;
bullet->Exist = 0;
Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y); //清除坦克
}
else if (bullet->Type == 0 && map[y][x] == 200) { //若敌方子弹击中我的坦克
my_tank.Exist = 0;
bullet->Exist = 0;
Clear_T(my_tank.x, my_tank.y);
my_tank.Revival++; //我方坦克复活次数加1
}
else if (map[y][x] == 9) { //子弹碰到巢
bullet->Exist = 0;
GoToxy(38, 46); std::cout << " ";
GoToxy(38, 47); std::cout << " ";
GoToxy(38, 48); std::cout << "◢◣ ";
GameOver();
}
}
int Cheak_B(int x, int y) {//子弹当前位置情况
if (map[y][x] == 0)
return 1;
else
return 0;
}
void Print_B(int x, int y){
GoToxy(2 * x, y);
std::cout << "o";
}
void Clear_B(int x, int y){
GoToxy(2 * x, y);
if (Cheak_B(x, y) == 1) {//子弹当前坐标在空地上
std::cout << " ";
}
}
void Init() { //初始化
Enemy = 24;
my_tank.Revival = 0; //我的坦克复活次数为0
Pos = 0;
B_num = 0;
Print_Map();
Creat_My_T(&my_tank);
for (int i = 0; i < 50; i++) {//子弹
bullet[i].Exist = 0;
}
for (int i = 0; i <= 5; i++) {//敌方坦克
AI_tank[i].Revival = 0;
AI_tank[i].Exist = 0; //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克
AI_tank[i].Num = i;
AI_tank[i].Type = 0;
}
}
int main() {
int i;
int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; //间隔数组,用于控制速度
HideCursor(); //隐藏光标
Show(); //打印框架
Init(); //初始化
while(1) {
if (gap[0]++ % Speed == 0) {
//速度调整,
Cheak_Game(); //游戏胜负检测
for (i = 0; i <= 5; i++) {//敌方坦克移动循环
if (gap[i + 7]++ % 3 == 0)
Move_AI_T(&AI_tank[i]);
}
for (i = 0; i <= 5; i++)//建立敌方坦克
if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) { //一个敌方坦克每局只有4条命
//坦克死掉后间隔一段时间建立
Creat_AI_T(&AI_tank[i]);
break;
}
for (i = 0; i <= 5; i++)
if (AI_tank[i].Exist)
Creat_AI_B(&AI_tank[i]);
if (my_tank.Exist && gap[14]++ % 2 == 0)
Key();
if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克复活
Creat_My_T(&my_tank);
Move_B(bullet);
}
Sleep(5);
}
return 0;
}
5.贪吃蛇
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
#include<iostream>
using namespace std;
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout<<"按 W S A D 移动方向"<<endl;
gotoxy(N+3,2);
color(20);
cout<<"按任意键暂停"<<endl;
gotoxy(N+3,3);
color(20);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"失败!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}