C語言實現(xiàn)貪吃蛇游戲代碼
更新時間:2022年02月07日 09:52:18 作者:鴻蒙之始
大家好,本篇文章主要講的是C語言實現(xiàn)貪吃蛇游戲代碼,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
一、實現(xiàn)效果
鍵位:使用wasd四個鍵位來控制方向,按q鍵退出(注意在終用英文輸入法實現(xiàn)鍵控)
規(guī)則:蛇每吃一個豆會得10分,同時身體邊長、移速加快
當蛇碰到墻壁或咬到自身時游戲結(jié)束,同時會輸出游戲得分
二、部分代碼解釋
(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)點擊控制函數(shù)
int ClickControl() { char c; while (1) { if (Judge()==0) return 0; if (_kbhit()) { click = _getch(); } MovingBody(); Eating(); } return 1; }
(5)移動控制
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語言實現(xiàn)貪吃蛇游戲代碼的文章就介紹到這了,更多相關(guān)C語言貪吃蛇游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux中使用VS Code編譯調(diào)試C++項目詳解
最近因為項目的需求,需要在Linux下開發(fā)C++相關(guān)項目,經(jīng)過一番摸索最終實現(xiàn)了,下面這篇文章就給大家簡單總結(jié)了一下如何通過VS Code進行編譯調(diào)試的一些注意事項。有需要的朋友們可以參考借鑒,下面來跟著小編一起看看吧。2016-12-12