用C语言怎么做用java编写贪吃蛇游戏戏

C语言简单贪吃蛇游戏代码_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C语言简单贪吃蛇游戏代码
上传于|0|0|文档简介
&&C语言简单贪吃蛇游戏代码
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢贪吃蛇编写思路及C语言源码
规则:每吃上一个点,就长大一点,不能撞墙或者撞上自己身体。
教训:在有限的空间里,可以贪吃,但是要注意安全哦。
这个游戏是我刚工作一年的时候写的,当时对C语言有了些了解,想做点东西,刚好看到同事的手机上有这个
游戏(那时我还没有手机呢,呵呵),觉得挺好玩,要实现的话也还比较简单,就在电脑上用TC上做出来了
。刚做出来的时候,那种喜悦,确实不是玩别人的游戏所能带来的。
这个程序是入门级别的,适合编程刚入门的朋友们试一试。高手达人们请跳过。
下面我简单说一下我的思路,理解下面几点会比较清楚一些。
1,基础:你首先要能画出一个带颜色的方块。举一反三:可以画一个就可以画很多个了。(TC中画方块用到了EGAVGA.BGI这个文件)
2,移动:一个方块消失,相邻地方一个方块出现,在视觉上就是移动了。
3,消失:用背景颜色在同样的地方画同样大小的方块。
4,相对坐标:视觉上像素这个单位太小,用方块的大小作为相对坐标的单位。
5,随机点:使用伪随机函数,参数一般用上系统当前时间,你再随意捏造个四则运算,就会产生出独一无二
的随机数了。
6,链表:这个是精髓啊,你看那蛇不是就像一个链表吗,这个可是我认为在这个游戏中使用的最高深的结构
了,呵呵。
7,长大:链表头遇上一个食物(随机产生的方块),链表上添加一个节点。
8,死亡:链表头撞上了自身或者撞墙。
也就这么多,理解了这几点,整个框架也就出来了。
下面就是源代码:
#include &stdio.h&
#include &graphics.h&
#include &stdlib.h&
#include &ctype.h&
#define LEN sizeof(struct list)
#define uchar unsigned char
int n,sco,r,t,speed=8800;
uchar i,a,x1,y1,hit,long1=16,cycy=0,str1[200],
struct list
&struct list * next,*
struct list * p1,* p2,* head,* eof1;
up(void);&&&&&&&&&&&&&&&&
void down();
left();&&&&&&&&&&&&&&&&&&
void right();
void unit(uchar,uchar,uchar);& void score();
dl(int);&&&&&&&&&&&&&&&&&
void save1();
load1();&&&&&&&&&&&&&&&&&
void save();&
&int driver=VGA,mode=VGAHI;
&unsigned char ss[8];
&initgraph(&driver,&mode,"c:\\tc");
&setfillstyle(1,8);
&setcolor(8);
&bar(2,2,600,400);
&setfillstyle(1,7);&&&&
&setcolor(7);
bar(117,117,170,140);
&setfillstyle(1,8);&
&setcolor(8);
&bar(118,118,170,140);
&setcolor(14);
&sprintf(ss,"enter");
&outtextxy(120,125,ss);
&setcolor(0);
&line(118,141,171,141);
&line(118,142,172,142);
&line(171,118,171,141);
&line(172,118,172,142);
&setfillstyle(1,14);
&setcolor(14);
&circle(15,15,10); sprintf(ss,"&
fast");&&&&
&outtextxy(30,15,ss);
circle(15,55,10); sprintf(ss,"& normol");
&outtextxy(30,55,ss);
circle(15,95,10); sprintf(ss,"& slow");
&outtextxy(30,95,ss);
&circle(15,15,6);floodfill(15,15,14);xisu=0;
&&&&&&&&&&
&a=getch();
&&if(a==9)&&&&&&&
&&&setcolor(14);setfillstyle(1,14);
&&&a=8;&&&&&&&
&&&xisu++;
&&&circle(15,15+(xisu%3)*40,6);
&&&floodfill(15,15+(xisu%3)*40,14);
&&&setfillstyle(1,7);&&&&&&
&&&setcolor(7);
&&&circle(15,15+((xisu-1)%3)*40,6);
&&&floodfill(15,15+((xisu-1)%3)*40,7);
&&if((a==76)||(a==108))&&&&&&
&&&load1();&&
&&&xisu=2;
while(a!=13);
&setcolor(8);
&line(118,141,171,141);&&&
&line(171,118,171,141);
&dl(speed);
&p2=(struct list
*)malloc(LEN);&&&&&&&&
&hit=1;&&&&&&&&&
&p1=p2; head=p2;
&p2-&x=20;
&p2-&y=20;&
&p2-&last=NULL;&
&p2-&next=NULL;
&rectangle(0,0,600,450);
&setfillstyle(1,0);&
&setcolor(0);
&bar(1,1,599,449);&&&&&
&setfillstyle(1,14);
&setcolor(14);
&speed=speed*(xisu%3+1);
&for(i=0;i&15;i++)&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&p2=(struct list
*)malloc(LEN);
&&p2-&x=21+i;
&&p2-&y=20;&&&&&&
&&unit(21+i,20,14);
&&&&&&&&&&&&&&&
p1-&next=p2;&
&&p2-&last=p1;&&&
&&p2-&next=NULL;
&&eof1=p2;
&unit(head-&x,head-&y,14);
LOOP:&& while(1)
&&if(hit==1)&&&&&
&&&randomize();
&&&x1=random(30);
&&&y1=random(20);
&&&x1=x1+3;
&&&y1=y1+3;
&&&while(p1-&next!=NULL)
&&&&if((x1==p2-&x)&&(y1==p2-&y))&
&&&&&x1=random(30)+3;y1=random(20)+3;p2=p1=p2;&
&&&&&p1=p2;
&&&&&p2=p1-&
&&&unit(x1,y1,14);
&&if(kbhit()!=0)
&&a=getch();
&&switch(a)&&&&&&&&&&&&&&&
&&&case 72 :
&&&case 80 :
&&&case 75 :
&&&case 77 :
&&&case 83
&&&case 115:
dl(500); save1();&&
&&if(cycy==1)
a=getch();
&while((a&14)||(a&13));&&&
&closegraph();
void up(void)
&p2=head-&
&while(p1-&next!=NULL)
&&if((head-&x==p2-&x)&&(head-&y-1==p2-&y))
&&&score();&&&&
&&&p2=p1-&
&if((hit==0)&&(head-&x==x1)&&(head-&y-1==y1))&
&&p2=(struct list
*)malloc(LEN);
&p2-&x=x1;
&&p2-&y=y1;
&&p2-&next=
&&head-&last=p2;
&&head=p2;&&&&&&&&&&&&&&&&&&&&
hit=1;long1++;
&&p2=eof1;
&&eof1=p2-&
&&eof1-&next=NULL;
&&unit(p2-&x,p2-&y,0);
&&p2-&x=head-&x;
&&p2-&y=head-&y-1;
&&p2-&next=&
&&head-&last=p2;
&&head=p2;&
&&head-&last=NULL;
&&unit(p2-&x,p2-&y,14);
&&if(p2-&y&1)
&dl(speed);
void down(void)
&p2=head-&p1=p2;
&while(p1-&next!=NULL)
&&if((head-&x==p2-&x)&&(head-&y+1==p2-&y))&
&&&score();
&&&p2=p1-&
&if((hit==0)&&(head-&x==x1)&&(head-&y+1==y1))&
&&p2=(struct list
*)malloc(LEN);
&p2-&x=x1;p2-&y=y1;
&p2-&next=
&&head-&last=p2;
&&head=p2;&&&
&&long1++;
&&p2=eof1;
&&eof1=p2-&
&&eof1-&next=NULL;
&&unit(p2-&x,p2-&y,0);
&&p2-&next=
&&head-&last=p2;&
&&p2-&x=head-&x;
&&p2-&y=head-&y+1;
&&head=p2;&
&&head-&last=NULL;
&&unit(p2-&x,p2-&y,14);
&&if(p2-&y&=30)&&&&&&&&&&&&
&&&&&&&&&&
&&score();
&dl(speed);
void left(void)
&p2=head-&p1=p2;
&while(p1-&next!=NULL)
&&if((head-&x-1==p2-&x)&&(head-&y==p2-&y))
&&&score();
&&&p2=p1-&
&if((hit==0)&&(head-&x-1==x1)&&(head-&y==y1))
&&p2=(struct list
*)malloc(LEN);
&p2-&x=x1;
&&p2-&y=y1;
&p2-&next=
&&head-&last=p2;
&&head=p2;&&&&&&&&&
&&long1++;
&&eof1=p2-&
&&eof1-&next=NULL;
&&unit(p2-&x,p2-&y,0);
&&p2-&next=
&&head-&last=p2;&
&&p2-&x=head-&x-1;
&&p2-&y=head-&y;
&&head=p2;&
&&head-&last=NULL;
&&unit(p2-&x,p2-&y,14);
&&if(p2-&x&1)
dl(speed);
void right(void)
&p2=head-&p1=p2;
&while(p1-&next!=NULL)
&&if((head-&x+1==p2-&x)&&(head-&y==p2-&y))
&&&score();
&&&p2=p1-&
&if((hit==0)&&(head-&x+1==x1)&&(head-&y==y1))
&&p2=(struct list
*)malloc(LEN);
&p2-&x=x1;
&&p2-&y=y1;
&p2-&next=
&&head-&last=p2;
&&head=p2;
&&long1++;
&&p2=eof1;
&&eof1=p2-&
&&eof1-&next=NULL;
&&unit(p2-&x,p2-&y,0);
&&p2-&next=
&&head-&last=p2;&
&&p2-&x=head-&x+1;
&&p2-&y=head-&y;
&&head=p2;&
&&head-&last=NULL;
&&unit(p2-&x,p2-&y,14);
&&if(p2-&x&=40)
&&score();
&dl(speed);
void dl(int a)
&for(r=0;r&a;r++)
&for(n=0;n&6000;n++)
void unit(uchar x,uchar y,uchar color)
&setfillstyle(1,color);
&setcolor(color);
&bar(x*15,y*15,(x+1)*15-2,(y+1)*15-2);
void score(void)
uchar ss[20];
&if(long1&50)
&sco=(long1-50)*3+(long1-30)*2+14;
&else if(long1&30)
&sco=(long1-30)*2+14;
&sco=long1-16;
&sprintf(ss,"your score is %d",sco);
&outtextxy(50,50,ss);
void save(void)
&fp=fopen("snascore.txt","rb+");
&i=(int)(fgetc(fp))-48;
&if(fp==NULL)
fclose(fp);
&if(i&sco)
&&fp=fopen("snascore.txt","wb+");
&fprintf(fp,"%d",sco);
&fclose(fp);
&printf("\n");
&& &printf("Your
score: %d are the highest!",sco);
void save1()
& &FILE *fl1;
& &dl(1500);
& &while(p1!=NULL)
&&str1[i]=(char)(p1-&x+30);
&str1[i+1]=(char)(p1-&y+30);
&& &i=i+2;
&p2=p1;p1=p2-&
&dl(1500);
&fl1=fopen("snasave.txt","w+");
& &if(fl1!=NULL)
&&fwrite(str1,i,1,fl1);
&&dl(1500);
&&fclose(fl1);
&&printf("bad\n");
& &dl(1500);
& &cycy=1;
void load1()
&FILE *fl1;
&fl1=fopen("snasave.txt","r");
& &if(fl1!=NULL)
&&fseek(fl1,0,SEEK_SET);
&while(feof(fl1)==0)
&&&str1[i++]=fgetc(fl1);
&cc=ftell(fl1);
&fclose(fl1);
&&printf("bad\n");
setcolor(8);
&line(118,141,171,141);&&&
&line(171,118,171,141);
dl(speed);
&p2=(struct list
*)malloc(LEN);&&&&&&&
&p2-&x=(int)(str1[0])-30;
&p2-&y=(int)(str1[1])-30;&
&p2-&last=NULL;&
&p2-&next=NULL;
&rectangle(0,0,600,450);
&setfillstyle(1,0);&
&setcolor(0);
&bar(1,1,599,449);&&&&&
&setfillstyle(1,14);
&setcolor(14);
&speed=speed*(xisu%3+1);
&for(i=2;i&i+=2)
&&p2=(struct list
*)malloc(LEN);
&&p2-&x=(int)(str1[i])-30;
&&p2-&y=(int)(str1[i+1])-30;
&&unit(p2-&x,p2-&y,14);
&&p1-&next=p2;&
&&p2-&last=p1;&&&
&&p2-&next=NULL;
&&eof1=p2;
&long1=cc/2;
&unit(head-&x,head-&y,14);
里面有个速度设置,这样一句话:“speed=8800;”
其实是个延迟时间,各位根据电脑CPU的速度可以改动一下,到你自己舒服为止哦。
原文地址:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。选择栏目:
分享自: |
永远的学童
C语言实现贪吃蛇游戏的代码
本文《C语言实现贪吃蛇游戏的代码》提供学童君学习参考,知识和精力有限,无法一一勘误,若有所错误,可以在文末“反馈”按钮提出反馈。学童网()尽量提供给您准确有价值的信息。
浏览《C语言实现贪吃蛇游戏的代码》小提示:
文章若附带资源/案例下载、效果/视频/动画演示,一般在文章底部资源列表里有提供
文章清晰的要点多数有加粗提示,多留意
涉及到代码地方,有清晰的代码样式。
文章内蓝色字体,多是本文的相关链接。
好文可以收藏到您个人的会员中心
要查阅跟本文相关的大量信息,可以访问专属栏目《》
#include&stdio.h&
#include&conio.h&
#include&windows.h&
#include&stdlib.h&
#include&time.h&
#define Key_Up 'w' // 向上方向键   
#define Key_Down 's' // 向下方向键
#define Key_Right 'd' // 向右方向键
#define Key_Left 'a'
// 向左方向键
#define Key_Space ' '
#define R 1
//向右的状态量
#define L 2
//向左的状态量
#define U 3
//向上的状态量
#define D 4
//向下的状态量
typedef struct node
struct node*
//////////全局变量
int score=0;
int endgamestatus=0;
int food_x,food_y;
snake*//蛇的头结点
snake*p;//遍历蛇身用的指针
int status=R;//蛇状态变量
void endgame();//退出游戏函数
void Pos(int x,int y);//光标定位函数
void crosswall();//判断蛇是否撞到墙壁
void Creat_Food();//生成食物
Bit_Self();//判断蛇头是否与蛇身有接触
void Crat_Map();//生成地图
void Snake_Moving();//蛇身移动
void gamecircle();// 游戏循环
void pause();//游戏暂停
void Pos(int x,int y)
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
Bit_Self()
if(p-&x==head-&x&&p-&y==head-&y)
Creat_Map()
for(i=0;i&=54;i++)
for(j=0;j&=26;j++)
printf("@");
Pos(i,26);
printf("@");
printf("@");
Pos(54,j);
printf("@");
void crosswall()
if(head-&x==0||head-&y==0||head-&x==54||head-&y==26)
endgamestatus=1;
endgame();
void Creat_Food()
srand(time(NULL));
food_x=rand()%50+2;
food_y=rand()%24+2;
Pos(food_x,food_y);
printf("@");
Init_Snake()
head=(snake*)malloc(sizeof(snake));
head-&x=25;
head-&y=5;
head-&next=NULL;
for(i=1;i&4;i++)
tail=(snake*)malloc(sizeof(snake));
tail-&x=25+i*1;
tail-&y=5;
tail-&next=
while(tail)
Pos(tail-&x,tail-&y);
printf("@");
tail=tail-&
void Snake_Moving()
newhead=(snake*)malloc(sizeof(snake));
crosswall();
if(Bit_Self())
endgamestatus=2;
endgame();
if(status==R)//向右走
if(head-&x==food_x&&head-&y==food_y)
score=score+10;
newhead-&x=head-&x+1;
newhead-&y=head-&y;
newhead-&next=
Pos(p-&x,p-&y);
printf("@");
Creat_Food();
newhead-&x=head-&x+1;
newhead-&y=head-&y;
newhead-&next=
while(p-&next-&next)
Pos(p-&x,p-&y);
printf("@");
Pos(p-&next-&x,p-&next-& y);
printf(" ");
free(p-&next);
p-&next=NULL;
if(status==L)
if(head-&x==food_x&&head-&y==food_y)
score=score+10;
newhead-&x=head-&x-1;
newhead-&y=head-&y;
newhead-&next=
Pos(p-&x,p-&y);
printf("@");
Creat_Food();
newhead-&x=head-&x-1;
newhead-&y=head-&y;
newhead-&next=
while(p-&next-&next)
Pos(p-&x,p-&y);
printf("@");
Pos(p-&next-&x,p-&next-& y);
printf(" ");
free(p-&next);
p-&next=NULL;
if(status==D)
if(head-&x==food_x&&head-&y==food_y)
score=score+10;
newhead-&x=head-&x+1;
newhead-&y=head-&y;
newhead-&next=
Pos(p-&x,p-&y);
printf("@");
Creat_Food();
newhead-&x=head-&x;
newhead-&y=head-&y+1;
newhead-&next=
while(p-&next-&next)
Pos(p-&x,p-&y);
printf("@");
Pos(p-&next-&x,p-&next-& y);
printf(" ");
free(p-&next);
p-&next=NULL;
if(status==U)
if(head-&x==food_x&&head-&y==food_y)
score=score+10;
newhead-&x=head-&x;
newhead-&y=head-&y-1;
newhead-&next=
Pos(p-&x,p-&y);
printf("@");
Creat_Food();
newhead-&x=head-&x;
newhead-&y=head-&y-1;
newhead-&next=
while(p-&next-&next)
Pos(p-&x,p-&y);
printf("@");
Pos(p-&next-&x,p-&next-& y);
printf(" ");
free(p-&next);
p-&next=NULL;
void gamecircle()
Pos(57,4);
printf("操作说明");
Pos(57,5);
printf("w a s d分别对应上 左 下 右 ");
Pos(57,6);
printf("按空格键暂停");
Pos(57,7);
printf("游戏分数:%d",score);
if(kbhit())
key=getch();
switch(key)
case Key_Right:
if(status!=L)
case Key_Left:
if(status!=R)
case Key_Up:
if(status!=D)
case Key_Down:
if(status!=U)
case Key_Space:
Sleep(300);
Snake_Moving();
void pause()
if(key=getch()==' ')
void endgame()
system("cls");
Pos(27,13);
if(endgamestatus==1)
printf("您撞到墙了");
if(endgamestatus==2)
printf("您咬到了自己");
Pos(27,14);
printf("您的得分为%d",score);
void welcome()
Pos(27,13);
printf("欢迎来到贪吃蛇游戏");
system("pause");
system("cls");
Pos(50,9);
printf("欢迎大家对源代码进行修改");
Pos(50,11);
printf("开发出更多好玩的玩法");
Pos(50,12);
system("pause");
system("cls");
int main()
welcome();
Creat_Map();
Creat_Food();
Init_Snake();
gamecircle();
学童君~以上就是关于《C语言实现贪吃蛇游戏的代码》的全部内容,更深入了解可以关注更多此方面的信息,学童网()给您提供此文的专有收藏链接,可以点击下方“收藏”按钮收藏佳文。
也可以选择如下操作:
获取和本文密切相关的大量同类型文章,可以访问《》栏目
关心跟此文相关大类栏目可以访问《》栏目,极大拓宽信息面。
如果有,文章相关的资源、代码包、视频、下载链接、演示效果可以在下方的资源列表里找到哦
知识点较多的文章可能会有上/下一页,上/下一篇,多留意。
当前观看编号:关闭
如果你要把《C语言实现贪吃蛇游戏的代码》相关的资源地址通过手机访问,或者要把其中某段信息保存到手机,那么你可以把它复制到上面的框中,通过手机扫一扫就可以啦。
Copyright (C)
All rights reserved. our email[] - Collect from}

我要回帖

更多关于 贪吃蛇游戏下载 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信