我是靠谱客的博主 真实黑裤,最近开发中收集的这篇文章主要介绍2020-12-29,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

写贪吃蛇吗?一起呀!C语言指针的那种
1贪吃蛇主界面

2
登录界面,密码a开头就行
3
登录成功界面
4登录失败界面
5登录成功后,选择贪吃蛇的颜色
6演示图片
附原码:
#include <iostream.h>#include <graphics.h>#include <stdio.h>#include <conio.h>#include <time.h>#define r 10 //蛇头和食物半径#define x0 600#define y0 375 //蛇头初始位置#define l 5 //初始化蛇长度#define gap 80typedef struct snake{ int x; int y; struct snake *next; struct snake *before;}Snake;void InitGame(); //加载游戏初始化界面void RunSnake(); //蛇链表移动void MakeFood(); //产生食物int GetPlayerCommand();int Abs(int a){ if(a<0) return -a; else return a;}int v=4;int go=1;int score=0; //玩家得分time_t T;int each=5; //每个食物加分COLORREF Se;Snake *food=(Snake *)malloc(sizeof(Snake));Snake *head=(Snake *)malloc(sizeof(Snake));void main() //主函数在此{ InitGame(); RunSnake(); _getch();// 按任意键退出 closegraph();}//此下为函数部分void InitGame(){ int TT=100; char str[4],str0[4]={‘a’,‘b’,‘c’}; IMAGE img; initgraph(1200, 750);// 初始化绘图窗口 HWND hWnd=GetHWnd();//获得窗口句柄 SetWindowText(hWnd,“贪吃蛇2020-冬季版 合肥学院祝您游戏愉快!”); //修改窗口名称 loadimage(&img,_T(“C:/Users/吕骄涵/Pictures/Saved Pictures/0_LI.jpg”));//加载图片 settextstyle(40,0,_T(“宋体”)); outtextxy(400,4,“欢迎来到《贪吃蛇2020》”); Resize(&img,1000,680); putimage(120,60,&img);//放置图片位置 settextstyle(55,0,_T(“黑体”)); outtextxy(40 ,90 ,“健”);Sleep(TT); outtextxy(40 ,160 ,“康”);Sleep(TT); outtextxy(40 ,230 ,“游”);Sleep(TT); outtextxy(40 ,300 ,“戏”);Sleep(TT); outtextxy(40 ,370 ,",");Sleep(TT); outtextxy(40 ,440 ,“快”);Sleep(TT); outtextxy(40 ,510 ,“乐”);Sleep(TT); outtextxy(40 ,580 ,“生”);Sleep(TT); outtextxy(40 ,650 ,“活”);Sleep(TT); outtextxy(1100 ,90 ,“享”);Sleep(TT); outtextxy(1100 ,160 ,“受”);Sleep(TT); outtextxy(1100 ,230 ,“美”);Sleep(TT); outtextxy(1100 ,300 ,“好”);Sleep(TT); outtextxy(1100 ,370 ,",");Sleep(TT); outtextxy(1100 ,440 ,“有”);Sleep(TT); outtextxy(1100 ,510 ,“你”);Sleep(TT); outtextxy(1100 ,580 ,“有”);Sleep(TT); outtextxy(1100 ,650 ,“我”); settextstyle(40,0,_T(“宋体”)); settextcolor(YELLOW); outtextxy(400,4,“欢迎来到《贪吃蛇2020》”); _getch(); cleardevice(); settextcolor(WHITE); outtextxy(400,50,“适度游戏,享受健康生活”); outtextxy(50,140,"·登录专区:"); line(0,200,1200,200); InputBox(str,4,“账号:”,“登录验证:”,“ABC”,300,100,false); for(int u=0;u<1210;u+=40) { outtextxy(u,600,"·"); Sleep(50); } //登录游戏 if(str[0]!=str0[0]){ outtextxy(300,300,“抱歉!账号错误,按任意键退出!”); _getch(); closegraph(); exit(0); } cleardevice(); outtextxy(300,300,“登录成功,按任意键开始游戏……”); _getch(); cleardevice(); int ii,k; char io[5]; char *ip=(char )malloc(sizeof(char)); int x1=200,x2=300,y1=100,y2=200; for(k=1;k<8;k++) { switch(k) { case 1: Se=WHITE; break; case 2: Se=GREEN; break; case 3: Se=YELLOW; break; case 4: Se=RED; break; case 5: Se=BLUE; break; case 6: Se=BROWN; break; case 7: Se=LIGHTGRAY; break; } setlinecolor(Se); setfillcolor(Se); rectangle(x1,y1,x2,y2); fillrectangle(x1,y1,x2,y2); sprintf(io,"%d",k); settextstyle(30,0,_T(“宋体”)); outtextxy(250+(k-1)120,230,io); x1=200+k120; x2=300+k120; } sprintf(ip,"%d",ii); InputBox(ip,3,“账号:”,“请选择蛇的颜色:”,“6”,300,100,true); ii=atoi(ip); switch(ii) { case 1: Se=WHITE; break; case 2: Se=GREEN; break; case 3: Se=YELLOW; break; case 4: Se=RED; break; case 5: Se=BLUE; break; case 6: Se=BROWN; break; case 7: Se=LIGHTGRAY; break; } cleardevice();}void RunSnake(){ char s[15]; char *t; char *t1; char *t2; Snake q=head; Snake p=head; q->next=NULL; p->before=NULL; head->x=x0; head->y=y0; MakeFood(); while(1) { setlinecolor(GREEN); line(0,50,1200,50); settextstyle(30,0,_T(“宋体”)); outtextxy(380,10,"《贪吃蛇2020》正在进行中……"); settextstyle(27,0,_T(“宋体”)); sprintf(s,"%d",score); //将整型变量score转换成字符串 outtextxy(100,10,"分数: "); outtextxy(180,10,s); T=time(NULL); //机器时间 t=ctime(&T); outtextxy(850,10,t); q=head; go=GetPlayerCommand(); if(go==1) { head->x-=v; if(head->x<=-r) head->x=1200+r; while(q) //画完所有蛇体 { setlinecolor(Se); setfillcolor(Se); circle(q->x,q->y,r); fillcircle(q->x,q->y,r); q=q->next; } Sleep(gap); q=head; while(q) //抹去所有蛇体 { setlinecolor(BLACK); setfillcolor(BLACK); circle(q->x,q->y,r); fillcircle(q->x,q->y,r); q=q->next; } q=head; if((Abs(q->x-food->x)<=2r-2)&&(Abs(q->y-food->y)<=2r-2))//向左移动过程中吃到食物 { if(v<50) v+=1; score+=each; setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(food->x,food->y,r);//吃过的食物消失 while(q->next) q=q->next; Snake rear=(Snake )malloc(sizeof(Snake)); //蛇变长 rear->x=q->x; rear->y=q->y; q->next=rear; q=rear; q->next=NULL; //尾插法 rear->before=p->before; //头插法 p->before=rear; MakeFood(); q=head; } } if(go==2) { head->y-=v; if(head->y<=50) head->y=700+r; while(q) //画完所有蛇体 { setlinecolor(Se); setfillcolor(Se); circle(q->x,q->y,r); fillcircle(q->x,q->y,r); q=q->next; } Sleep(gap); q=head; while(q) //抹去所有蛇体 { setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(q->x,q->y,r); q=q->next; } q=head; if((Abs(q->x-food->x)<=2r-2)&&(Abs(q->y-food->y)<=2r-2))//向上移动过程中吃到食物 { if(v<50) v+=1; score+=each; setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(food->x,food->y,r);//吃过的食物消失 while(q->next) q=q->next; Snake rear=(Snake )malloc(sizeof(Snake)); //蛇变长 rear->x=q->x; rear->y=q->y; q->next=rear; q=rear; q->next=NULL; //尾插法 rear->before=p->before; //头插法 p->before=rear; MakeFood(); q=head; } } if(go==3) { head->x+=v; if(head->x>=1200+r) head->x=-r; while(q) //画完所有蛇体 { setlinecolor(Se); setfillcolor(Se); circle(q->x,q->y,r); fillcircle(q->x,q->y,r); q=q->next; } Sleep(gap); q=head; while(q) //抹去所有蛇体 { setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(q->x,q->y,r); q=q->next; } q=head; if((Abs(q->x-food->x)<=2r-2)&&(Abs(q->y-food->y)<=2r-2))//向右移动过程中吃到食物 { if(v<50) v+=1; score+=each; setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(food->x,food->y,r);//吃过的食物消失 while(q->next) q=q->next; Snake rear=(Snake )malloc(sizeof(Snake)); //蛇变长 rear->x=q->x; rear->y=q->y; q->next=rear; q=rear; q->next=NULL; //尾插法 rear->before=p->before; //头插法 p->before=rear; MakeFood(); q=head; } } if(go==4) { head->y+=v; if(head->y>=750+r) head->y=50; while(q) //画完所有蛇体 { setlinecolor(Se); setfillcolor(Se); circle(q->x,q->y,r); fillcircle(q->x,q->y,r); q=q->next; } Sleep(gap); q=head; while(q) //抹去所有蛇体 { setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(q->x,q->y,r); q=q->next; } q=head; if((Abs(q->x-food->x)<=2r-2)&&(Abs(q->y-food->y)<=2r-2))//向下移动过程中吃到食物 { if(v<50) v+=1; score+=each; setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(food->x,food->y,r);//吃过的食物消失 while(q->next) q=q->next; Snake *rear=(Snake *)malloc(sizeof(Snake)); //蛇变长 rear->x=q->x; rear->y=q->y; q->next=rear; q=rear; q->next=NULL; //尾插法 rear->before=p->before; //头插法 p->before=rear; MakeFood(); q=head; } } if(head->before!=NULL) { p=head->before; if(p->before!=NULL)//有>=3个结点 { while(p->before) //让蛇身体每个节点获得最新位置 { p->x=p->before->x; p->y=p->before->y; p=p->before; } //p=p->next; p->x=head->x; p->y=head->y; } else{ //只有两个节点 p->x=head->x; p->y=head->y; } } q=head; }}void MakeFood(){ food->x=rand()%1100+50; food->y=rand()%650+50; setlinecolor(BLUE); setfillcolor(BLUE); circle(food->x,food->y,r); fillcircle(food->x,food->y,r);}/*void MakeSnake(){ Snake p=head; for(int i=0;i<l;i++) //初始化蛇长度为 { Snake rear=(Snake )malloc(sizeof(Snake)); rear->x=x0+2ir; rear->y=y0; p->next=rear; p=rear; } p->next=NULL; }/int GetPlayerCommand(){ if(GetAsyncKeyState(‘A’)& 0x8000||GetAsyncKeyState(VK_LEFT)& 0x8000) //左 go=1; if(GetAsyncKeyState(‘W’)& 0x8000||GetAsyncKeyState(VK_UP)& 0x8000) //上 go=2; if(GetAsyncKeyState(‘D’)& 0x8000||GetAsyncKeyState(VK_RIGHT)& 0x8000) //右 go=3; if(GetAsyncKeyState(‘S’)& 0x8000||GetAsyncKeyState(VK_DOWN)& 0x8000) //下 go=4; return go;}

最后

以上就是真实黑裤为你收集整理的2020-12-29的全部内容,希望文章能够帮你解决2020-12-29所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部