贪吃蛇代码

  1 #include<stdio.h>
  2 #include<windows.h>
  3 #include<time.h>
  4 #include<conio.h>
  5 #include<stdlib.h>
  6 struct node{
  7     int x,y;
  8     node *next;
  9 };
 10
 11 int Life=1;//是否死亡  0代表死亡;
 12 int x_food,y_food;//食物位置
 13 char movedir=‘R‘;//方向
 14 node* SHead=NULL;//
 15 node* STail=NULL;
 16 node* p=NULL;
 17 int foodnum=0;
 18 int score=0;
 19 int level=0;
 20 int death=0;
 21 int speed=400;
 22 int Stime=0;
 23 int count=0;
 24
 25 void creatsnakemap();//创建地图
 26 void creatsnake();//创建蛇
 27 void gotoxy(int,int);//光标定位函数*
 28 void SetColor(unsigned short);//设置颜色
 29 void startgame();//开始游戏
 30 void isdead();//判断是否死亡;
 31 void nofood(int x,int y);//前方没有食物
 32 void yesfood();//前方有食物
 33 void makefood();//随机产生食物
 34 void drawsnake();//画出蛇
 35 void Snakedelete();//删除蛇的尾巴
 36 void isfood();//判断前方是否有食物
 37 void creatscreen();//打印开始屏幕
 38 void ending();//结束
 39 bool judge();//判断食物是否出现在蛇身体上
 40 int time();//计算时间
 41
 42 int main()
 43 {
 44     system("mode con cols=80 lines=35");
 45     creatscreen();
 46     creatsnakemap();
 47     creatsnake();
 48     startgame();
 49     ending();
 50     while(getch()!=27)
 51         continue;
 52 }
 53 void creatscreen()
 54 {
 55     SetColor(7);
 56     gotoxy(31,10);printf("经典贪吃蛇");
 57     gotoxy(28,12);printf("按任意键开始游戏");
 58     gotoxy(28,30);printf("[email protected]_御心飞行");
 59     getch();
 60     system("cls");
 61 }
 62 void creatsnakemap()
 63 {
 64     int i;
 65     SetColor(3);
 66     for(i=4;i<50;i+=2)//横墙
 67     {
 68         gotoxy(i,4);
 69         printf("■");
 70         gotoxy(i,29);
 71         printf("■");
 72         Sleep(30);
 73     }
 74     for(i=4;i<29;i++)//竖墙
 75     {
 76         gotoxy(4,i);
 77         printf("■");
 78         gotoxy(48,i);
 79         printf("■");
 80         Sleep(30);
 81     }
 82     SetColor(7);
 83     gotoxy(58,13);printf("人头:");
 84     gotoxy(58,15);printf("等级:");
 85     gotoxy(58,17);printf("速度:");
 86     gotoxy(58,19);printf("时间:");
 87 }
 88 void creatsnake()
 89 {
 90     node* newsnake;
 91     int i;
 92     SHead=(node *)malloc(sizeof(node));
 93     STail=SHead;
 94     STail->next=NULL;
 95     for(i=0;i<3;i++)
 96     {
 97         newsnake=(node *)malloc(sizeof(node));
 98         newsnake->next=NULL;
 99         STail->next =newsnake;
100         STail=newsnake;
101     }
102     STail->next=NULL;
103     p=SHead;
104     SetColor(2);
105     gotoxy(p->x=16,p->y=10),printf("◆");
106     p=p->next;
107     SetColor(3);
108     gotoxy(p->x=14,p->y=10),printf("□");
109     p=p->next;
110     gotoxy(p->x=12,p->y=10),printf("□");
111     p=p->next;
112     gotoxy(p->x=10,p->y=10),printf("□");
113
114 }
115 void startgame()
116 {
117     while(1)
118     {
119         isdead();
120         if(Life==0)
121             break;
122         if(foodnum==0)
123         {
124             makefood();
125             foodnum=1;
126         }
127         if(GetAsyncKeyState(VK_UP)&&movedir!=‘D‘)
128             movedir=‘U‘;
129         if(GetAsyncKeyState(VK_DOWN)&&movedir!=‘U‘)
130             movedir=‘D‘;
131         if(GetAsyncKeyState(VK_LEFT)&&movedir!=‘R‘)
132             movedir=‘L‘;
133         if(GetAsyncKeyState(VK_RIGHT)&&movedir!=‘L‘)
134             movedir=‘R‘;
135         isfood();    //判断前方是否有食物,然后移动一格
136         Sleep(speed);//刷新时间,即速度;
137         count++;
138         time();
139         int Olevel=level;
140         SetColor(7);
141         gotoxy(68,13);printf("%d",score);
142         gotoxy(68,15);printf("%d",level=score/7+1);
143         gotoxy(68,17);printf("%d",speed);
144         gotoxy(68,19);printf("%d",Stime);
145         if(Olevel==level-1)//等级判断
146             if(speed>200)
147                 speed-=60;
148             else if(speed>100)
149                 speed-=40;
150             else if(speed>15)
151                 speed-=14;
152     }
153     system("cls");
154 }
155 void isfood()
156 {
157     if(movedir==‘U‘)
158     {
159         if(SHead->x==x_food&&SHead->y-1==y_food)//判断
160             yesfood();    //前方有食物
161         else
162             nofood(SHead->x,SHead->y-1);//前方没有食物
163     }
164     if(movedir==‘D‘)
165     {
166         if(SHead->x==x_food&&SHead->y+1==y_food)
167             yesfood();
168         else
169             nofood(SHead->x,SHead->y+1);
170     }
171     if(movedir==‘L‘)
172     {
173         if(SHead->x-2==x_food&&SHead->y==y_food)
174             yesfood();
175         else
176             nofood(SHead->x-2,SHead->y);
177     }
178     if(movedir==‘R‘)
179     {
180         if(SHead->x+2==x_food&&SHead->y==y_food)
181             yesfood();
182         else
183             nofood(SHead->x+2,SHead->y);
184
185     }
186     drawsnake();
187 }
188 void nofood(int x,int y)
189 {
190         node* Nh;//临时头节点
191         Nh=(node *)malloc(sizeof(node));//设定新的蛇头
192         Nh->x=x;
193         Nh->y=y;
194         Nh->next=SHead;
195         SHead=Nh;
196         Snakedelete();//删除最后一个节点
197 }
198 void yesfood()
199 {
200     node* food;
201     food=(node*)malloc(sizeof(node));
202     food->x=x_food;
203     food->y=y_food;
204     food->next=SHead;
205     SHead=food;
206     foodnum=0;
207     makefood();
208     score++;
209 }
210 void Snakedelete()
211 {
212     p=SHead;
213     while((p->next)->next!=NULL)
214     {
215         p=p->next;
216     }
217     gotoxy(STail->x,STail->y),printf(" ");
218     free(STail);
219     STail=p;
220     STail->next=NULL;
221 }
222 void drawsnake()
223 {
224     p=SHead->next;
225     SetColor(2);
226     gotoxy(SHead->x,SHead->y);
227     printf("◆");
228     while(p!=NULL)
229     {
230         SetColor(2);
231         gotoxy(p->x,p->y),printf("□");
232         p=p->next;
233     }
234 }
235 void isdead()//判断是否死亡
236 {
237     node* p;
238     if(SHead->y==4&&movedir==‘U‘)//预判是否撞到墙
239         Life=0;
240     if(SHead->y==29&&movedir==‘D‘)
241         Life=0;
242     if(SHead->x==4&&movedir==‘L‘)
243         Life=0;
244     if(SHead->x==48&&movedir==‘R‘)
245         Life=0;
246     for(p=SHead->next;p!=NULL;p=p->next)//判断是否撞到自己
247     {
248         if(SHead->x==p->x&&SHead->y==p->y)
249             Life=0;
250     }
251 }
252 void makefood()
253 {
254     srand(time(NULL));
255     do
256     {
257         x_food=rand()%41+5;
258         if(x_food%2==1)
259             x_food++;
260         y_food=(rand()*rand())%23+5;
261     }while(judge());
262     gotoxy(x_food,y_food);
263     SetColor(4);
264     printf("●");
265     foodnum=1;
266 }
267 bool judge()
268 {
269     node* p=SHead;
270     for(;p!=NULL;p=p->next)
271     {
272         if(p->x==x_food&&p->y==y_food)
273             return 1;
274     }
275     return 0;
276 }
277 int time()
278 {
279     Stime=(int)(count/(1000/(double)speed));
280 }
281 void ending()
282 {
283     SetColor(6);
284     gotoxy(27,10);printf("game over!   -_-!");
285     SetColor(7);
286     gotoxy(30,14);printf("成绩:%d",score);
287     gotoxy(30,16);printf("时间:%d",Stime);
288     gotoxy(21,30);printf("我的博客:www.cnblog.com/a1225234/");
289 }
290 void gotoxy(int x, int y)//光标定位函数  x列,y行
291 {
292      COORD coord = {x, y};
293      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
294 }
295
296 void SetColor(unsigned short ForeColor) // ForeColor前景色
297 {
298     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //获得缓冲区句柄
299     SetConsoleTextAttribute(hCon,ForeColor);//设置文本及背景颜色
300 }
时间: 2025-01-02 17:43:36

贪吃蛇代码的相关文章

转载——C++控制台贪吃蛇代码

游戏截图: 以下是3个代码文件: Snake_Class.h文件: 1 #ifndef SNAKE 2 #define SNAKE 3 4 #include<windows.h> 5 #include<vector> 6 #include<string> 7 8 //标记界面和菜单项的ID 9 enum ID_ { ID_1=1, ID_2, ID_3, ID_4, ID_5, ID_6, ID_7 }; 10 11 //标记初级,中级,高级三种游戏等级 12 enum

C++控制台贪吃蛇代码

游戏截图: 以下是3个代码文件: Snake_Class.h文件: 1 #ifndef SNAKE 2 #define SNAKE 3 4 #include<windows.h> 5 #include<vector> 6 #include<string> 7 8 //标记界面和菜单项的ID 9 enum ID_ { ID_1=1, ID_2, ID_3, ID_4, ID_5, ID_6, ID_7 }; 10 11 //标记初级,中级,高级三种游戏等级 12 enum

html 贪吃蛇代码

最近在搞自己的网站,维护的时候准备放个贪吃蛇上去,顶一下原有的页面. 这个贪吃蛇有一点毒.原来设定了100级:100级刚开局就挂了.后来改掉了选项菜单,修复了. 还有什么bug,欢迎点击侧边的QQ按钮联系我. 代码部分: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> &l

基于c++的贪吃蛇代码

#include <windows.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <cstring>#include <cstdio>#include <iostream>#define N 22using namespace std;int gameover;int x1, y1; // 随机出米int x,y;long start;//

贪吃蛇代码(转)

双人博弈贪吃蛇游戏 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <jsoncpp/json.h> 6 #include <cmath> 7 #include <ctime> 8 9 #define mp make_pair 10 #define x first 11 #def

有没有人要贪吃蛇的代码,然而有....................

呵呵 博友们, 贪吃蛇代码.................... #include<iostream.h> #include<windows.h> #include<time.h> #include<stdlib.h> #include<conio.h> #define N 21 void gotoxy(int x,int y)//位置函数 { COORD pos; pos.X=2*x; pos.Y=y; SetConsoleCursorPos

【C语言小游戏】 贪吃蛇

windows编译运行 源代码: /*这是一个贪吃蛇代码,运行环境VC++6.0(亲测完美运行)*/ /*该程序在dos系统下运行,不需要graphics.h头文件*/ #include <windows.h> #include <stdlib.h> #include <time.h> #include <stdio.h> #include <string.h> #include <conio.h> #define N 21 int 

greedysnake 贪吃蛇

大一上学期软导的贪吃蛇代码,当时实现了很久,努力自己实现了大部分, 另外参考了师兄给的代码,最终才完整实现,但是没有实现自动的部分 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #define SNAKE_MAX_LENGTH 20 #define SNAKE_HEAD 'H' #define SNAKE_BODY 'X' #define BLANK_CELL

贪吃蛇“大作战”(四)

创建真正的游戏 之前演示的贪吃蛇游戏是以python命令行的形式模拟实现的,今天博客的主题就是是创建一个真正的贪吃蛇游戏,其摆脱了命令行的限制,界面更加美观,人机交互更加方便,可以让玩家有更好的游戏体验. 那么,要如何将贪吃蛇游戏做的更好.一个真正的游戏是通过图形用户界面GUI(Graphical User Interface)来展示的.在python中,要做一个GUI有多种选择,简单的就是easygui模块,进阶版的就是tkinter模块.wxPython模块等:当然,还有些模块虽然不是专门做