有可能delay()函数里的数字大家要自己调整下.基本的结构因为完善了.
#include "graphics.h"
#include "stdlib.h"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int lose=0,i,j;
int gd=DETECT,gr,x[200]={100,110,120,130},y[200]={100,100,100,100};
int key,t=1,n=4,s=0,xx,yy,yes=1;
int score[8]={0};
begain()/*开始画面*/
{
setcolor(11);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
}
over()/*结束的话*/
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
}
win()/*胜利*/
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200," YOU WIN! ");
}
doscore()/*处理分数*/
{
score[5]++;
for(i=5;i>=1;i--)
if(score[i]==10)
{score[i-1]++;score[i]=0;}
}
printscore(int n)/*输出成绩*/
{
if(n==1)
setcolor(YELLOW);
else setcolor(0);
settextstyle(0,0,2);
for(i=0,j=50;i<8;i++,j+=20)
{
switch(score[i])
{
case 0: outtextxy(j,20,"0");break;
case 1: outtextxy(j,20,"1");break;
case 2: outtextxy(j,20,"2");break;
case 3: outtextxy(j,20,"3");break;
case 4: outtextxy(j,20,"4");break;
case 5: outtextxy(j,20,"5");break;
case 6: outtextxy(j,20,"6");break;
case 7: outtextxy(j,20,"7");break;
case 8: outtextxy(j,20,"8");break;
case 9: outtextxy(j,20,"9");break;
}
}
}
main()
{
initgraph(&gd,&gr,"c:\\tc");
cleardevice();
begain();
while(1)
{
while(!kbhit())
{
randomize();
printscore(1);/*输出得分*/
if(yes)/*出现要吃的东西*/
{
while(1)/*判定吃的东西是否在蛇的身上*/
{
xx=rand()%400+60;
yy=rand()%350+60;
while(xx%10!=0)
xx++;
while(yy%10!=0)
yy++;
for(i=0;i<n;i++)
if(xx==x[i]&&yy==y[i])
break;
if(i>=n)
break;
}
yes=0;
}
if(yes==0)/*画要吃的东西*/
{
setcolor(GREEN);
rectangle(xx,yy,xx+10,yy-10);
}
for(i=n-1;i>0;i--)/*每个往前移动*/
{
x[i]=x[i-1];
y[i]=y[i-1];
}
if(t==1)/*right*/
x[0]+=10;
else if(t==2)/*left*/
x[0]-=10;
else if(t==3)/*up*/
y[0]-=10;
else if(t==4)/*down*/
y[0]+=10;
for(i=3;i<n;i++)
if(x[i]==x[0]&&y[i]==y[0])/*蛇的身体是否自己接触*/
{
over();
lose=1;
break;
}
if(x[0]<55||x[0]>595||y[0]<55||y[0]>455)/*失败条件*/
{
over();
lose=1;
}
if(lose)
break;
if(x[0]==xx&&y[0]==yy&&n<200)/*长身体*/
{
setcolor(0);/*把要吃的东西去掉*/
rectangle(xx,yy,xx+10,yy-10);
printscore(2);/*输出得分,这里是把原来的分数覆盖*/
doscore();
n++;
if(t==1)
{
x[n-1]=x[n-2]-10;
y[n-1]=y[n-2];
}
else if(t==2)
{
x[n-1]=x[n-2]+10;
y[n-1]=y[n-2];
}
else if(t==3)
{
y[n-1]=y[n-2]+10;
x[n-1]=x[n-2];
}
else if(t==4)
{
y[n-1]=y[n-2]-10;
x[n-1]=x[n-2];
}
yes=1;
}
if(n==199)
{
lose=1;
win();/*胜利*/
}
setcolor(4);/*画出来*/
for(i=0;i<n;i++)
rectangle(x[i],y[i],x[i]+10,y[i]-10);
delay(5000);
setcolor(0);
for(i=0;i<n;i++)
rectangle(x[i],y[i],x[i]+10,y[i]-10);
}
if(lose)
break;
key=bioskey(0);/*按键的影响*/
if(key==ESC)
break;
else if(key==UP&&t!=4)
t=3;
else if(key==RIGHT&&t!=2)
t=1;
else if(key==LEFT&&t!=1)
t=2;
else if(key==DOWN&&t!=3)
t=4;
}
getch();
closegraph();
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |