網站首頁 編程語言 正文
一、實現效果
鍵位:使用wasd四個鍵位來控制方向,按q鍵退出(注意在終用英文輸入法實現鍵控)
規則:蛇每吃一個豆會得10分,同時身體邊長、移速加快
當蛇碰到墻壁或咬到自身時游戲結束,同時會輸出游戲得分
二、部分代碼解釋
(1)用結構體定義蛇和豆
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)點擊控制函數
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; } }
總結
原文鏈接:https://blog.csdn.net/z_9999999/article/details/122796455
相關推薦
- 2023-10-31 IP地址、網關、網絡/主機號、子網掩碼關系
- 2022-02-13 QT 控件 QListWidget 設置所有事件都激發編輯(雙擊, 選擇, 選項變化)
- 2022-09-05 springboot是怎么實現自動配置的?
- 2022-04-03 C#字符串內存駐留機制分析_C#教程
- 2022-09-22 繼承關系下構造方法的訪問特點
- 2022-08-16 C++超詳細梳理基礎知識_C 語言
- 2022-10-13 pytorch和tensorflow計算Flops和params的詳細過程_python
- 2023-04-03 PyTorch中Torch.arange函數詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支