爱收集资源网

C语言推箱子小游戏代码酣畅淋漓,不到300行

网络整理 2023-10-27 15:03

好的,下边是一份带有注释和流程图的C语言推袋子小游戏,代码共计约300行。首先是游戏界面和规则的介绍:```c/**推袋子小游戏**游戏规则:*1.玩家通过联通可以将袋子推到目标点;*2.玩家不能促进袋子穿过墙;*3.当所有袋子都被推到目标点时,游戏胜利;*4.玩家可以通过菜单选择不同的关卡。**控制说明:*1.上下左右键控制玩家联通;*2.空格键控制玩家引领袋子;*3.ESC键退出游戏。**开发环境:VisualStudio2019*编译命令:gccmain.c-omain-Wall**作者:CSDN-开发者头条*时间:2021年9月*/#include#include#include//Windows下的控制台输入输出库#include//Windows下的API库```接出来是一些常量和数据结构的定义推箱子c语言程序,包括地图和玩家的座标,以及菜单和游戏提示的字符串。```c//地图大小#defineROW10#defineCOL16//地图元素定义#defineWALL1#defineBOX2#defineTARGET3//菜单选项#defineMENU_ITEMS3#defineMENU_LEVEL0#defineMENU_HELP1#defineMENU_QUIT2//游戏状态定义#defineGAME_MENU0#defineGAME_PLAYING1#defineGAME_WIN2#defineGAME_OVER3//游戏提示字符串#defineTIP_MENU"1.选择关卡2.操作说明3.退出游戏"#defineTIP_LEVEL"请选择关卡:1.简单2.中等3.困难"#defineTIP_HELP"使用方向键联通,空格键推袋子,ESC键返回"#defineTIP_WIN"恭喜你博得了胜利!"#defineTIP_OVER"很遗憾游戏失败了,按ESC键返回菜单"#defineTIP_ERROR"输入错误推箱子c语言程序,请重新输入!"//关卡地图定义constintlevels[3][ROW][COL]={{//简单{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1},{1,0,2,2,2,2,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}},{//中等{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1},{1,0,2,2,2,2,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}},{//困难{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1},{1,0,2,2,2,2,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}};```接出来是函数申明和主函数体:```c//函数申明voidinit_console();//初始化控制台voidprint_map(intmap[ROW][COL]);//复印地图voidgotoxy(intx,inty);//光标定位voidclear_input();//清空输入缓冲区voidshow_menu();//显示菜单voidshow_level_menu();//显示关卡选择菜单voidshow_help();//显示操作帮助voidshow_tip(constchar*tip);//显示提示信息voidplay_level(intmap[ROW][COL]);//开始游戏intcheck_win(intmap[ROW][COL]);//检测是否胜利intcan_move(intmap[ROW][COL],introw,intcol,intdir_row,intdir_col);//检测是否可联通intmove_box(intmap[ROW][COL],introw,intcol,intdir_row,intdir_col);//联通袋子voidmove_player(intmap[ROW][COL],int*row,int*col,intdir_row,intdir_col);//联通玩家voidplay_game();//游戏主循环//主函数intmain(){init_console();play_game();return0;}```以下是各个函数的实现。

初始化控制台,设置控制台窗口大小和字体大小,以及隐藏控制台光标。```c//初始化控制台voidinit_console(){HANDLEhOut=GetStdHandle(STD_OUTPUT_HANDLE);COORDsize={COL*2,ROW+1};SMALL_RECTrc={0,0,size.X-1,size.Y-1};SetConsoleWindowInfo(hOut,TRUE,&rc);SetConsoleScreenBufferSize(hOut,size);CONSOLE_FONT_INFOEXinfo={sizeof(CONSOLE_FONT_INFOEX)};info.dwFontSize.Y=16;wcscpy_s(info.FaceName,L"Consolas");SetCurrentConsoleFontEx(hOut,FALSE,&info);CONSOLE_CURSOR_INFOcursor_info={sizeof(CONSOLE_CURSOR_INFO),FALSE};SetConsoleCursorInfo(hOut,&cursor_info);}```复印地图,依据不同的元素类型复印不同的字符。

```c//复印地图voidprint_map(intmap[ROW][COL]){inti,j;for(i=0;i

```c//显示菜单voidshow_menu(){intselected=0;//当前选中项intch=0;//输入字符constchar*menu_items[MENU_ITEMS]={TIP_LEVEL,TIP_HELP,"3.退出游戏"};while(1){//显示菜单项inti;for(i=0;i");}else{printf("");}printf("%s\n",menu_items[i]);}//处理输入ch=_getch();switch(ch){case72://方向键上selected--;if(selected<0){selected=MENU_ITEMS-1;}break;case80://方向键下selected++;if(selected>=MENU_ITEMS){

推箱子c语言程序
相关文章