【2017-2-11】贪吃蛇C++

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<conio.h>
using namespace std;
#define Size 22
#define Max_snake_length 400
typedef struct{
int x,y;
}Point;


char map[Size][Size];
Point snake[Max_snake_length],food,Next;   //定义蛇,食物,下一步蛇头的位置的变量
int head,tail;                             //在snake[]中的蛇头,蛇尾的下标
int grade,length,autotime;                 //游戏难度等级,蛇长度,蛇前进时间间隔
char direction;
double start_time=clock()/CLOCKS_PER_SEC;


inline void Update(char map[][Size],int grade,int length,int autotime){
//地图刷新
//inline内联函数,可以减少程序运行时间和内存开销
system("cls");
double now_time=clock()/CLOCKS_PER_SEC;
int i,j;
printf("\n");
for(i=0;i<Size;i++){
printf("\n");
for(j=0;j<Size;j++)printf("%c ",map[i][j]);
if(i==0) printf("\t等级为: %d",grade);
if(i==2) printf("\t长度为: %d",length);
if(i==6) printf("\t游戏进行时间: %f 秒",(double)(now_time-start_time));
if(i==8) printf("\t前进间隔为: %d ms",autotime);
printf("\n");
}
}


//欢迎界面
inline void hello(){
    system("cls");
puts("\n\n\n\t\t\t贪吃蛇即将开始!");
double start;
for(int i=3;i>=0;i--){
start=(double)clock()/CLOCKS_PER_SEC;
while((double)clock()/CLOCKS_PER_SEC-start<=1);  //经过1秒以后
if(i>0){
system("cls");
printf("\n\n\n\t\t\t进入倒计时: %d\n",i);  //倒计时
}else{
Update(map,grade,length,autotime);
}
}
}
//随机生成食物位置
inline void create_food(){
srand(int(time(0)));        //种子函数,后面生成随机数
do{
food.x=rand()%20+1;
food.y=rand()%20+1;
}while(map[food.x][food.y]!=' ');    //该位置不为空则可以生成食物
map[food.x][food.y]='!';
}
//初始化
inline void init(){
int i,j;
for(i=1;i<=20;i++){
for(j=1;j<=20;j++){
map[i][j]=' ';
}
}
for(i=0;i<=21;i++){
map[0][i]=map[21][i]=map[i][0]=map[i][21]='*';   //地图边界
}
map[1][1]=map[1][2]='0';      //蛇的出生点,蛇身的位置
map[1][3]='@';
head=2;                       //蛇头位置在snake[]中的下标
tail=0;  //蛇尾在snake[]下标
snake[head].x=1;              //蛇出生点的头尾的X,Y坐标
snake[head].y=3;
snake[tail].x=1;
snake[tail].y=1;
snake[1].x=1;    //出生点蛇头,尾的X,Y坐标
snake[1].y=2;
create_food();
grade=1;length=3;autotime=500;
direction=77;                 //初始时方向
}
//预前进
inline int GO(){
bool timeover=true;
double start=(double)clock()/CLOCKS_PER_SEC;         //获取一个时间戳
//自动经过1秒,或等待1秒内的键盘输入
while((timeover=((double)clock()/CLOCKS_PER_SEC-start<=autotime/1000.0))&&!_kbhit());
if(timeover){   //键盘输入
_getch();
direction=_getch();    //获取输入的方向
}
switch(direction){
case 72:             //上方向键,ASCII码为72
Next.x=snake[head].x-1;
Next.y=snake[head].y;
break;
case 80: //下方向键,ASCII码为80
Next.x=snake[head].x+1;
Next.y=snake[head].y;
break;
case 75:            //左方向键,ASCII码为75
Next.x=snake[head].x;
Next.y=snake[head].y-1;
break;
case 77: //右方向键,ASCII码为77
Next.x=snake[head].x;
Next.y=snake[head].y+1;
break;
default:
puts("\tGame over!");
return 0;
}
//触碰边界
if(Next.x==0||Next.x==21||Next.y==0||Next.y==21){
puts("\tGame over!");
return 0;
}
//吃到自己
if(map[Next.x][Next.y]!=' '&&!(Next.x==food.x&&Next.y==food.y)){
puts("\tGame over!");
return 0;
}
//吃到最大长度,显示通关
if(length==Max_snake_length){
puts("\tGame over!");
return 0;
}
return 1;
}
//成功吃到食物
inline void eating_food(){
length++;
int _grade=length/10+1;
if(_grade!=grade){
grade=_grade;
if(autotime>=100){
autotime=550-grade*50;        //游戏难度每升一级,前进间隔就缩减50ms
}
}
map[Next.x][Next.y]='@';              //蛇头位置移动到下一格
map[snake[head].x][snake[head].y]='O';    //原来蛇头处变为蛇身
head=(head+1)%400;            //蛇头下标加1
snake[head].x=Next.x;         //蛇头在snake[]中的下标变化
snake[head].y=Next.y;
create_food();
Update(map,grade,length,autotime);    //刷新地图
}
//没有吃到食物
inline void eating_failed(){
map[snake[tail].x][snake[tail].y]=' ';   //蛇尾原来的位置变成空格
tail=(tail+1)%Max_snake_length;          //蛇尾下标加1
map[Next.x][Next.y]='@';                 //显示蛇头位置
map[snake[head].x][snake[head].y]='O';     //原蛇头位置变化为蛇身
head=(head+1)%Max_snake_length;          //蛇头下标加1
snake[head].x=Next.x;                    //蛇头下标变化
snake[head].y=Next.y;
Update(map,grade,length,autotime);       //刷新地图
}


int main(){
start_pos:    init();
    hello();
char ch='Y';   //是否继续?Y/N
while(ch=='Y'){
if(GO()){
if(Next.x==food.x&&Next.y==food.y){
eating_food();
}else{
eating_failed();
}
}else{
puts("\n\t继续游戏?(Y/N)");
cin>>ch;
if(ch=='Y')goto start_pos;
else return 0;
}
}
system("pause");
return 0;

}


运行截图:


猜你喜欢

转载自blog.csdn.net/verylonglongago/article/details/54997820