日期:2018.9.11
用时:150min
项目:贪吃蛇(C语言--数组 结构体实现)
开发工具:vs2013
关键知识:数组,结构体,图形库,键位操作
源代码:
1 #include<stdio.h> 2 #include<graphics.h> 3 #include<stdlib.h> 4 #include<conio.h> 5 #include<time.h> 6 7 #define N 200 8 int i, key; 9 int score = 0; 10 int gamespeed = 500; 11 12 void init(void); 13 void Draw(); 14 void Playgame(); 15 void Prscore(); 16 void Gameover(); 17 18 struct Food { 19 int x; 20 int y; 21 int yes; 22 }food; 23 24 struct Snake{ 25 int x[N]; 26 int y[N]; 27 int node; 28 int direcion; 29 int life; 30 }snake; 31 32 void main() 33 { 34 init(); 35 Draw(); 36 Playgame(); 37 _getch(); 38 } 39 40 void init(void) 41 { 42 initgraph(640,480); 43 setbkcolor(WHITE); 44 cleardevice(); 45 } 46 47 void Draw() 48 { 49 setcolor(BLACK); 50 for (i = 50; i <= 600; i += 10) 51 { 52 rectangle(i, 40, i + 10, 49); 53 rectangle(i, 451, i + 10, 460); 54 } 55 for (i = 40; i <= 450; i += 10) 56 { 57 rectangle(50, i, 59, i + 10); 58 rectangle(601, i, 610, i + 10); 59 } 60 } 61 62 void Playgame() 63 { 64 srand((unsigned)time(NULL));//用时间做种,每次产生的随机数不一样。 65 food.yes = 1;//1表示需要出现食物,0表示已有食物 66 snake.life = 0;//0,活着,1 死亡 67 snake.direcion = 1; 68 snake.x[0] = 100; snake.y[0] = 100; 69 snake.x[1] = 110; snake.y[1] = 100; 70 snake.node = 2; 71 Prscore(); 72 while (1) 73 { 74 while (!_kbhit()) 75 { 76 if (food.yes == 1) 77 { 78 food.x = rand() % 400 + 60; 79 food.y = rand() % 350 + 60; 80 while (food.x % 10 != 0) 81 food.x++; 82 while (food.y % 10 != 0) 83 food.y++; 84 food.yes = 0; 85 } 86 if (food.yes == 0) 87 { 88 setcolor(RED); 89 rectangle(food.x, food.y, food.x + 10, food.y + 10); 90 } 91 for (i = snake.node - 1; i > 0; i--) 92 { 93 snake.x[i] = snake.x[i - 1]; 94 snake.y[i] = snake.y[i - 1]; 95 } 96 switch (snake.direcion) 97 { 98 case 1:snake.x[0] += 10; break; 99 case 2:snake.x[0] -= 10; break; 100 case 3:snake.y[0] -= 10; break; 101 case 4:snake.y[0] += 10; break; 102 } 103 for (i = 3; i < snake.node; i++) 104 { 105 if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) 106 { 107 Gameover(); 108 snake.life = 1; 109 break; 110 } 111 } 112 if (snake.x[0]<55 || snake.x[0]>595 || snake.y[0]<55 || snake.y[0]>455) 113 { 114 Gameover(); 115 snake.life = 1; 116 } 117 if (snake.life == 1) 118 break; 119 if (snake.x[0] == food.x&&snake.y[0] == food.y) 120 { 121 setcolor(BLACK); 122 rectangle(food.x, food.y, food.x + 10, food.y + 10); 123 snake.x[snake.node] = -20; snake.y[snake.node] = -20; 124 snake.node++; 125 food.yes = 1; 126 score += 10; 127 Prscore(); 128 } 129 setcolor(GREEN); 130 for (i = 0; i < snake.node; i++) 131 rectangle(snake.x[i], snake.y[i],snake.x[i] + 10, snake.y[i] + 10); 132 if (food.yes == 1) 133 { 134 if (gamespeed >= 20) 135 gamespeed -= 10;// 速度最大不超过 136 } 137 138 Sleep(gamespeed); 139 setcolor(WHITE); 140 rectangle(snake.x[snake.node - 1], snake.y[snake.node - 1], snake.x[snake.node-1] + 10, snake.y[snake.node - 1] + 10); 141 } 142 if (snake.life == 1) 143 break; 144 switch (_getch()) 145 { 146 case ‘w‘: 147 case ‘W‘: 148 if (snake.direcion != 4) 149 { 150 snake.direcion = 3; 151 } 152 break; 153 case ‘d‘: 154 case ‘D‘: 155 if (snake.direcion != 2) 156 { 157 snake.direcion = 1; 158 } 159 break; 160 case ‘a‘: 161 case ‘A‘: 162 if (snake.direcion != 1) 163 { 164 snake.direcion = 2; 165 } 166 break; 167 case ‘s‘: 168 case ‘S‘: 169 if (snake.direcion != 3) 170 { 171 snake.direcion = 4; 172 } 173 break; 174 } 175 } 176 } 177 178 179 void Prscore() 180 { 181 char str[10]; 182 setfillstyle(SOLID_FILL, YELLOW); 183 bar(50, 15, 220, 35); 184 setcolor(6); 185 sprintf_s(str, "score:%d", score); 186 outtextxy(55, 20, str); 187 } 188 189 void Gameover() 190 { 191 cleardevice(); 192 setbkcolor(WHITE); 193 Prscore(); 194 setcolor(RED); 195 settextstyle(20, 20, "楷体"); 196 outtextxy(200, 200, "GAME OVER"); 197 _getch(); 198 }
运行截图:
原文地址:https://www.cnblogs.com/qingfenghanli/p/9634180.html
时间: 2024-10-29 00:14:53