/*要求改进:用图形模式表示print()函数*/
#include <mem.h>
#include <fcntl.h>
#include <conio.h>
#include <graphics.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <bios.h>
#include <math.h>
#define N 50
#define Time 10000
int A[N][N];
int IE=6;
int SCORE=0;
char a[]="9999";
void *ball;
int maxx;
unsigned int size;
int head=1;/*0,1,2,3*/
int i=3,j=0,ii=0,jj=0;
int length=3;
int x,y;/*蛇头标志*/
int add=1;/*食物标志*/
int tii,tjj;/* 前蛇尾坐标 */
void start_image()
{
setlinestyle(SOLID_LINE,1,2);
setcolor(GREEN);
line(39,39,39,441);
line(39,441,441,441);
line(441,441,441,39);
line(441,39,39,39);
}
void draw_image(int x,int y)
{
int i=x*400/N+40;
int j=y*400/N+40;
setfillstyle(1,4);
bar(j,i,j+400/N,i+400/N);
}
void eraser_image(int x,int y)
{
int i=x*400/N+40;
int j=y*400/N+40;
setfillstyle(1,0);
bar(j,i,j+400/N,i+400/N);
}
void tu()
{/*tuxing jiemian*/
maxx=getmaxx();
size=imagesize(210,390,450,420);
ball=malloc(size);
setfillstyle(1,10);
bar(160,170,473,253);
setfillstyle(1,9);
bar(163,173,470,250);
setcolor(13);
outtextxy(200,200,"Welcome to Greedy Snake Game");
setcolor(14);
outtextxy(250,220,"Developer :Wu siyuan ");
outtextxy(290,240,"2003.5");
outtextxy(210,390,"Press any key to contunue...");
getimage(210,390,450,420,ball);
while(!kbhit())
{/* 没有键盘击键 */
putimage(210,390,ball,XOR_PUT);
delay(30000);
}
cleardevice();
}
void print()/*输出*/
{ /*要将转化成图形模式*/
int a,b;
/*start_image(); */
for(a=0;a<N;a++)
{ for(b=0;b<N;b++)
{if(A[a][b]>1)draw_image(a,b);
if(A[a][b]==0)eraser_image(a,b); }
}
}
void snake() /*begin*/
{
int a,b;
for(a=0;a<N;a++)
for(b=0;b<N;b++)
A[a][b]=0;
A[i][j]=5;A[i-1][j]=4;A[i-2][j]=3;A[i-3][j]=2;
}
void del_tail()/*蛇尾的操作*/
{
int number=A[ii][jj];
tii=ii;tjj=jj;/*记录前蛇尾*/
while(1)
{
if(A[ii][jj-1]==number+1||A[ii][jj-1]==2&&number==30)
{jj--;break;}
if(A[ii][jj+1]==number+1||A[ii][jj+1]==2&&number==30)
{jj++;break;}
if(A[ii-1][jj]==number+1||A[ii-1][jj]==2&&number==30)
{ii--;break;}
if(A[ii+1][jj]==number+1||A[ii+1][jj]==2&&number==30)
{ii++;break;}
}
}
void down()
{
head=1;
if(A[i+1][j]>1&&A[i+1][j]!=99)
{printf("I hit myself!");exit(1);}
i++;
if(IE>30)IE=2;
A[i][j]=IE;IE++;
del_tail();
A[tii][tjj]=0;
}
void up()
{
head=0;
if(A[i-1][j]>1&&A[i-1][j]!=99)
{printf("I hit myself!");exit(1);}
i--;
if(IE>30)IE=2;
A[i][j]=IE;IE++;
del_tail();
A[tii][tjj]=0;
}
void left()
{
head=2;
if(A[i][j-1]>1&&A[i][j-1]!=99)
{printf("I hit myself!");exit(1);}
j--;
if(IE>30)IE=2;
A[i][j]=IE;IE++;
del_tail();
A[tii][tjj]=0;
}
void right()
{
head=3;
if(A[i][j+1]>1&&A[i][j+1]!=99)
{printf("I hit myself!");exit(1);}
j++;
if(IE>30)IE=2;
A[i][j]=IE;IE++;
del_tail();
A[tii][tjj]=0;
}
void test()/*测试是否到达边界*/
{
if(i<0||i>N-1||j<0||j>N-1)
{
printf("出界! Game over.");
exit(1);
}
}
void move()/*当前移动*/
{
switch(head)
{
case 0:up();break;
case 1:down();break;
case 2:left();break;
case 3:right();break;
/*default:printf("方向出错!\n");*/
}
}
int keyboard()
{
/*判断是否有键盘输入
// 有返回值0,1,2,3
// int k=keyboard;
//判断方向是否是有效方向
//无效则什么也不做
//若方向有效则改变head的值*/
int a,key,h;
for(a=0;a<2;a++)
if(bioskey(1))break;
key=bioskey(0);
switch (key)
{
/*up*/
case 18432:h=0;break;
/*down*/
case 20480:h=1;break;
/*left*/
case 19200:h=2;break;
/*right*/
case 19712:h=3;break;
/*Esc*/
case 283:printf("You press the Esc key!");exit(1);break;
/*default: h=999 ;*/
}
return h;
}
void add_point()/* 用于产生食物 */
{
while(1){
x=random(100)%N;
y=random(100)%N;
if(A[x][y]==0)break;
}
A[x][y]=99;/*食物的标志是5*/
add=0;/* 加食物标志清0*/
}
void score()
{
setcolor(9);
settextstyle(3,0,4);
outtextxy(450,40,"Score:");
setcolor(RED);
outtextxy(450,200,"Speed:");
}
void number_char(int x)
{
int a0,a1,a2,a3;
if(x>9999){a[0]='9';a[1]='9';a[2]='9';a[3]='9';}
else {
a0=x/1000;
a[0]=a0+48;
a1=(x-a0*1000)/100;
a[1]=a1+48;
a2=(x-a0*1000-a1*100)/10;
a[2]=a2+48;
a3=(x-a0*1000-a1*100-a2*10);
a[3]=a3+48;
a[4]='\0';
}
}
void snake_get()/*吃到食物时*/
{
if(i==x&&j==y)
{
length++;
A[tii][tjj]=A[ii][jj]-1;/*蛇尾加长*/
ii=tii;jj=tjj;/*蛇尾重新定义*/
add=1;/*食物标志置1*/
setfillstyle(1,BLACK);
bar(520,100,650,140);/*clear score*/
setcolor(3);
number_char(SCORE+=100);
outtextxy(550,100,a);
}
}
void main()
{
int get_head;/*定义键盘输入*/
int gd=DETECT,gm;
clrscr();
/*regibgiobjdriver(public name)*/
initgraph(&gd,&gm,"c:\\bgi");
setbkcolor(BLACK);
tu();
snake();/*初始化*/
start_image();
score();/*定义分数界面*/
while(1)
{ while(bioskey(1)==0)/*是否有键盘输入*/
{
if(add)add_point();/*是否可以增加食物*/
print();
move();
snake_get();/* 是否吃到食物 (test)*/
test();
delay(Time);
}
get_head=keyboard();
/* choose */
while(1){/*判断得到的坐标是否合法*/
if(get_head==0&&head==1)break;
if(get_head==1&&head==0)break;
if(get_head==2&&head==3)break;
if(get_head==3&&head==2)break;
else {head=get_head;break;}/*合法给head赋值*/
}
}
}