C語言實(shí)現(xiàn)貪吃蛇游戲代碼
一、實(shí)現(xiàn)效果
鍵位:使用wasd四個(gè)鍵位來控制方向,按q鍵退出(注意在終用英文輸入法實(shí)現(xiàn)鍵控)
規(guī)則:蛇每吃一個(gè)豆會(huì)得10分,同時(shí)身體邊長、移速加快
當(dāng)蛇碰到墻壁或咬到自身時(shí)游戲結(jié)束,同時(shí)會(huì)輸出游戲得分
二、部分代碼解釋
(1)用結(jié)構(gòu)體定義蛇和豆
typedef struct Snakes { int x; int y; struct Snakes *next; }snake; snake *head,*tail; struct Food { int x; int y; }food;
(2)打印墻壁
void creatgraph() { int i; for (i = 0; i<58; i += 2)//打印上下邊框 { gotoprint(i, 0); gotoprint(i, 26); } for (i = 1; i < 26; i++) { gotoprint(0, i); gotoprint(56, i); } head = (snake*)malloc(sizeof(snake)); head->x = 16; head->y = 15; //gotoprint(head->x, head->y); tail = (snake*)malloc(sizeof(snake)); snake *p = (snake*)malloc(sizeof(snake)); snake *q = (snake*)malloc(sizeof(snake)); p->x = 16; p->y = 16; q->x = 16; q->y = 17; head->next = p; p->next = q; q->next = tail; //gotoprint(p->x, p->y); //gotoprint(q->x, q->y); tail->next = NULL; tail->x = 4; tail->y = 2; } void gotoxy(int x, int y) { COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, pos); } void gotoprint(int x, int y) { gotoxy(x, y); printf("■"); } void gotodelete(int x, int y) { gotoxy(x, y); printf(" "); }
(3)生成豆
void creatfood() { srand((int)time(NULL)); lable: food.y = rand() % (25 - 1 + 1) + 1; food.x = rand() % (54 - 2 + 1) + 2; if (food.x % 2 != 0) { food.x = food.x+1; } snake *judge = head; while (1) { if (judge->next == NULL) break; if (food.x == judge->x&&food.y == judge->y) { goto lable; } judge = judge->next; } gotoxy(food.x, food.y); printf("⊙"); }
(4)點(diǎn)擊控制函數(shù)
int ClickControl() { char c; while (1) { if (Judge()==0) return 0; if (_kbhit()) { click = _getch(); } MovingBody(); Eating(); } return 1; }
(5)移動(dòng)控制
void MovingBody() { int count = 0; int a = head->x, b = head->y; snake *p = head; while (1) { if (p->next == NULL) break; gotodelete(p->x, p->y); count++; p = p->next; } switch (click) { case up: head->y -= 1; ChangeBody(a,b); break; case down: head->y += 1; ChangeBody(a,b); break; case left: head->x -= 2; ChangeBody(a,b); break; case right: head->x += 2; ChangeBody(a,b); break; case stop: _getch(); break; } p = head; while (1) { if (p->next == NULL) break; gotoprint(p->x, p->y); p = p->next; } p = head; gotoxy(0, 28); if (count <= 10) speed = 150; else if (count > 10 && count <= 20) speed = 100; else if (count > 20 && count <= 40) speed = 50; else speed = 10; Sleep(speed); }
(6)更改蛇身
void ChangeBody(int a,int b) { snake *p = head->next; int mid1, mid2,_mid1,_mid2; mid1 = p->x; mid2 = p->y; while (1) { if (p->next->next == NULL) break; _mid1 = p->next->x; _mid2 = p->next->y; p->next->x = mid1; p->next->y = mid2; mid1 = _mid1; mid2 = _mid2; p = p->next; } p = head->next; { p->x = a; p->y = b; } }
總結(jié)
到此這篇關(guān)于C語言實(shí)現(xiàn)貪吃蛇游戲代碼的文章就介紹到這了,更多相關(guān)C語言貪吃蛇游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT編寫地圖實(shí)現(xiàn)設(shè)備點(diǎn)位的示例代碼
在地圖應(yīng)用的相關(guān)項(xiàng)目中,在地圖上標(biāo)識(shí)一些設(shè)備點(diǎn),并對(duì)點(diǎn)進(jìn)行交互這個(gè)功能用的最多的,于是需要一套機(jī)制可以動(dòng)態(tài)的添加、刪除、清空、重置。本文將詳細(xì)介紹這些功能如何實(shí)現(xiàn),需要的可以參考一下2022-01-01基于Matlab圖像處理的公路裂縫檢測(cè)實(shí)現(xiàn)
隨著公路的大量投運(yùn),公路日常養(yǎng)護(hù)和管理已經(jīng)成為制約公路運(yùn)營水平提高的瓶頸,特別是路面狀態(tài)采集、檢測(cè)維護(hù)等工作更是對(duì)傳統(tǒng)的公路運(yùn)維模式提出了挑戰(zhàn)。這篇文章主要介紹了如何通過Matlab圖像處理實(shí)現(xiàn)公路裂縫檢測(cè),感興趣的可以了解一下2022-02-02Linux中使用VS Code編譯調(diào)試C++項(xiàng)目詳解
最近因?yàn)轫?xiàng)目的需求,需要在Linux下開發(fā)C++相關(guān)項(xiàng)目,經(jīng)過一番摸索最終實(shí)現(xiàn)了,下面這篇文章就給大家簡(jiǎn)單總結(jié)了一下如何通過VS Code進(jìn)行編譯調(diào)試的一些注意事項(xiàng)。有需要的朋友們可以參考借鑒,下面來跟著小編一起看看吧。2016-12-12