【C语言小练习】CMD控制台版贪吃蛇

【C语言小练习】CMD控制台版贪吃蛇

 

 

请用VC6或者VS系列编译器编译!

 

#include #include #include #include //--------------------------------------------- //1.全局数据 const unsigned int StageLength = 18; //舞台宽度 const unsigned int StageHeight = 8; //舞台高度 const DWORD GameSpeed = 350; //游戏速度,刷新率 //游戏的字符 const char G_Head = '@'; const char G_Body = '#'; const char G_Food = '*'; const char G_None = '.'; /* GameArr:游戏数组 0:什么都没有; 1:蛇头; 2:蛇身; 3:可以吃的节点 坐标摆放方式: 原点------------->X++ | | | GameArr[Y][X] | V Y++ */ unsigned int GameArr[StageHeight][StageLength] = {0}; char ScreenArr[StageHeight][StageLength] = {0};//屏幕数组 /* SCache:蛇的节点数组 [X][Y] X:蛇的第几个节点。头部是第一个节点。从1开始使用。 Y:[0] / [1] :该节点的坐标 [2] :该节点的方向 0:停止,1:左, 2:右, 3:上, 4:下 */ unsigned int SCache[StageHeight*StageLength][3] = {0}; unsigned int SLength = 0;//蛇的长度 bool SAddNewNode = false; int GameState = 0;//游戏状态 -1:错误,0:未开始, 1:游戏中, 2:输了 unsigned int FoodLocation[2] = {0}; clock_t StartTime = 0;//开始时间 clock_t EndTime = 0;//结束时间 int RandSrand = 0;//随机数种子 //---------------------------------------------- //2.内部数据处理函数 //2.1 初始化,蛇在屏幕中央 void Data_Init( ); //在游戏开始之前,保存初始化状态 void Data_KeepInit( ); //2.2 蛇移动 void Data_SMove( ); //2.3 处理越界 void Data_SMove_isOutofStage( unsigned int posX, unsigned int posY ); //2.4 处理碰撞 void Data_SMove_HitTest( unsigned int SHeadPosX, unsigned int SHeadPosY ); //2.5 处理食物 void Data_SMove_EatFoodTest( unsigned int SHeadPosX, unsigned int SHeadPosY ); //2.6 摆放食物 void Data_SetFood( ); //---------------------------------------------- //3.图形处理函数 //3.1 更新图形缓存 void Graphic_GetNewData( ); //3.2 刷新图形 void Graphic_RefreshScreen( ); //---------------------------------------------- //4.输出处理函数 void Input_Control( ); //5.入口函数 int main( void ) { //1.初始化游戏 Data_Init(); //2.运行游戏 while( true ) { //2.1 检查输入 Input_Control( ); //2.2 执行游戏 switch( GameState ) { case -1: printf( "游戏出错。按任意键退出。/r/n" ); getch(); exit(1); case 0: Data_KeepInit(); break; case 1: Data_SMove(); break; case 2: EndTime = clock(); unsigned int gameTime = ( EndTime - StartTime )/CLOCKS_PER_SEC; printf( "你输了,游戏结束。你的成绩是:/r/n"); printf( "时间:%u秒。长度:%u 。/r/n", gameTime, SLength ); printf( "按任意键退出。/r/n"); getch(); exit(1); } if( (GameState == 0) || (GameState == 1) ) { //2.3 刷新图形 Graphic_GetNewData( ); Graphic_RefreshScreen( ); //2.4 延时 Sleep( GameSpeed ); } } } //---------------------------------------------- //2.内部数据处理函数 //2.1 初始化,蛇在屏幕中央 void Data_Init( ) { system( "title 贪吃蛇 by 蛋疼喵咪 @ : 蛇头 # : 蛇身 * : 食物" ); //1.检查参数 if( StageLength < 5 ) { printf( "舞台长度太小。/r/n"); GameState = -1; return; } if( StageHeight < 5 ) { printf( "舞台长度太小。/r/n"); GameState = -1; return; } //2.得到屏幕中心坐标 unsigned int XCenterPos = 0; unsigned int YCenterPos = 0; XCenterPos = StageLength / 2; YCenterPos = StageHeight / 2; //3.初始化蛇 SLength = 1; SCache[1][0] = YCenterPos; SCache[1][1] = XCenterPos; SCache[1][2] = 0; //4.初始化食物 Data_SetFood(); } //在游戏开始之前,保存初始化状态 void Data_KeepInit( ) { GameArr[ SCache[1][0] - 1 ][ SCache[1][1] - 1 ] = 1; GameArr[ FoodLocation[0]-1 ][ FoodLocation[1]-1 ] = 3; } //2.2 蛇移动 void Data_SMove( ) { unsigned int nextPosX = 0;//蛇头移动后的X坐标 unsigned int nextPosY = 0;//蛇头移动后的Y坐标 unsigned int SDirection = SCache[1][2]; //1.获取预移动坐标 switch( SDirection ) { case 0://不移动 return; case 1://1.1 左移 nextPosY = SCache[1][0]; nextPosX = SCache[1][1] - 1; break; case 2://1.2 右移 nextPosY = SCache[1][0]; nextPosX = SCache[1][1] + 1; break; case 3://1.3 上移 nextPosY = SCache[1][0] - 1; nextPosX = SCache[1][1]; break; case 4://1.4 下移 nextPosY = SCache[1][0] + 1; nextPosX = SCache[1][1]; break; } //2.处理越界 Data_SMove_isOutofStage( nextPosX, nextPosY ); if( GameState == 2 ) { return; } //3.处理碰撞 Data_SMove_HitTest( nextPosX, nextPosY ); if( GameState == 2 ) { return; } //4.处理食物 Data_SMove_EatFoodTest( nextPosX, nextPosY ); //5.获取最后一个节点值,用于增加长度 unsigned int lastNode[2] = {0}; lastNode[0] = SCache[ SLength ][0]; lastNode[1] = SCache[ SLength ][1]; //5.移动 unsigned int i = 0; unsigned int preNodeLastPos[2] = {0}; unsigned int tPos[2] = {0}; preNodeLastPos[0] = SCache[1][0]; preNodeLastPos[1] = SCache[1][1]; //5.1 移动第一个节点 SCache[1][0] = nextPosY; SCache[1][1] = nextPosX; //5.2 处理后续节点 for( i = 2; i<=SLength; i++ ) { tPos[0] = SCache[i][0]; tPos[1] = SCache[i][1]; SCache[i][0] = preNodeLastPos[0]; SCache[i][1] = preNodeLastPos[1]; preNodeLastPos[0] = tPos[0]; preNodeLastPos[1] = tPos[1]; } //6.处理新增加节点:SAddNewNode if( SAddNewNode == true ) { SAddNewNode = false; SLength++; SCache[SLength][0] = lastNode[0]; SCache[SLength][1] = lastNode[1]; } //7.写入蛇的位置 GameArr[ SCache[1][0]-1 ][ SCache[1][1]-1 ] = 1; for( i = 2; i <= SLength; i++ ) { GameArr[ SCache[i][0]-1 ][ SCache[i][1]-1 ] = 2; } } //2.3 处理越界 void Data_SMove_isOutofStage( unsigned int posX, unsigned int posY ) { if( (posX < 1) || (posX > StageLength) ) { printf( "越界!/r/n" ); GameState = 2; } else if( (posY < 1) || (posY > StageHeight) ) { printf( "越界!/r/n" ); GameState = 2; } } //2.4 处理碰撞 void Data_SMove_HitTest( unsigned int SHeadPosX, unsigned int SHeadPosY ) { unsigned int i = 0; for( i = 2; i <= SLength; i++ ) { if( (SHeadPosY == SCache[i][0]) && (SHeadPosX == SCache[i][1]) ) { printf( "撞到自己的第%u个节点了。/r/n", i ); GameState = 2; break; } } } //2.5 处理食物 void Data_SMove_EatFoodTest( unsigned int SHeadPosX, unsigned int SHeadPosY ) { unsigned int i = 0; unsigned int SHeadX = 0; unsigned int SHeadY = 0; unsigned int FoodX = 0; unsigned int FoodY = 0; //1.获取蛇头坐标 SHeadY = SCache[1][0]; SHeadX = SCache[1][1]; //2.获取食物坐标 FoodY = FoodLocation[0]; FoodX = FoodLocation[1]; //3.判断是否吃到 if( (SHeadX == FoodX) && (SHeadY == FoodY) ) { SAddNewNode = true;//设置节点+1 Data_SetFood( );//放置新食物 } else { //放置旧食物 GameArr[ FoodLocation[0]-1 ][ FoodLocation[1]-1 ] = 3; } } //2.6 摆放食物 void Data_SetFood( ) { unsigned int FoodX = 0; unsigned int FoodY = 0; unsigned int i = 0; bool continueSrand = false; //1.取消上一次的位置 if( FoodLocation[0] != 0 ) { GameArr[ FoodLocation[0] - 1 ][ FoodLocation[1] - 1 ] = 0; FoodLocation[0] = 0; FoodLocation[1] = 0; } do { //2.获取随机位置 RandSrand++; srand( time(NULL) + RandSrand ); FoodY = rand() % StageHeight + 1; FoodX = rand() % StageLength + 1; //3.测试这个位置是否可用 continueSrand = false; for( i = 1; i <= SLength; i++ ) { if( (SCache[i][0] == FoodY) && (SCache[i][1] == FoodX) ) { continueSrand = true; } } }while( continueSrand ); //4.设置食物位置 FoodLocation[0] = FoodY; FoodLocation[1] = FoodX; GameArr[ FoodLocation[0] - 1 ][ FoodLocation[1] - 1 ] = 3; } //---------------------------------------------- //3.图形处理函数 //3.1 更新图形缓存,同时清空GameArr void Graphic_GetNewData( ) { unsigned int XI = 0; unsigned int YI = 0; for( YI = 0; YI < StageHeight; YI++ ) { for( XI = 0; XI < StageLength; XI++ ) { switch( GameArr[YI][XI] ) { case 0: ScreenArr[YI][XI] = G_None; break; case 1: ScreenArr[YI][XI] = G_Head; break; case 2: ScreenArr[YI][XI] = G_Body; break; case 3: ScreenArr[YI][XI] = G_Food; break; } GameArr[YI][XI] = 0; } } } //3.2 刷新图形 void Graphic_RefreshScreen( ) { unsigned int XI = 0; unsigned int YI = 0; system( "CLS" ); for( YI = 0; YI < StageHeight; YI++ ) { for( XI = 0; XI < StageLength; XI++ ) { printf( "%c", ScreenArr[YI][XI] ); } printf( "/r/n" ); } //输出当前成绩 if( GameState == 1 ) { EndTime = clock(); } unsigned int gameTime = ( EndTime - StartTime )/CLOCKS_PER_SEC; printf( "/r/n当前成绩:时间:%u秒。长度:%u 。/r/n/r/n", gameTime, SLength ); if( GameState == 0 ) { printf( "请按方向键,开始游戏。/r/n"); } } //---------------------------------------------- //4.输出处理函数 void Input_Control( ) { char inputChar[2] = {0}; unsigned int inputNum = 0; while( true ) { if( kbhit() != 0 ) { if( inputNum == 2 ) { inputNum = 0; } inputNum++; inputChar[ inputNum-1 ] = getch(); } else { break; } } if( inputNum == 2 ) { if( inputChar[0] == -32 ) { if( GameState == 0 ) { GameState = 1; StartTime = clock(); } switch( inputChar[1] ) { //不能往返走 case 75://左 if( (SLength > 1) && (SCache[1][2] != 2) ) { SCache[1][2] = 1; } else { SCache[1][2] = 1; } break; case 77://右 if( (SLength > 1) && (SCache[1][2] != 1) ) { SCache[1][2] = 2; } else { SCache[1][2] = 2; } break; case 72://上 if( (SLength > 1) && (SCache[1][2] != 4) ) { SCache[1][2] = 3; } else { SCache[1][2] = 3; } break; case 80://下 if( (SLength > 1) && (SCache[1][2] != 3) ) { SCache[1][2] = 4; } else { SCache[1][2] = 4; } break; } } } }

 

 

By 蛋疼喵咪(xxMix)

你可能感兴趣的:(【C语言小练习】CMD控制台版贪吃蛇)