C語言鍵盤控制走迷宮小游戲
本文實(shí)例為大家分享了C語言鍵盤控制走迷宮小游戲的具體代碼,供大家參考,具體內(nèi)容如下
在看了《啊哈C語言》之后想寫一個(gè)游戲demo
游戲的截圖
首先是啟動(dòng)界面
然后是初始化
接下來是鍵盤操控
地圖的復(fù)雜度也很容易修改。
也支持退出。按s鍵選擇退出游戲這個(gè)選項(xiàng)即可。
下面是源代碼
#include <stdio.h> #include <stdlib.h> void startUp(); void gameInstructions(); void menu(char c); void gameMap(); char map[6][6]={"######", "#o # ", "# ## #", "# # #", "## #", "######" }; int main() { startUp();//游戲啟動(dòng)界面 gameInstructions();//游戲說明 //顯示菜單 printf(" -> 開始游戲\n"); printf(" 退出游戲\n"); printf("按Enter進(jìn)入游戲\n"); char choose1; choose1 = getch(); menu(choose1); //加載游戲地圖 gameMap(); //玩家進(jìn)行操作 system("cls"); int i,x,y,p,q,j; char ch; x=1;y=1;p=1;q=5; for(i=0;i<6;++i){ printf(" "); for(j=0;j<6;++j){ printf("%c ",map[i][j]); } printf("\n"); } while(x != p || y != q){ ch = getch(); if(ch == 's' || ch == 'S'){ if(map[x+1][y] != '#'){ map[x][y] = ' '; x++; map[x][y] = 'o'; }else{ printf("\a"); } } if(ch == 'w' || ch == 'W'){ if(map[x-1][y] != '#'){ map[x][y] = ' '; x--; map[x][y] = 'o'; }else{ printf("\a"); } } if(ch == 'a' || ch == 'A'){ if(map[x][y-1] != '#'){ map[x][y] = ' '; y--; map[x][y] = 'o'; }else{ printf("\a"); } } if(ch == 'd' || ch == 'D'){ if(map[x][y+1] != '#'){ map[x][y] = ' '; y++; map[x][y] = 'o'; }else{ printf("\a"); } } system("cls"); for(i=0;i<6;++i){ printf(" "); for(j=0;j<6;++j){ printf("%c ",map[i][j]); } printf("\n"); } } system("cls"); printf("You Win!\n"); Sleep(3000); return 0; } void startUp(){ int i; int circle = 2; for(i=0;i<circle;++i){ system("cls"); printf(" 游戲正在加載"); Sleep(500); system("cls"); printf(" 游戲正在加載."); Sleep(500); system("cls"); printf(" 游戲正在加載.."); Sleep(500); system("cls"); printf(" 游戲正在加載..."); Sleep(500); } system("cls"); printf(" 游戲加載完畢!\n"); Sleep(1000); } void gameInstructions(){ printf("<---操作按鍵:[W][S][A][D]--->\n"); printf("<---分別對應(yīng)[上][下][左][右]--->\n"); printf("<---注意使用英文輸入法--->\n"); Sleep(1000); } void menu(char c){ system("cls"); if(c == 'S' || c == 's'){ printf(" 游戲加載完畢!\n"); printf("<---操作按鍵:[W][S][A][D]--->\n"); printf("<---分別對應(yīng)[上][下][左][右]--->\n"); printf(" 開始游戲\n"); printf(" -> 退出游戲\n"); printf("程序?qū)⒃?s后關(guān)閉\n"); int i = 0; for(i=0;i<5;++i){ system("cls"); printf("程序?qū)⒃?%ds后關(guān)閉\n",5-i); Sleep(1000); } exit(0);//退出程序 }else{ printf(" 游戲加載完畢!\n"); printf("<---操作按鍵:[W][S][A][D]--->\n"); printf("<---分別對應(yīng)[上][下][左][右]--->\n"); printf(" -> 開始游戲\n"); printf(" 退出游戲\n"); printf("按Enter進(jìn)入游戲\n"); } } void gameMap(){ system("cls"); printf("---正在加載游戲地圖---\n"); int i, j; for(i=0;i<6;++i){ printf(" "); for(j=0;j<6;++j){ printf("%c ",map[i][j]); Sleep(100); } printf("\n"); Sleep(200); } system("cls"); printf("---Part1---\n"); for(i=0;i<6;++i){ printf(" "); for(j=0;j<6;++j){ printf("%c ",map[i][j]); } printf("\n"); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++使用TinyXml實(shí)現(xiàn)讀取XMl文件
常見C/C++?XML解析器有Tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,本文為大家介紹的是使用TinyXml實(shí)現(xiàn)讀取XMl文件,需要的可以參考一下2023-06-06C++實(shí)現(xiàn)LeetCode(57.插入?yún)^(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(57.插入?yún)^(qū)間),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++字符串拼接效率對比(+=、append、stringstream、sprintf)
這篇文章主要介紹了C++字符串拼接效率對比(+=、append、stringstream、sprintf),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08C語言循環(huán)隊(duì)列的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語言循環(huán)隊(duì)列的表示與實(shí)現(xiàn),對于數(shù)據(jù)結(jié)構(gòu)與算法的研究很有幫助,需要的朋友可以參考下2014-07-07