贪吃蛇小黑框版本0.01

#include <stdio.h>
#include <conio.h>
#include <Windows.h>
int MusePointer = 5,b= 33;
char ch = 0;
bool MuseExit =true;
extern void Snake_Game();
void  Menu_Display()
{
printf("制作时间 : 2016年12月25日\n");
printf("作    者 : explorers\n");
printf("版    本 : 1.00\n");
for(int i = 0;i < 20;i++)
{
for (int i1 = 0;i1<37;i1++)
{
if (MusePointer == i&&i1 ==30)b--;
if(i1 == 29&&i ==MusePointer)printf("%c",16);
if(i1 == 35&&i == MusePointer)printf("%c",17);
if (i1 == b&&i == 5)printf("游 戏 开 始");
if (i1 == b&&i == 7)printf("读 取 存 档");
if (i1 == b&&i == 9)printf("选 择 难 度");
if (i1 == b&&i == 11)printf("成 绩 榜 单");
if (i1 == b&&i == 13)printf("退 出 游 戏");
if (MusePointer == i&&i1 ==36)b++;
else printf(" ");
}
printf("\n");
}
}
void Muse_Input()
{
while(1)
{
if(kbhit())
{
ch = getch();
if(ch==72)
{
if (MusePointer == 5)
{
MusePointer = 13;
}
else MusePointer-=2;
}
if(ch==80)
{
if (MusePointer == 13)
{
MusePointer = 5;
}
else MusePointer+=2;
}
system("cls");
Menu_Display();
if (ch ==13)
break;
}
}
}

  1.  

int main ()
{
system("color 0A");
while (MuseExit)
{
Menu_Display();
Muse_Input();
if (MusePointer == 5){system("cls");Snake_Game();};
if (MusePointer == 7){system("cls");printf("还没学会建立文件夹,等我学成过来...");Sleep(600);system("cls");Snake_Game();};
if (MusePointer == 9){system("cls");printf("还没学会建立文件夹,等我学成过来...");Sleep(600);system("cls");Snake_Game();};
if (MusePointer == 11){system("cls");printf("还没学会建立文件夹,等我学成过来...");Sleep(600);system("cls");Snake_Game();};
if (MusePointer == 13) MuseExit = false;
}
return 0;

}

#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <time.h>
struct Snake_Body
{
int a2;
int b2;
Snake_Body* next;
};
Snake_Body m_head ={0,0,NULL};
class Gamestop
{
private:
int GamePointer;int gameconst;char ch1;bool stop;
public:
Gamestop()
{
GamePointer = 7;gameconst =33;ch1 = 0;stop = true;
}
void Game_stop()
{
for(int i = 0;i < 20;i++)//游戏暂停菜单
{
for (int i1 = 0;i1<37;i1++)
{
if (GamePointer == i&&i1 ==30)gameconst--;
if(i1 == 29&&i ==GamePointer)printf("%c",16);
if(i1 == 35&&i == GamePointer)printf("%c",17);
if (i1 == gameconst&&i == 7)printf("继 续 游 戏");
if (i1 == gameconst&&i == 9)printf("游 戏 存 档");
if (i1 == gameconst&&i == 11)printf("退 出 游 戏");
if (GamePointer == i&&i1 ==36)gameconst++;
else printf(" ");
}
printf("\n");
}
};
void Game_Input()//打印暂停菜单
{
while(stop)
{
while(kbhit())
{
ch1 = getch();
if(ch1==72){if (GamePointer == 7)GamePointer = 11;else GamePointer-=2;}
if(ch1==80){if (GamePointer == 11)GamePointer = 7;else GamePointer+=2;}
system("cls");Game_stop();
if (ch1 ==13)stop =false;
}
}
}
int Gamestop_Muse()
{
Game_stop();
Game_Input();
if (GamePointer == 7){system("cls");return 0;};//选择继续游戏
if (GamePointer == 9){system("cls"); printf("还没学会建立文件夹,等我学成过来...");return 0;};//选择保存信息
if (GamePointer == 11){system("cls");return 1;};//退出游戏
return 0;
}


};


class Snake
{
private:
int a;int b;char map[20][30];int level;int socre;int time1;int time2;int times;bool over;char ch;char ch1;int spend;
public:
//初始化数据
Snake()
{
a = 1;b = 1;level = 1;socre = 1;time1=0;;time2 = 0;ch = 's';ch1 = 's';over = false;spend =1000;
for (int i = 0;i<20;i++)//初始化地图
{
for (int i1 = 0;i1<30;i1++)
{
if (i==0||i==19)map[i][i1] = 1;
else if(i1==0||i1==29)map[i][i1] = 2;
else 
map[i][i1] = 0;
}
}
map[1][1]=5;
};
//打印地图
void print()
{
for (int i=0;i<20;i++)
{
for (int i1=0;i1<30;i1++)
{
if (map[i][i1]==0)printf("  ");//活动范围
if (map[i][i1]==1)printf("- ");//边界
if (map[i][i1]==2)printf("| ");
if (map[i][i1]==3)printf("* ");//豆子显示
if (map[i][i1]==4)printf("%c ",4);//蛇身显示
if (map[i][i1]==5)printf("# ");//蛇头显示


}
if(i == 3)printf("统 计 信 息");//游戏相关信息
if(i == 7)printf("等 级  :    %d",level);//等级
if(i == 9)printf("分 数  :    %d",socre-1);//吃了多少豆子
if(i == 11)printf("时 间  :%02d:%02d",time2,time1);//运行时间
if(i == 15)printf("空格键  暂 停");
if(i == 17)printf("ESC 键  退 出");
printf("\n");
}
}
//放出一个豆子
void food()
{
if(socre==1)times=clock();
int a1,b1;
do 
{
srand(time(NULL));
a1 = 1+rand()%19;
b1 = 1+rand()%29;
} while (map[a1][b1] != 0);
map[a1][b1] = 3;
}


//判断蛇移动方向
void moves()
{
while(!over)
{
int now = clock();
while(clock()-now<spend){if(kbhit()){ch = getch();if(isascii(ch))continue;ch = getch();break;};};
if(ch == 'w'||ch=='W'||ch == 72){int x=a-1;int y=b;EatFood(x,y);ch1 = ch;};
if(ch == 's'||ch=='S'||ch == 80){int x=a+1;int y=b;EatFood(x,y);ch1 = ch;};
if(ch == 'a'||ch=='A'||ch == 75){int x=a;int y=b-1;EatFood(x,y);ch1 = ch;};
if(ch == 'd'||ch=='D'||ch == 77){int x=a;int y=b+1;EatFood(x,y);ch1 = ch;};
if(ch == ' '){system("cls");Gamestop a;if(a.Gamestop_Muse()){over = true;break;}ch = ch1;};
if(ch == '\033'){over = true;system("cls");ch = ch1;};
if (over)
{
system("cls");
printf("\n\n\n\n\n\n\n\n\n\t\t\t   G A M E  O V E R ! ! ! !");
Sleep(900);
system("cls");
}
}

}
//吃豆子判断
void EatFood(int x2,int y2)
{
if((map[x2][y2]==4||map[x2][y2]==5)||(map[x2][y2]==1||map[x2][y2]==2))over =true; //吃到自己游戏结束

else
{
Snake_Body* obj1 = (Snake_Body*)malloc (sizeof(Snake_Body));
obj1->a2 = x2;
obj1->b2 = y2;
obj1->next = m_head.next;
m_head.next = obj1;
time1=(clock()-times)/1000;
if(time1>60)
{
time2=time1/60;
time1=time1%60;
}
if(map[x2][y2]==3)
{
socre++;
map[a][b]=4;
map[x2][y2]=5;
food();
if(socre%8==0)
level++;
spend-=10;


}
else
{
map[a][b]=4;
map[x2][y2]=5;
Snake_Body* p= &m_head;
for(int i = 0;i<=socre;i++)
{
p = p->next;
}
map[p->a2][p->b2] = 0;
free(p);
}
a=x2;
b=y2;
system("cls");
print();
}
}
};


void Snake_Game()//开始接口
{
system("color 0A");
Snake bejin;//放入蛇头
Snake_Body* obj1 = (Snake_Body*)malloc (sizeof(Snake_Body));
obj1->a2 = 1;
obj1->b2 = 1;
obj1->next = m_head.next;
m_head.next = obj1;
bejin.food();
bejin.print();
bejin.moves();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值