链表-->贪吃蛇--------TO COPY
1 /* 2 贪吃蛇 --->链表 3 */ 4 5 #include "stdio.h" 6 #include "string.h" 7 #include "windows.h" //windows编程头文件 8 #include "time.h" 9 #include "conio.h" //控制台输入输出文件 10 #include "stdbool.h" 11 12 #define up 'w' 13 #define down 's' 14 #define right 'd' 15 #define left 'a' 16 17 void welcome(); // 开始界面 18 void Finish(); // 结束界面 19 void creatgraph(); // 围墙打印 20 void gotoxy(int x, int y); // 光标跳转,横为X 0,1,2.. 21 void gotoprint(int x, int y); // 跳转打印 22 void gotodelete(int x, int y);// 跳转删除 23 void creatfood(); // 食物产生 24 int ClickControl(); // 获取键盘信号 25 int Judge(); // 游戏结束判断 26 void MovingBody(); // 蛇的移动 27 void Eating(); // 蛇吃到东西后的操作(伸长) 28 void ChangeBody(int x, int y); // 蛇的坐标变换 29 30 /*全局变量 + 预处理:*/ 31 typedef struct Snakes { 32 int x; 33 int y; 34 struct Snakes* next; 35 } snake; 36 37 snake* head; // 声明蛇头指针 38 39 // 申明并定义食物 40 struct Food { 41 int x; 42 int y; 43 } food; 44 45 char name[20]; // 保存用户名 有兴趣可以制作登录系统 46 int score = 0; // 分数 47 char click = 1; // 记录敲下的键盘按键 48 int speed; // 速度 其实是延迟的毫秒数 49 50 /************************************************************/ 51 52 int main() { 53 system("color 0B"); // 设置控制台字体颜色 54 welcome(); // 欢迎界面 55 creatgraph(); // 创建地图 56 creatfood(); // 新建食物 57 // 捕获鼠标按键 ClickControl 58 if (ClickControl() == 0) return 0; 59 return 0; 60 } 61 62 /**********************************************************/ 63 void welcome() { 64 system("mode con cols=100 lines=40 "); //cols为控制台的宽度,lines则代表控制台的高度。 65 gotoxy(15, 10); 66 printf("/**********************************************/"); 67 gotoxy(15, 20); 68 printf("/**********************************************/"); 69 gotoxy(20, 13); 70 printf("WELCOME TO THE GAME OF RETRO SNAKE"); 71 gotoxy(14, 16); 72 printf("请在英文输入法中操作,反向移动等同于吃到自己,wasd控制p暂停"); 73 gotoxy(20, 18); 74 printf("PLEASE ENTER YOUR NAME:"); 75 scanf_s("%s", &name, 20); 76 system("cls"); 77 } 78 79 /**********************************************************/ 80 void creatgraph() { 81 int i; 82 /* 83 注意这里横坐标是每次+2 因为控制台字符宽高比为1:2 84 */ 85 for (i = 0; i < 58; i += 2) { 86 gotoprint(i, 0); 87 gotoprint(i, 26); 88 } 89 for (i = 1; i < 26; i++) { 90 gotoprint(0, i); 91 gotoprint(56, i); 92 } 93 gotoxy(63, 10); 94 printf("hello %s,Welcome To Play", name); 95 gotoxy(63, 15); 96 printf("Your Score Is:%d = ̄ω ̄= ", score); 97 gotoxy(63, 20); 98 printf("This Game Is Created By VITO--->TO COPY"); 99 head = (snake*)malloc(sizeof(snake)); 100 snake* p = (snake*)malloc(sizeof(snake)); 101 snake* q = (snake*)malloc(sizeof(snake)); 102 head->x = 16; 103 head->y = 15; 104 p->x = 16; 105 p->y = 16; 106 q->x = 16; 107 q->y = 17; 108 head->next = p; 109 p->next = q; 110 q->next = NULL; 111 } 112 /**********************************************************/ 113 void gotoxy(int x, int y) { 114 // 更新光标位置 115 COORD pos; 116 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); 117 pos.X = x; 118 pos.Y = y; 119 SetConsoleCursorPosition(hOutput, pos); 120 // 隐藏光标 121 CONSOLE_CURSOR_INFO cursor; 122 cursor.bVisible = FALSE; 123 cursor.dwSize = sizeof(cursor); 124 SetConsoleCursorInfo(hOutput, &cursor); 125 } 126 127 /**********************************************************/ 128 void gotoprint(int x, int y) { 129 gotoxy(x, y); 130 printf("■"); 131 } 132 133 /**********************************************************/ 134 void gotodelete(int x, int y) { 135 gotoxy(x, y); 136 printf(" "); 137 } 138 139 /**********************************************************/ 140 void creatfood() { 141 // 随机产生一个食物 142 bool flag = false; 143 while (!flag) { 144 flag = true; 145 srand((int)time(NULL)); 146 food.y = rand() % (25 - 1 + 1) + 1; 147 food.x = rand() % (54 - 2 + 1) + 2; 148 if (food.x % 2 != 0) { 149 food.x = food.x + 1; 150 } 151 snake* judge = head; 152 while (1) { //遍历排除蛇身重复 153 if (judge->next == NULL) break; 154 if (food.x == judge->x && food.y == judge->y) { 155 flag = false; 156 } 157 judge = judge->next; 158 } 159 } 160 gotoxy(food.x, food.y); 161 printf("⊙"); 162 } 163 164 /**********************************************************/ 165 // 捕获鼠标 游戏主循环 166 int ClickControl() { 167 char c; 168 while (1) { 169 if (Judge() == 0) return 0; 170 if (_kbhit()) { 171 click = _getch(); 172 } 173 MovingBody(); 174 Eating(); 175 } 176 return 1; 177 } 178 179 /**********************************************************/ 180 void MovingBody() { 181 int x = head->x, y = head->y; 182 snake* p = head; 183 // 通过先清空后打印实现动画效果 184 while (p->next != NULL) { 185 p = p->next; 186 } 187 gotodelete(p->x, p->y); // 消除尾节点 188 switch (click) { 189 case up: 190 y -= 1; 191 break; 192 case down: 193 y += 1; 194 break; 195 case left: 196 x -= 2; 197 break; 198 case right: 199 x += 2; 200 break; 201 default: 202 break; 203 } 204 if (x != head->x || y != head->y) { 205 // 改变坐标时更新 暂停游戏停止更新蛇 206 ChangeBody(x, y); 207 } 208 p = head; 209 // 打印蛇头 210 gotoprint(p->x, p->y); 211 // 蛇速度控制 212 int count = score / 10; 213 if (count <= 10) speed = 150; 214 else if (count > 10 && count <= 20) speed = 100; 215 else if (count > 20 && count <= 40) speed = 50; 216 else speed = 10; 217 Sleep(speed); 218 } 219 220 /**********************************************************/ 221 // 吃到食物处理 添加一个尾巴 222 void Eating() { 223 if (head->x == food.x && head->y == food.y) { 224 creatfood(); 225 snake* _new = (snake*)malloc(sizeof(snake)); 226 snake* p; 227 p = head; 228 while (1) { 229 if (p->next == NULL) break; 230 p = p->next; 231 } 232 p->next = _new; 233 _new->next = NULL; 234 score += 10; 235 gotoxy(77, 15); 236 printf("%d", score); 237 } 238 } 239 240 /**********************************************************/ 241 // 更新蛇体坐标 只需要消除尾结点 然后把新坐标结点置为头结点即可 242 void ChangeBody(int x, int y) { 243 snake* p = head; 244 while (p->next->next != NULL) { 245 p = p->next; 246 } 247 free(p->next); 248 p->next = NULL; 249 snake* new_head = (snake*)malloc(sizeof(snake)); 250 new_head->x = x; 251 new_head->y = y; 252 new_head->next = head; 253 head = new_head; 254 } 255 256 /**********************************************************/ 257 // 判断是否游戏结束 258 int Judge() { 259 if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) { 260 Finish(); 261 return 0; 262 } 263 snake* p = head->next; 264 while (1) { 265 if (p == NULL) break; 266 if (head->x == p->x && head->y == p->y) { 267 Finish(); 268 return 0; 269 } 270 p = p->next; 271 } 272 return 1; 273 } 274 275 /**********************************************************/ 276 void Finish() { 277 system("cls"); 278 gotoxy(15, 10); 279 printf("/**********************************************/"); 280 gotoxy(15, 20); 281 printf("/**********************************************/"); 282 gotoxy(18, 14); 283 printf("GAME OVER o(* ̄▽ ̄*)o"); 284 gotoxy(20, 16); 285 printf("Your Score is %d hiahiahia", score); 286 gotoxy(18, 18); 287 printf("还不错哦, 继续努力O(∩_∩)O"); 288 gotoxy(0, 27); 289 // 释放空间 290 snake* p = head, * q; 291 while (p != NULL) { 292 q = p->next; 293 free(p); 294 p = q; 295 } 296 system("pause"); 297 }
#include "stdlib.h" #include "stdio.h" //快速排序和冒泡排序 int numbers [8]= {1000,99,2,44,256,789,3,9}; int sz = sizeof(numbers)/sizeof(numbers[0]); int i,j,t; void buddleSort(int arr[],int arr_sz); void swap(int *x, int *y); void printfArr(int arr[],int x); void quick_sort_recursive(int arr[], int start, int end); void quick_sort(int arr[], int len); int main() { //buddleSort(numbers,sz); quick_sort(numbers,sz); printfArr(numbers,sz); return 0; } //冒泡排序 void buddleSort(int arr[],int arr_sz) { for(i = 0; i< arr_sz-1; i++) { // n个数 n-1趟 for(j=0; j< arr_sz-i-1; j++) { if(arr[j]>arr[j+1]) { t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; } } } } //&代表取x地址,*代表指针运算,*&x和x在引用时是等价的.*和&是互逆运算. //int *a = &b; 是一个返回值为int型指针的函数的声明 void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; } //-- 去重 : 判断a[i] 与a[i-1]的数据是否相同,相同则不输出,不同则输出 //快速排序 void quick_sort_recursive(int arr[], int start, int end) { if (start >= end) return; int mid = arr[end]; int left = start, right = end - 1; while (left < right) { while (arr[left] < mid && left < right) left++; while (arr[right] >= mid && left < right) right--; swap(&arr[left], &arr[right]); } //交换基准值 if (arr[left] >= arr[end]) swap(&arr[left], &arr[end]); else left++; //递归 if (left) quick_sort_recursive(arr, start, left - 1); quick_sort_recursive(arr, left + 1, end); } void quick_sort(int arr[], int len) { quick_sort_recursive(arr, 0, len - 1); } void printfArr(int arr[],int x) { for(i =0 ; i<x; i++) { printf("%d-->",arr[i]); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!