www.pudn.com > 可以休矣.zip > 111.cpp


//  贪吃蛇源程序,tc20版 
#include  
#include  
#include  
#include  
#include  
#include  
 
 
#define MAX 80 
#define MAXX 28 
#define MAXY 35 
#define UP 18432 
#define DOWN 20480 
#define LEFT 19200 
#define RIGHT 19712 
#define ESC 283 
#define KEY_W 4471 
#define KEY_S 8051 
#define KEY_A 7777 
#define KEY_D 8292 
#define KEY_F12 7181 
#define ENTER 7181 
#define PAGEUP 18688 
#define PAGEDOWN 20736 
#define TRUE 1 
#define FALSE 0 
 
 
struct snake 
{ 
int x; 
int y; 
int st; 
} place[MAX]; 
int score; 
int control; 
int head; 
int tear; 
int eat,flag; 
int x,y; 
int bx,by; 
int opt=0; 
int game; 
 
 
main() 
{ 
void begin(int maxx,int maxy); 
void option(); 
void end(); 
void init(void); 
void next(void); 
void draw(void); 
void draw2(void); 
void good(void); 
void clear_key(void); 
void startgame(void); 
void music(void); 
 
int gdriver,gmode; 
int maxx; 
int maxy; 
int p=1; 
int c; 
gdriver=DETECT; 
 
initgraph(&gdriver,&gmode,"c:\\tc20");/*这里要设置你的tc的目录,否则不能打开图形模式*/ 
maxx=getmaxx(); 
maxy=getmaxy(); 
begin(maxx,maxy); 
setviewport(0,0,maxx,maxy,0); 
do{ 
setcolor(LIGHTGRAY); 
setfillstyle(1,LIGHTGRAY); 
circle(maxx/2-80,maxy/2+(p-1)*25,8); 
floodfill(maxx/2-80,maxy/2+(p-1)*25,LIGHTGRAY); 
 
c=getch(); 
if(c==13)break; 
setcolor(LIGHTBLUE); 
setfillstyle(1,LIGHTBLUE); 
circle(maxx/2-80,maxy/2+(p-1)*25,8); 
floodfill(maxx/2-80,maxy/2+(p-1)*25,LIGHTBLUE); 
if(p==3)p=1; 
else p++; 
 
}while(1); 
switch(p) 
{ 
 case 1: startgame();break; 
 case 2: option(); 
  startgame(); 
  end(); break; 
 case 3: 
  end(); 
  break; 
} 
} 
 
 
void begin(int maxx,int maxy) 
{ 
int i,j; 
int r=10; 
setbkcolor(9); 
setcolor(7); 
rectangle(10,10,623,470); 
setfillstyle(1,7); 
floodfill(9,9,7); 
settextstyle(4,0,6); 
outtextxy(maxx/3,maxy/2,"By Smallbird"); 
settextstyle(0,0,2); 
outtextxy(180,450,"Press any key to continue...."); 
outtextxy(30,maxy/2+120," Class 1 Computer Scientes"); 
outtextxy(30,maxy/2+140," ID: 2001374126"); 
outtextxy(30,maxy/2+160," Name: Situ Zhiyuan"); 
outtextxy(15,15," Copyright(c) 2002 by SCAU Bird"); 
while(!kbhit()) 
{ 
setcolor(13); 
settextstyle(0,0,10); 
outtextxy(maxx/5,maxy/5-10,"snake"); 
setcolor(6); 
setfillstyle(1,6); 
for(i=0;i<20;i++) 
{ 
delay(10000); 
circle(maxx/5+i*r*2,maxy/5-30,r); 
floodfill(maxx/5+i*r*2,maxy/5-30,6); 
circle(maxx/5+i*r*2,maxy/2.5,r); 
floodfill(maxx/5+i*r*2,maxy/2.5,6); 
} 
 
outtextxy(maxx/5,maxy/5-10,"snake"); 
setcolor(8); 
setfillstyle(1,8); 
for(i=0;i<20;i++) 
{ 
delay(10000); 
circle(maxx/5+i*r*2,maxy/5-30,r); 
floodfill(maxx/5+i*r*2,maxy/5-30,8); 
circle(maxx/5+i*r*2,maxy/2.5,r); 
floodfill(maxx/5+i*r*2,maxy/2.5,8); 
} 
outtextxy(maxx/5,maxy/5-5,"snake"); 
delay(100000); 
outtextxy(maxx/5,maxy/5-5,"snake"); 
} /*end while*/ 
setviewport(200,223,maxx-30,maxy/2+80,1); 
clearviewport(); 
setcolor(14); 
settextstyle(3,0,3); 
outtextxy(50,1," Now start! "); 
outtextxy(50,26," Option"); 
outtextxy(50,51," Quit "); 
} 
 
 
void end() 
{ 
int i,j ,t; 
for (i=0;i<1;i++) 
{ 
t=4; 
for(j=10;j<100;j=j+11,t=t+3) 
{ 
setcolor(t); 
circle(150,150,j); 
setfillstyle(1,t); 
floodfill(150,150,t); 
delay(5000); 
} 
for(j=10;j<100;j=j+4,t++) 
{ 
setcolor(t); 
circle(350,250,j); 
setfillstyle(1,t); 
floodfill(350,250,t); 
delay(5000); 
cleardevice(); 
} 
setcolor(14); 
settextstyle(3,0,6); 
outtextxy(100,200,"Thank you!"); 
getch(); 
cleardevice(); 
outtextxy(100,200,"For my love!"); 
getch(); 
closegraph(); 
} 
} 
 
 
void option() 
{ 
char c; 
char temp[30]; 
setviewport(20,200,600,350,0); 
setcolor(14); 
settextstyle(4,0,5); 
clearviewport(); 
sprintf(temp," Game level %d",opt); 
outtextxy(50,50,temp); 
while(1) 
{ 
c=getch(); 
clearviewport(); 
if(c==13) break; 
sprintf(temp," Game level %d",opt); 
settextstyle(4,0,5); 
outtextxy(50,50,temp); 
if(opt==20) opt=1; 
else opt++; 
} 
} 
 
void startgame() 
{ 
int key; 
setbkcolor(0); 
init(); 
while(game==1) 
{ 
if (kbhit()) 
{ 
key = bioskey(0); 
switch(key) 
{ 
case UP: if(control !=2) 
  control = 1; 
  break; 
case DOWN: if(control != 1) 
  control = 2; 
  break; 
case LEFT: if(control != 4) 
  control = 3; 
  break; 
case RIGHT: if(control != 3) 
  control = 4; 
  break; 
case KEY_W: if(control !=2) 
  control = 1; 
  break; 
case KEY_S: if(control != 1) 
  control = 2; 
  break; 
case KEY_A: if(control != 4) 
  control = 3; 
  break; 
case KEY_D: if(control != 3) 
  control = 4; 
  break; 
case ESC: game=0;break; 
case KEY_F12: getch(); break; 
case PAGEUP: opt++; 
  if (opt>20) opt = 20; 
  flag=TRUE; 
  break; 
case PAGEDOWN: opt--;if (opt<0) opt = 0; flag=TRUE;break; 
} /*end case*/ 
}/*end if*/ 
 
next(); 
draw(); 
if(flag) draw2(); 
}/*end while*/ 
if (game==0) end(); 
else good(); 
 
} 
 
void init(void) 
{ 
int i; 
flag=TRUE; 
cleardevice(); 
setviewport(0,0,600,420,0); 
for(i=0;i= MAXY) || 
(x < 0) ||(x >= MAXX) ) 
{ 
game = 0; 
control = 0; 
return; 
} 
for (i = 0; i < MAX; i++) 
{ 
if ((place[i].st) &&(x == place[i].x) &&(y == place[i].y) ) 
{ 
game =0; 
control = 0; 
return; 
} 
} 
if ( (x == bx) && (y == by) ) 
{ 
eat = TRUE; 
score += (1+opt) * 10; 
flag=TRUE; 
music(); 
} 
head ++; 
if (head == MAX) head = 0; 
place[head].x = x; 
place[head].y = y; 
place[head].st= TRUE; 
if (eat == FALSE) 
{ 
place[tear].st = FALSE; 
tear ++; 
if (tear == MAX) tear = 0; 
} 
else 
{ 
eat = FALSE; 
exit = TRUE; 
while(exit) 
{ 
bx = rand()%MAXX; 
by = rand()%MAXY; 
exit = FALSE; 
for( i = 0; i< MAX; i++ ) 
if( (place[i].st)&&( place[i].x == bx) && (place[i].y == by)) 
exit=TRUE; 
} 
} 
if ((head+50)%MAX == tear) game = 2; 
} 
 
 
void draw(void) 
{ 
int i,j; 
int temp[50]; 
clear_key(); 
delay(62700-opt*5000); 
setviewport(30,15,MAXX*18+30,MAXY*12+15,0); 
setcolor(9); 
setfillstyle(1,9); 
for (i = 0; i < MAX; i++ ) 
{ 
if (place[i].st) 
fillellipse(place[i].x*18+8,place[i].y*12+5,8,5); 
} 
 
setcolor(4); 
setfillstyle(1,4); 
fillellipse(bx*18+8,by*12+5,10,7); 
 
setcolor(8); 
setfillstyle(1,8); 
fillellipse(place[head].x*18+8,place[head].y*12+5,9,6); 
 
setcolor(3); 
setfillstyle(1,3); 
fillellipse(place[tear].x*18+8,place[tear].y*12+5,11,7); 
} 
 
void draw2 (void) 
{ int temp[60]; 
{ 
setviewport(MAXX*18+45,50,620,MAXY*10-100,0); 
clearviewport(); 
setcolor(14); 
settextstyle(2,0,5); 
sprintf(temp,"Score:%d",score); 
outtextxy(0,10,temp); 
sprintf(temp,"Level:%d",opt); 
outtextxy(0,40,temp); 
} 
flag=FALSE; 
 
} 
 
void clear_key(void) 
{ 
int offset; 
offset=peek(0x40,0x1a); 
pokeb(0x40,0x1c,offset); 
} 
 
 
void good(void) 
{ 
cleardevice(); 
setbkcolor(9); 
getch(); 
getch(); 
setviewport(0,0,630,470,0); 
setcolor(14); 
settextstyle(4,0,9); 
outtextxy(70,200,"Well Done!!"); 
getch(); 
cleardevice(); 
outtextxy(60,200,"For my love!"); 
getch(); 
closegraph(); 
} 
 
 
void music(void) 
{ 
int i; 
int a[10]={500,1000,1500,2000,2500,3000,3500,4000,4500,5000}; 
for(i=0;i<10;i++) 
{sound(a[i]); 
delay(5000); 
} 
nosound(); 
}