C語(yǔ)言制作貪吃蛇小游戲
本文實(shí)例為大家分享了C語(yǔ)言制作貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼
?#include <stdio.h> #include <stdlib.h> #include <math.h> #include <conio.h> #include <time.h> #include <windows.h> ? //MAXWIDTH、MAXHEIGHT、INITLEN 以字符記 #define MAXWIDTH (30) #define MAXHEIGHT MAXWIDTH #define INITLEN (3) ?//貪吃蛇的初始長(zhǎng)度? ? //程序中用到的各種字符,以及它們的顏色和類型(以數(shù)字表示) struct{ ? ? char *ch; ? ? int color; ? ? char type; } charBorder = {"□", 4, 1}, ?//邊框 charBg = {"■", 2, 2}, ?//背景 charSnake = {"★", 0xe, 3}, ?//貪吃蛇節(jié)點(diǎn) charFood = {"●", 0xc, 4}; ?//食物 ? //用一個(gè)結(jié)構(gòu)體數(shù)組保存地圖中的各個(gè)點(diǎn) struct{ ? ? char type; ? ? int index; }globalMap[MAXWIDTH][MAXHEIGHT]; ? //貪吃蛇有效活動(dòng)范圍地圖的索引 struct{ ? ? int x; ? ? int y; } snakeMap[ (MAXWIDTH-2)*(MAXHEIGHT-2) ], scoresPostion; ? int scores = 0; ?//得分 int snakeMapLen = (MAXWIDTH-2)*(MAXHEIGHT-2); int headerIndex, tailIndex; ?//蛇頭蛇尾對(duì)應(yīng)的snakeMap中的索引(下標(biāo)) HANDLE hStdin; ?//控制臺(tái)句柄 ? // 設(shè)置光標(biāo)位置,x為行,y為列 void setPosition(int x, int y){ ? ? COORD coord; ? ? coord.X = 2*y; ? ? coord.Y = x; ? ? SetConsoleCursorPosition(hStdin, coord); } ? // 設(shè)置顏色 void setColor(int color){ ? ? SetConsoleTextAttribute(hStdin, color); } ? //創(chuàng)建食物 void createFood(){ ? ? int index, rang, x, y; ? ? //產(chǎn)生隨機(jī)數(shù),確定 snakeMap 數(shù)組的索引? ? ? srand((unsigned)time(NULL)); ? ? if(tailIndex<headerIndex){ ? ? ? ? rang = headerIndex-tailIndex-1; ? ? ? ? index = rand()%rang + tailIndex + 1; ? ? }else{ ? ? ? ? rang = snakeMapLen - (tailIndex - headerIndex+1); ? ? ? ? index = rand()%rang; ? ? ? ? if(index>=headerIndex){ ? ? ? ? ? ? index += (tailIndex-headerIndex+1); ? ? ? ? } ? ? } ? ? ? ? ? x = snakeMap[index].x; ? ? y = snakeMap[index].y; ? ? setPosition(x, y); ? ? setColor(charFood.color); ? ? printf("%s", charFood.ch); ? ? globalMap[x][y].type=charFood.type; } ? //死掉 void die(){ ? ? int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1; ? ? int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1; ? ? ? setPosition(xCenter, yCenter-5); ? ? setColor(0xC); ? ? ? printf("You die! Game Over!"); ? ? getch(); ? ? exit(0); } ? // 蛇移動(dòng) void move(char direction){ ? ? int newHeaderX, newHeaderY; ?//新蛇頭的坐標(biāo) ? ? int newHeaderPreIndex; ?//新蛇頭坐標(biāo)以前對(duì)應(yīng)的索引 ? ? int newHeaderPreX, newHeaderPreY; ?//新蛇頭的索引以前對(duì)應(yīng)的坐標(biāo) ? ? int newHeaderPreType; ?//新蛇頭以前的類型 ? ? int oldTailX, oldTailY; ?//老蛇尾坐標(biāo) ? ? // ----------------------------------------------- ? ? //新蛇頭的坐標(biāo) ? ? switch(direction){ ? ? ? ? case 'w': ? ? ? ? ? ? newHeaderX = snakeMap[headerIndex].x-1; ? ? ? ? ? ? newHeaderY = snakeMap[headerIndex].y; ? ? ? ? ? ? break; ? ? ? ? case 's': ? ? ? ? ? ? newHeaderX = snakeMap[headerIndex].x+1; ? ? ? ? ? ? newHeaderY = snakeMap[headerIndex].y; ? ? ? ? ? ? break; ? ? ? ? case 'a': ? ? ? ? ? ? newHeaderX = snakeMap[headerIndex].x; ? ? ? ? ? ? newHeaderY = snakeMap[headerIndex].y-1; ? ? ? ? ? ? break; ? ? ? ? case 'd': ? ? ? ? ? ? newHeaderX = snakeMap[headerIndex].x; ? ? ? ? ? ? newHeaderY = snakeMap[headerIndex].y+1; ? ? ? ? ? ? break; ? ? } ? ? //新蛇頭的索引 ? ? headerIndex = headerIndex==0 ? snakeMapLen-1 : headerIndex-1; ? ? // ----------------------------------------------- ? ? //新蛇頭坐標(biāo)以前對(duì)應(yīng)的索引 ? ? newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index; ? ? //新蛇頭的索引以前對(duì)應(yīng)的坐標(biāo) ? ? newHeaderPreX = snakeMap[headerIndex].x; ? ? newHeaderPreY = snakeMap[headerIndex].y; ? ? ? //雙向綁定新蛇頭索引與坐標(biāo)的對(duì)應(yīng)關(guān)系 ? ? snakeMap[headerIndex].x = newHeaderX; ? ? snakeMap[headerIndex].y = newHeaderY; ? ? globalMap[newHeaderX][newHeaderY].index = headerIndex; ? ? ? //雙向綁定以前的索引與坐標(biāo)的對(duì)應(yīng)關(guān)系 ? ? snakeMap[newHeaderPreIndex].x = newHeaderPreX; ? ? snakeMap[newHeaderPreIndex].y = newHeaderPreY; ? ? globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex; ? ? ? //新蛇頭以前的類型 ? ? newHeaderPreType = globalMap[newHeaderX][newHeaderY].type; ? ? //設(shè)置新蛇頭類型 ? ? globalMap[newHeaderX][newHeaderY].type = charSnake.type; ? ? // 判斷是否出界或撞到自己 ? ? if(newHeaderPreType==charBorder.type || newHeaderPreType==charSnake.type ){ ? ? ? ? die(); ? ? } ? ? //輸出新蛇頭 ? ? setPosition(newHeaderX, newHeaderY); ? ? setColor(charSnake.color); ? ? printf("%s", charSnake.ch); ? ? //判斷是否吃到食物 ? ? if(newHeaderPreType==charFood.type){ ?//吃到食物 ? ? ? ? createFood(); ? ? ? ? //更改分?jǐn)?shù) ? ? ? ? setPosition(scoresPostion.x, scoresPostion.y); ? ? ? ? printf("%d", ++scores); ? ? }else{ ? ? ? ? //老蛇尾坐標(biāo) ? ? ? ? oldTailX = snakeMap[tailIndex].x; ? ? ? ? oldTailY = snakeMap[tailIndex].y; ? ? ? ? //刪除蛇尾 ? ? ? ? setPosition(oldTailX, oldTailY); ? ? ? ? setColor(charBg.color); ? ? ? ? printf("%s", charBg.ch); ? ? ? ? globalMap[oldTailX][oldTailY].type = charBg.type; ? ? ? ? tailIndex = (tailIndex==0) ? snakeMapLen-1 : tailIndex-1; ? ? } } ? //下次移動(dòng)的方向 char nextDirection(char ch, char directionOld){ ? ? int sum = ch+directionOld; ? ? ch = tolower(ch); ? ? if( (ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum!=197 && sum!=234 ){ ? ? ? ? return ch; ? ? }else{ ? ? ? ? return directionOld; ? ? } } ? //暫停 char pause(){ ? ? return getch(); } ? // 初始化 void init(){ ? ? // 設(shè)置相關(guān)變量? ? ? int x, y, i, index; ? ? int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1; ? ? int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1; ? ? CONSOLE_CURSOR_INFO cci; ?//控制臺(tái)光標(biāo)信息 ? ? ? //判斷相關(guān)設(shè)置是否合理 ? ? if(MAXWIDTH<16){ ? ? ? ? printf("'MAXWIDTH' is too small!"); ? ? ? ? getch(); ? ? ? ? exit(0); ? ? } ? ? ? //設(shè)置窗口大小? ? ? system("mode con: cols=96 lines=32"); ? ? ? //隱藏光標(biāo) ? ? hStdin = GetStdHandle(STD_OUTPUT_HANDLE); ? ? GetConsoleCursorInfo(hStdin, &cci); ? ? cci.bVisible = 0; ? ? SetConsoleCursorInfo(hStdin, &cci); ? ? ? ? ? //打印背景? ? ? for(x=0; x<MAXHEIGHT; x++){ ? ? ? ? for(y=0; y<MAXWIDTH; y++){ ? ? ? ? ? ? if(y==0 || y==MAXWIDTH-1 || x==0 || x==MAXHEIGHT-1){ ? ? ? ? ? ? ? ? globalMap[x][y].type = charBorder.type; ? ? ? ? ? ? ? ? setColor(charBorder.color); ? ? ? ? ? ? ? ? printf("%s", charBorder.ch); ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? index = (x-1)*(MAXWIDTH-2)+(y-1); ? ? ? ? ? ? ? ? snakeMap[index].x= x; ? ? ? ? ? ? ? ? snakeMap[index].y= y; ? ? ? ? ? ? ? ? globalMap[x][y].type = charBg.type; ? ? ? ? ? ? ? ? globalMap[x][y].index = index; ? ? ? ? ? ? ? ? ? setColor(charBg.color); ? ? ? ? ? ? ? ? printf("%s", charBg.ch); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? printf("\n"); ? ? } ? ? ? ? ? //初始化貪吃蛇 ? ? globalMap[xCenter][yCenter-1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter+1].type = charSnake.type; ? ? ? headerIndex = (xCenter-1)*(MAXWIDTH-2)+(yCenter-1) - 1; ? ? tailIndex = headerIndex+2; ? ? setPosition(xCenter, yCenter-1); ? ? setColor(charSnake.color); ? ? for(y = yCenter-1; y<=yCenter+1; y++){ ? ? ? ? printf("%s", charSnake.ch); ? ? } ? ? //生成食物 ? ? createFood(); ? ? ? //設(shè)置程序信息 ? ? setPosition(xCenter-1, MAXWIDTH+2); ? ? printf(" ? Apples : 0"); ? ? setPosition(xCenter, MAXWIDTH+2); ? ? printf(" ? Author : xiao p"); ? ? setPosition(xCenter+1, MAXWIDTH+2); ? ? printf("Copyright : c.biancheng.net"); ? ? scoresPostion.x = xCenter-1; ? ? scoresPostion.y = MAXWIDTH+8; } ? int main(){ ? ? char charInput, direction = 'a'; ? ? init(); ? ? ? ? ? charInput = tolower(getch()); ? ? direction = nextDirection(charInput, direction); ? ? ? while(1){ ? ? ? ? if(kbhit()){ ? ? ? ? ? ? charInput = tolower(getch()); ? ? ? ? ? ? if(charInput == ' '){ ? ? ? ? ? ? ? ? charInput = pause(); ? ? ? ? ? ? } ? ? ? ? ? ? direction = nextDirection(charInput, direction); ? ? ? ? } ? ? ? ? move(direction); ? ? ? ? Sleep(500); ? ? } ? ? ? ? ? getch(); ? ? return 0; }
【操作過(guò)程】
編譯運(yùn)行后,先按enter鍵使游戲開始,然后w,s a d 分別控制上下左右移動(dòng)
【運(yùn)行展示】
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VSCode斷點(diǎn)調(diào)試CMake工程項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了VSCode斷點(diǎn)調(diào)試CMake工程項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03C語(yǔ)言結(jié)構(gòu)體版學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言結(jié)構(gòu)體版的學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02C++中set/multiset容器詳解(附測(cè)試用例與結(jié)果圖)
set/multiset屬于關(guān)聯(lián)式容器,底層結(jié)構(gòu)是用二叉樹實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于C++中set/multiset容器的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02C++ OpenCV單峰三角閾值法Thresh_Unimodal詳解
本文主要介紹了適合當(dāng)圖像的直方圖具有明顯單峰特征時(shí)使用,結(jié)合了三角法的原理而設(shè)計(jì)的圖像分割方法,感興趣的小伙伴可以了解一下2021-12-12C語(yǔ)言實(shí)現(xiàn)數(shù)獨(dú)輔助器(附源碼)
數(shù)獨(dú)是源自瑞士的一種數(shù)學(xué)游戲。是一種運(yùn)用紙、筆進(jìn)行演算的邏輯游戲。本文將利用C語(yǔ)言制作一個(gè)數(shù)獨(dú)輔助器,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-01-01