9159.comvoidcolor //颜色函数,c语言之贪吃蛇

作者: 编程  发布:2019-12-28

#includestdio.h#includestring.h#includewindows.h#includetime.h#includeconio.h#defineup'w'#definedown's'#defineleft'a'#defineright'd'#definestop'P'voidwelcome();//55开始界面voidFinish();//291结束界面voidcreategraph();//69围墙打印voidgotoxy(intx,inty);//111光标跳转,横为X0,1,2..voidgotoprint(intx,inty);//121跳转打印voidgotodelete(intx,inty);//127跳转删除voidcreatefood();//133食物产生intClickControl();//157获取键盘信号intJudge();//270游戏结束判断voidMovingBody();//172蛇的移动voidEatting();//223蛇吃到东西后的操作voidChangeBody(inta,intb);//245蛇的坐标变换,后一个复制前一个STRUCT,a,b为head之前坐标/*全局变量+预处理:*/typedefstructSnakes{intx;inty;structSnakes*next;}snake;snake*head,*tail;structFood{intx;inty;}food;charname[20];intscore=0;charclick=1;intspeed;intmain(){system("color3E");welcome();creategraph();createfood();if(ClickControl()==0)return0;return0;}voidwelcome(){gotoxy(15,10);printf(");gotoxy(15,20);printf("");gotoxy(20,13);printf("WELCOMETOTHEGAME:贪吃蛇");gotoxy(14,16);printf("请在英文输入法中操作,相反方向移动则直接游戏结束,wasd控制,p暂停");gotoxy(20,18);printf("你的游戏名:");scanf_s("%s",name,10);system("cls");}voidcreategraph(){inti;for(i=0;i58;i+=2)//打印上下边框{gotoprint(i,0);gotoprint(i,26);}for(i=1;i26;i++){gotoprint(0,i);gotoprint(56,i);}gotoxy(63,10);printf("你好,%s,你正在玩贪吃蛇",name);gotoxy(63,15);printf("你的当前得分是:%d",score);gotoxy(63,20);head=(snake*)malloc(sizeof(snake));head-x=16;head-y=15;//gotoprint(head-x,head-y);tail=(snake*)malloc(sizeof(snake));snake*p=(snake*)malloc(sizeof(snake));snake*q=(snake*)malloc(sizeof(snake));p-x=16;p-y=16;q-x=16;q-y=17;head-next=p;p-next=q;q-next=tail;tail-next=NULL;}/**********************************************************/voidgotoxy(intx,inty){COORDpos;HANDLEhOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);}/**********************************************************/voidgotoprint(intx,inty){gotoxy(x,y);printf("");}/**********************************************************/voidgotodelete(intx,inty){gotoxy(x,y);printf("");}/**********************************************************/voidcreatefood(){srand((int)time(NULL));lable:food.y=rand()%(25-1+1)+1;food.x=rand()%(54-2+1)+2;if(food.x%2!=0){food.x=food.x+1;}snake*judge=head;while(1)//遍历排除蛇身重复{if(judge-next==NULL)break;if(food.x==judge-xfood.y==judge-y){gotolable;}judge=judge-next;}gotoxy(food.x,food.y);printf("¥");}/**********************************************************/intClickControl(){charc;while(1){if(Judge()==0)return0;if(_kbhit()){click=_getch();}MovingBody();Eatting();}return1;}/**********************************************************/voidMovingBody(){intcount=0;inta=head-x,b=head-y;snake*p=head;//通过先清空后打印实现动画效果while(1){if(p-next==NULL)break;gotodelete(p-x,p-y);count++;p=p-next;}switch(click){caseup:head-y-=1;ChangeBody(a,b);break;casedown:head-y+=1;ChangeBody(a,b);break;caseleft:head-x-=2;ChangeBody(a,b);break;caseright:head-x+=2;ChangeBody(a,b);break;casestop:break;}p=head;while(1){if(p-next==NULL)break;gotoprint(p-x,p-y);p=p-next;}p=head;gotoxy(0,28);if(count=10)speed=150;elseif(count10count=20)speed=100;elseif(count20count=40)speed=50;elsespeed=10;Sleep(speed);}/**********************************************************/voidEatting(){if(head-x==food.xhead-y==food.y){createfood();snake*_new=(snake*)malloc(sizeof(snake));snake*p;p=head;while(1){if(p-next-next==NULL)break;p=p-next;}p-next=_new;_new-next=tail;score+=10;gotoxy(77,15);printf("%d",score);}}/**********************************************************/voidChangeBody(inta,intb){snake*p=head-next;intmid1,mid2,_mid1,_mid2;mid1=p-x;mid2=p-y;while(1){if(p-next-next==NULL)break;_mid1=p-next-x;_mid2=p-next-y;p-next-x=mid1;p-next-y=mid2;mid1=_mid1;mid2=_mid2;p=p-next;}p=head-next;//if(p-next!=NULL){p-x=a;p-y=b;}}/**********************************************************/intJudge(){if(head-x==0||head-x==56||head-y==0||head-y==26){Finish();return0;}snake*p=head-next;while(1){if(p-next==NULL)break;if(head-x==p-xhead-y==p-y){Finish();charq;printf("是否继续?y/n");q=getchar();if(q=='y')main();elseif(q=='n')return0;}p=p-next;}return1;}/**********************************************************/voidFinish(){system("cls");gotoxy(15,10);printf("····································");gotoxy(15,20);printf("····································");gotoxy(18,14);printf("游戏结束下次加油(•w•ଓ)=33");gotoxy(20,16);printf("最终得分:%d挺不错的",score);gotoxy(18,18);printf("还不错哦,继续努力(ŏ﹏ŏ)");gotoxy(0,27);system("pause");printf("");}

[代码运行效果截图]

9159.com 1

关于c语言的贪吃蛇游戏问题,为什么最后编译出错?
/* c语言之贪吃蛇 */

9159.com 2

#includeiostream#includegraphics.h#includeconio.h#includemath.h#includewindows.husingnamespacestd;intexamine(inta[]);//数组是否整齐;intretemp(intx,inty);intexamine(inta[]){inti=0;for(i=0;i10;i++){if(a[i]!=i)return0;}return1;}intf[11]={1,1,2,6,24,120,720,5040,40320,362880,3628800};intcurrent_kangtuo;intkangtuo(intstatus[]);typedefstructshuzu{intdate;structshuzu*next;}shuzu;typedefstructnode{intdate;structnode*next;}node;voidpushshuzu(shuzu*shuzuhead,intn);voidpushshuzu(shuzu*shuzuhead,intn){shuzu*s;s=(shuzu*)malloc(sizeof(shuzu));s-date=n;s-next=shuzuhead-next;shuzuhead-next=s;}voidpushnode(node*head,intn);voidpushnode(node*head,intn){node*s;s=(node*)malloc(sizeof(node));s-date=n;s-next=head-next;head-next=s;}intstatus[10]={1,5,6,7,3,2,0,4,8,9};//{1,0,2,3,4,5,6,7,8,9};intcoordinate[10][2]={{1,-76},{0,0},{100,0},{200,0},{0,76},{100,76},{200,76},{0,152},{100,152},{200,152}};intnext_num[10][4]={//左、上、右、下{-1,-1,-1,1},{-1,0,2,4},{1,-1,3,5},{2,-1,-1,6},{-1,1,5,7},{4,2,6,8},{5,3,-1,9},{-1,4,8,-1},{7,5,9,-1},{8,6,-1,-1}};voidbackward(node*head);intforward(shuzu*shuzuhead,node*head);voidauto_deep_play(intcurrent_status[],shuzu*shuzuhead,node*head);intcount1=0;IMAGEimage[10];//图片指针TCHARs[5];voidshow();intmain(){shuzu*shuzuhead=(shuzu*)malloc(sizeof(shuzu));shuzuhead-next=NULL;MOUSEMSGm;//定义一个鼠标消息node*head;head=(node*)malloc(sizeof(node));head-next=NULL;loadimage(image[0],_T("0.jpg"));loadimage(image[1],_T("1.jpg"));loadimage(image[2],_T("2.jpg"));loadimage(image[3],_T("3.jpg"));loadimage(image[4],_T("4.jpg"));loadimage(image[5],_T("5.jpg"));loadimage(image[6],_T("6.jpg"));loadimage(image[7],_T("7.jpg"));loadimage(image[8],_T("8.jpg"));loadimage(image[9],_T("9.jpg"));inti=0;inttemp;inttemp1=-1;inttemp2;intchoic;cout"请输入你选择的游戏模式";cinchoic;initgraph(680,680);//初始化绘图窗口setorigin(0,76);//设置坐标原点cleardevice();//清屏////closegraph();switch(choic){case1:for(inti=0;i=9;i++){putimage(coordinate[i][0],coordinate[i][1],image[status[i]]);}while(1){m=GetMouseMsg();if(m.uMsg==WM_LBUTTONDOWN){temp2=retemp(m.x,m.y);if(temp2==-1)break;for(i=0;i4;i++){if(status[next_num[temp2][i]]==0)//第i号位置上下左右的status是否有等于0的;{swap(status[next_num[temp2][i]],status[temp2]);count1++;break;}//返回几号位置上下左右的位置}//Sleep(10);show();temp=examine(status);//检查数组是否排序if(temp==1){closegraph();cout"你走了"count1endl;break;}}}break;case2:for(inti=0;i=9;i++){putimage(coordinate[i][0],coordinate[i][1],image[status[i]]);}auto_deep_play(status,shuzuhead,head);break;}system("pause");}voidauto_deep_play(intcurrent_status[],shuzu*shuzuhead,node*head)//深度优先自动运行模式{current_kangtuo=kangtuo(current_status);//求当前的康拓展开值while(current_kangtuo!=1)//已整齐{if(current_status[0]!=0)//深度优先向前{forward(shuzuhead,head);Sleep(300);//暂停显示cleardevice();//清屏for(inti=0;i=9;i++){putimage(coordinate[i][0],coordinate[i][1],image[status[i]]);}_stprintf_s(s,_T("%d"),count1);outtextxy(300,400,s);if(examine(status)==1)//检测是否走整齐了{closegraph();cout"你走了"count1endl;break;}//向前//如果当前状态为终止状态,游戏成功,结束。}else//向前失败,则回退{if(head-next!=NULL){backward(head);cleardevice();//清屏for(inti=0;i=9;i++){putimage(coordinate[i][0],coordinate[i][1],image[status[i]]);}_stprintf_s(s,_T("%d"),count1);outtextxy(300,400,s);}else{//栈已空的情况closegraph();cout"游戏无解,结束"endl;}}}}voidshow(){cleardevice();//清屏for(inti=0;i=9;i++){putimage(coordinate[i][0],coordinate[i][1],image[status[i]]);}_stprintf_s(s,_T("%d"),count1);outtextxy(300,400,s);}intforward(shuzu*shuzuhead,node*head)//shuzu是用来记录康拓,head记录压栈{inti;inttemp=0;shuzu*e;e=(shuzu*)malloc(sizeof(shuzu));for(i=0;i10;i++)//根据当前状态,找到0号图片所在位置,并求出在d方向下的邻近位置;{if(status[i]==0)break;}//0号图片所在的位置;for(intd=0;d4;d++)//在选择方向时,可以适当加入启发,实现优化{//第i号位置的上下左右是否等于-1,不等于-1即可走;if(status[next_num[i][d]]!=-1)//d方向是合法的图片位置,则尝试向前走{swap(status[next_num[i][d]],status[i]);//尝试移动图片;current_kangtuo=kangtuo(status);//移动后的康拓拓展e=shuzuhead;while(e-next!=NULL){if(e-date==current_kangtuo){temp=-1;//如果这个数组里面有这个状态break;}e=e-next;}Sleep(300);show();if(temp==-1)//这个状态已经遍历过,不能再遍历,换回来{swap(status[next_num[i][d]],status[i]);//还原状态,d方向不可走temp=0;}else//沿d方向向前,压栈{pushshuzu(shuzuhead,current_kangtuo);//保存这个temp,表示已经走过这个状态pushnode(head,d);////d方向压栈count1++;//已走步数加1//更新当前0号图片位置,标志当前状态为已访问状态kangtuo_status[当前状态对应的康拓展开值]temp=0;return1;}}}return0;//没有一个方向可走,前进失败}voidbackward(node*head)//后退{inti;node*s=(node*)malloc(sizeof(node));s=head-next;inttemp=s-date;if(temp==0)temp=2;elseif(temp==1)temp=3;elseif(temp==2)temp=0;elsetemp=1;for(i=0;i10;i++){if(status[i]==0)break;}//0号图片所在的位置;swap(status[next_num[i][temp]],status[i]);head-next=s-next;count1++;//根据当前状态以及d方向的方向,退回到上一个状态,已走步数加1free(s);}intkangtuo(intstatus[])//求康拓展开值{intans=0;intn=10;for(inti=0;in+1;i++){intlessnum=0;for(intj=i+1;jn+1;j++)if(status[j]status[i])lessnum++;ans+=lessnum*f[n-i];}returnans+1;}intretemp(intx,inty)//游戏模式{inttemp2;if(0=x100x0=y76y)temp2=0;elseif(100=x200x76=y76+76y)temp2=2;elseif(0=x100x76+76=y152+76y)temp2=4;elseif(0=x100x0+76=y76+76y)temp2=1;elseif(200=x300x0+76=y76+76y)temp2=3;elseif(100=x200x76+76=y152+76y)temp2=5;elseif(200=x300x76+76=y152+76y)temp2=6;elseif(0=x100x152+76=y228+76=y)temp2=7;elseif(100=x200x152+76=y228+76=y)temp2=8;elseif(200=x300x152+76=y228+76=y)temp2=9;elsereturn-1;returntemp2;}华容道拼图问题:为什么第三轮的时候status[4]会变成3266880那个大佬能调试一下吗?成

#include
#include
#include
#include
#include
#include

[c]代码库

#define N 24
int apple[3]; //食物
char score[3]; //分数
char tail [3];//蛇尾前面一节

#include

//输出坐标
void gotoxy(int x,int y)
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); //定义一个结构体pos,x,y就是构成的成员

#include

}

#include

// 判断出界
int Block(char head[2])
{
if(||)
return 1;
return 0;
}
//颜色函数
void color
{
HANDLE hConsole =GetStdHandle((STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(hConsole,b);//调用API设置字体和背景的颜色函数
}

#include

//界面初始化
void init(char plate[N+2][N+2],char ***snake_x,int len)
{
int i,j;
char
*snake=NULL;
*len=3;
score[0]=score[3]=3;
snake =(char *)realloc(snake,sizeof(char );
for(i=0;i<
len;i++)
snake[i]=malloc(sizeof;
for(i=0;i<3;++i)
{
snake[i][0]=N/2+1;
snake[i][1]=N/2+1+i;

#include

}for(i=1;i<=N;++i)for(j=1;j<=N;++j) plate[i][j]=1;apple[0]=read()%N+1;apple[1]=read()%N+1;apple[2]= 1; for(i=0;i<N+2;++i) { gotoxy; for(j=0;j<N+2;++j) { switch(plate[i][j]) { case 0: color; printf; continue; case 1: color; printf; continue; default:; } }printf;

#include

}
for(i=0;i<;++i)
{
gotoxy(snake[i][1]*2,snake[i][0]);
printf;
}
printf;
*snake_x=snake;
}
void Manual()
{
gotoxy;
color;
printf("按W,D,S,A移动方向");
gotoxy;
printf("按SPACE暂停");
gotoxy;
color;
printf("历史最高分为:");
gotoxy;
printf("你当前的得分为:");
}
//键盘控制
char **Move(char **snake,char dirx,int *len)
{
int i,full=Eat;
memcpy(tail,snake[-1],2);
for-1;i>0;--i )
memcpy(snake[i],snake[i-1],2);
switch
{
case'w':
case'W':
--snake[0][0];
break;
case'a':
case'A':
--snake[0][1];
break;
case's':
case'S':
++snake[0][0];
break;
case'd':
case'D':
++snake[0][1];
break;
default:;

#define N 21

 } if { snake=realloc(snake,sizeof*; snake[]=malloc(sizeof; memcpy(snake[],tail,2); ++; ++score[0]; if(score[3]<16) ++score[3]; tail[2]=1; } else tail[2]=0; return snake;

//●★

}

intapple[3];

//取记录积分
int File_in()
{
FILE *fp;
if((fp=fopen("C:tcs.txt","a+"))==NULL)
{
gotoxy;
printf("文件不能打开n");
exit;
}
if((score[1]=fgetc!=EOF)
score[1]=1;
else
score[1]=0;
return 0;

charscore[3];

}

chartail[3];

//存数据
int Fie_out()
{
FILE *fp;
if(score[1]>score[0])
{
gotoxy;
color;
puts("闯关失败 加油吧 骚年");
gotoxy;
return 0;
}
if((fp=fopen("C:tcs.txt","w+"))==NULL)
{
printf("文件不能打开n");
exit;
}
if(fputc(--score[0],fp)==EOF)
printf;
gotoxy;
color;
puts;
gotoxy;
return 0;
}

voidgotoxy(intx, inty) //输出坐标

//释放空间
void Free(char**snake,int len)
{
int i;
for(i=0;i<len;i++)
free;
free;
}

{

//吃食物
int Eat(char snake[2])
{
if(snake[0]==apple[0]&&snake[1]==apple[1])
{
apple[0]=apple[1]=apple[2]=0;
gotoxy;
printf("%d",score[0]*10);
return 1;

COORD pos;//WINDOWS API中定义的一个结构表示一个字符在控制台屏幕上的坐标

} return 0;

pos.X = x;

}

pos.Y = y;

//蛇移动
void Draw(char **snake,int len)
{
if
{
gotoxy(apple[1*2],apple[0]);
printf;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}if{ printf; } else printf; gotoxy(snake[0][1]*2,snake[0][0]); printf; printf;

//使光标 到这个位置的函数.

}

}

int main(int argc, char *argv[])
{
int len;
char ch='g';
char a[N+2][N+2]={{0}};
char **snake;
srand(time;
color;
File_in();
init(a, &snake, &len);
Manual();

voidcolor //颜色函数

while //按esc键结束
{
Draw(snake,len);
if(!apple[2])
{
apple[0]=read()%N+1;
apple[1]=read()%N+1;
apple[2]=1;
}
Sleep(200-score[3]*10);
setbuf(stdin,NULL);
if
{
gotoxy;
ch=getche();
}
snake=Move(snake,ch,&len);
if(Block==1)
{
gotoxy;
printf;
File_out();
Free(snake,len);
getche();
return 1;

{

 } } Free(snake,len); exit;return 0;

HANDLEhConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;//用来修改命令行窗口的颜色

}

SetConsoleTextAttribute(hConsole,b) ;

}

intBlock(charhead[2]) //判断出界

{

if((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))

return1;

return0;

}

intEat(charsnake[2]) //吃了苹果

{

if((snake[0] == apple[0]) && (snake[1] == apple[1]))

{

apple[0] = apple[1] = apple[2] = 0;

gotoxy;

color;

printf("%d",score[0]*10);

color;

return1;

}

return0;

}

voidDraw(char**snake, intlen) //蛇移动

{

if

{

gotoxy(apple[1] * 2, apple[0]);

color;

printf;

color;//轨迹颜色

}

gotoxy(tail[1] * 2, tail[0]);

if

{

color;

printf;

color;

}

else

printf;

gotoxy(snake[0][1] * 2, snake[0][0]);

color;

printf;

color;

putchar;

}

char** Move(char**snake, chardirx, int*len) //控制方向

{

inti, full = Eat;

memcpy(tail, snake[-1], 2);

for(i = - 1; i > 0; --i)

memcpy(snake[i], snake[i-1], 2);

switch

{

case'w': case'W': --snake[0][0]; break;

case's': case'S': ++snake[0][0]; break;

case'a': case'A': --snake[0][1]; break;

case'd': case'D': ++snake[0][1]; break;

default: ;

}

if

{

snake = realloc(snake, sizeof * + 1));

snake[] = malloc(sizeof * 2);

memcpy(snake[], tail, 2);

++;

++score[0];

if(score[3] < 16)

++score[3];

tail[2] = 1;

}

else

tail[2] = 0;

returnsnake;

}

voidinit(charplate[N+2][N+2], char***snake_x, int*len) //初始化

{

inti, j;

char**snake = NULL;

*len = 3;

score[0] = score[3] =3;

snake = realloc(snake, sizeof * ;

for(i = 0; i < *len; ++i)

snake[i] = malloc(sizeof * 2);

for(i = 0; i < 3; ++i)

{

snake[i][0] = N/2 + 1;

snake[i][1] = N/2 + 1 + i;

}

for(i = 1; i <= N; ++i)

for(j = 1; j <= N; ++j)

plate[i][j] = 1;

apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;

apple[2] = 1;

for(i = 0; i < N + 2; ++i)

{

gotoxy;

for(j = 0; j < N + 2; ++j)

{

switch(plate[i][j])

{

case0:

color;printf;color; continue;

case1: printf; continue;

default: ;

}

}

putchar;

}

for(i = 0; i < ; ++i)

{

gotoxy(snake[i][1] * 2, snake[i][0]);

printf;

}

putchar;

*snake_x = snake;

}

voidManual()

{

gotoxy;

color;

printf("按 W S A D 移动方向");

gotoxy;

printf("按 space 键暂停");

gotoxy;

color;

printf("历史最高分为: ");

color;

gotoxy;

printf("%d",score[1]*10);

color;

gotoxy;

printf("你现在得分为: 0");

}

intFile_in() //取记录的分数

{

FILE*fp;

if((fp = fopen("C:\tcs.txt","a+")) == NULL)

{

gotoxy(N+18, N+2);

printf("文件不能打开n");

exit;

}

if((score[1] = fgetc != EOF);

else

score[1] = 0;

return0;

}

intFile_out() //存数据

{

FILE*fp;

if(score[1] > score[0])

{gotoxy;

color;

puts("Demi 加油哦");

gotoxy;

return0;

}

if((fp = fopen("C:\tcs.txt","w+")) == NULL)

{

printf("文件不能打开n");

exit;

}

if(fputc(--score[0],fp)==EOF)

printf;

gotoxy;

color;

puts("恭喜Demi打破记录");

gotoxy;

return0;

}

voidFree(char**snake, intlen) //释放空间

{

inti;

for(i = 0; i < len; ++i)

free;

free;

}

intmain

{

intlen;

charch = 'g';

chara[N+2][N+2]9159.com, = {{0}};

char**snake;

srand(time;

color;

File_in();

init(a, &snake, &len);

Manual();

while(ch != 0x1B) // 按 ESC 结束

{

Draw(snake, len);

if(!apple[2]) {

apple[0] = rand()%N + 1;

apple[1] = rand()%N + 1;

apple[2] = 1;

}

Sleep(200-score[3]*10);

setbuf(stdin, NULL);

if

{

gotoxy;

ch = getche();

}

snake = Move(snake, ch, &len);

if(Block==1)

{

gotoxy;

puts;

File_out();

Free(snake, len);

getche();

exit;

}

}

Free(snake, len);

exit;

}

9159.com 39159.com 49159.com 5

感觉大家一直以来的支持,谢谢

本文由9159.com发布于编程,转载请注明出处:9159.comvoidcolor //颜色函数,c语言之贪吃蛇

关键词: