#include<graphics.h>
#include<conio.h>
#include<STDLIB.h>
#include<dos.h>
#include<time.h>
#define BK_COLOR BLACK
#define CHAR_COLOR WHITE
#define C_COLOR BLUE
#define num 10
#define Esc 27
#define MenuX 200
#define MenuY 110
#define MenuWidth 200
#define MenuSinH 40
#define ChoiceX MenuX+15
#define ChoiceY MenuY+10
#define ChoiceWidth 170
#define ChoiceH 25
#define UpKey 72
#define DownKey 80
#define Enter 13
#define ViewLeft 20
#define ViewRigeht 520
#define ViewTop 15
#define ViewBotton 464
#define DateViewX ViewRigeht
#define DateViewY ViewTop+40
#define DateH 40
/*静态变量*/
int nCount=30;
int speed=6000;
struct PlayDate
{
int time;
int Hit;
int Lose;
double ratio;
}result;
void Choice(int c_x,int c_y,int color)
{
setfillstyle(1,color);
bar(c_x,c_y,c_x+ChoiceWidth,c_y+ChoiceH);
}
void Main_Menu()
{
settextstyle(0,0,2);
outtextxy(MenuX+20,MenuY+15,"Start Game");
outtextxy(MenuX+20,MenuY+MenuSinH+15,"Game Steup");
outtextxy(MenuX+40,MenuY+2*MenuSinH+15,"Exit");
}
void Option_Menu()
{
settextstyle(0,0,2);
outtextxy(MenuX+20,MenuY+15,"beginner");
outtextxy(MenuX+20,MenuY+MenuSinH+15,"advancer");
outtextxy(MenuX+40,MenuY+2*MenuSinH+15,"senior");
}
int ChooseMenu(int flag)
{
int x,y;
char C_key;
x=ChoiceX;y=ChoiceY;
do
{
kbhit();
C_key=getch();
if(C_key==Esc)
return Esc;
else
if(C_key==Enter)
return y;
else
switch(C_key)
{
case UpKey:
if(y==ChoiceY)
break;
else
{
Choice(x,y,BK_COLOR);
y-=MenuSinH;
Choice(x,y,C_COLOR);
}
break;
case DownKey:
if (y==ChoiceY+2*MenuSinH)
break;
else
{
Choice(x,y,BLACK);
y+=MenuSinH;
Choice(x,y,BLUE);
}
break;
default:
break;
}
if (flag ==0)
Main_Menu();
else
if(flag==1)
Option_Menu();
}while(C_key!=Esc);
}
void SetResult()
{
int result;
result=ChooseMenu(1);
switch(result)
{
case Esc:
return 0;
break;
case ChoiceY:
speed=6000;
nCount=30;
break;
case ChoiceY+MenuSinH:
speed=3000;
nCount=60;
break;
case ChoiceY+2*MenuSinH:
speed=1000;
nCount=100;
break;
default:
break;
}
}
void Interface()
{
int i,j;
setcolor(CYAN);
for(i=0;i<60;i++)
{
line(i,0,i,500);
line((getmaxx()-i),0,(getmaxx())-i,500);
}
setcolor(YELLOW);
for(i=60,j=0;i<85;i++,j++)
{
line(i,j,i,500);
line((getmaxx()-i),j,(getmaxx()-i),500);
}
setcolor(WHITE);
rectangle(MenuX,MenuY,MenuX+MenuWidth,MenuY+3*MenuSinH);
line(MenuX,MenuY+MenuSinH,MenuX+MenuWidth,MenuY+MenuSinH);
line(MenuX,MenuY+2*MenuSinH,MenuX+MenuWidth,MenuY+2*MenuSinH);
}
void Loading()
{
int i,j,l;
Interface();
setfillstyle(1,0);
bar(200,110,400,230);
settextstyle(0,0,1);
outtextxy(250,207,"Loading");
for(l=0;l<6;l++)
{
setcolor(15);
rectangle(237+l*15,225,244+l*15,235);
}
l=0;
j=1;
while(j!=480-j)
{
j++;
if(j%40==0)
{
setfillstyle(1,RED);
bar(237+l*15,225,244+l*15,235);
l++;
}
setfillstyle(1,0);
bar(0,0,getmaxx(),j);
bar(0,480,getmaxx(),480-j);
delay(1000);
}
delay(60000);
clearviewport();
}
void PlayViewPort()
{
int i;
cleardevice();
setcolor(CYAN);
for(i=0;i<3;i++)
rectangle(ViewLeft-10-i,ViewTop-10-i,ViewRigeht+110+i,ViewBotton+10+i);
/*利用循环画矩形外框,目的是为了让外框显示有厚度*/
rectangle(ViewLeft,ViewTop,ViewRigeht+100,ViewBotton);
/*画内框,与外框相差10像素*/
setfillstyle(XHATCH_FILL,YELLOW);/*设置填充样式和颜色*/
floodfill(ViewLeft-1,ViewTop-1,CYAN);
setcolor(WHITE);
for(i=0;i<3;i++)
rectangle(ViewLeft-i,ViewTop-i,ViewRigeht+100+i,ViewBotton+i);
/*用和填充外框厚度的方法填充内框的厚度*/
line(ViewRigeht,ViewTop,ViewRigeht,ViewBotton);
/*用直线吧视图区和数据区分开*/
settextstyle(0,0,0);
/*对数据区各个区域的内容进行描述*/
rectangle(DateViewX,DateViewY,DateViewX+100,DateViewY+DateH );
rectangle(DateViewX,DateViewY+DateH ,DateViewX+100,DateViewY+2*DateH );
rectangle(DateViewX,DateViewX+2*DateH ,DateViewX+100,DateViewY+3*DateH );
outtextxy(DateViewX+10,DateViewY+10,"Hit");
outtextxy(DateViewX+10,DateViewY+DateH +10,"Lose");
outtextxy(DateViewX+10,DateViewY+DateH *2+10,"PreKey");
}
void DrawChar(int i,int j,char c)
{
char ch[1];
ch[0]=c;
ch[1]='/0';
moveto(i,j);
settextstyle(0,0,2);
outtext(ch);
}
void AutoDraw_Down(int x,int y,char c,int n)
{
setcolor(BK_COLOR);
DrawChar(x,y,c);
setcolor(CHAR_COLOR);
DrawChar(x,y+1,c);
delay(n);
}
void AutoDraw_Up(int x,int y,char c,int n)
{
setcolor(BK_COLOR);
DrawChar(x,y,c);
setcolor(CHAR_COLOR);
DrawChar(x,y+1,c);
delay(n);
}
char GenerateChar()
{
int flag;
char ch;
randomize();
flag=random(3);
switch(flag)
{
case 0:
ch='a'+ random(26);break;
case 1:
ch='A'+random(26);break;
case 2:
ch='0'+random(10);break;
default:break;
}
return ch;
}
int getX()
{
int xx=0;
randomize();
xx=random(ViewRigeht-ViewLeft-30);
return xx+ViewLeft+20;
}
void BombShow(int x,int y,char c)
{
int c1,c2,r1=15,r2=8;
char ch;
c1=YELLOW;
c2=BLACK;
setcolor(c1);
circle(x,y,r1);
setfillstyle(1,c1);
floodfill(x,y,c1);
setcolor(c2);
circle(x,y,r2);
setfillstyle(1,c2);
floodfill(x,y,c2);
settextstyle(0,0,2);
outtextxy(x-2,y-2,c);
delay(1000);
}
void ShowDate(int x,int y,int z)
{
char *str;
setfillstyle(1,BLACK);
bar(DateViewX+60,DateViewY+2,DateViewX+90,DateViewY-2+DateH);
bar(DateViewX+60,DateViewY+2+DateH,DateViewX+90,DateViewY-2+DateH*2);
bar(DateViewX+60,DateViewY+2+DateH*2,DateViewX+90,DateViewY-2+DateH*3);
settextstyle(0,0,0);
setcolor(CHAR_COLOR);
sprintf(str,"%d",x);
outtextxy(DateViewX+60,DateViewY+10,str);
sprintf(str,"%d",y);
outtextxy(DateViewX+60,DateViewY+DateH+10,str);
sprintf(str,"%d",z);
outtextxy(DateViewX+60,DateViewY+DateH*2+10,str);
}
void DrawLaugh(int x,int y,int r)/*x=ViewRigeht+50,y=ViewTop+20*/
{
int d,c1,c2,c3;
c1=YELLOW;
c2=BLACK;
c3=WHITE;
d=r/5;
setcolor(c1);
circle(x,y,r);
setfillstyle(1,c1);
floodfill(x,y,c1);
setcolor(c2);
line(x-d,y-d*3,x-2*d,y-4*d);
line(x-2*d,y-4*d,x-2.9*d,y-4*d);
line(x+d,y-d*3,x+2*d,y-4*d);
line(x+2*d,y-4*d,x+2.9*d,y-4*d);
line(x-3.5*d,y-2*d,x-2.5*d,y-2.6*d);
line(x-2.5*d,y-2.6*d,x-1.5*d,y-2*d);
line(x+3.5*d,y-2*d,x+2.5*d,y-2.6*d);
line(x+2.5*d,y-2.6*d,x+1.5*d,y-2*d);
pieslice(x,y,180,360,4*d);
setfillstyle(1,c3);
floodfill(x,y+d,c2);
line(x-2*d,y,x-2*d,y+3.5*d);
line(x+2*d,y,x+2*d,y+3.5*d);
line (x,y,x,y+4*d);
line(x+3.5*d,y,x+3.5*d,y+1.8*d);
}
void Laugh(int x,int y)/*x=ViewRigeht+50,y=ViewTop+20*/
{
int i,d,r,flag=0;
d=6;i=0;
r=10;
while(!kbhit())
{
DrawLaugh(x,y+i,r);
delay(10000);
setfillstyle(1,BK_COLOR);
setcolor(BK_COLOR);
pieslice(x,y+i,0,360,r);
if(i==d)
flag=1;
if(i==0)
flag=0;
if(flag==0)
i++;
else
i--;
}
}
void Smile(int x,int y)/*x=ViewRigeht+50,y=ViewTop+20*/
{
int r=10,c1=YELLOW,c2=BLACK;
int d=r/5,i;
setcolor(c1);
circle(x,y,r);
setfillstyle(1,c1);
floodfill(x,y,c1);
setcolor(c2);
setfillstyle(1,c2);
sector(x-2.5*d,y-2.5*d,0,360,1,1);
sector(x+2.5*d,y-2.5*d,0,360,1,1);
setcolor(c2);
arc(x,y,200,350,3.5*d);
}
void Weep(int x,int y)/*x=ViewRigeht+50,y=ViewTop+20*/
{
int r=10,c1=YELLOW,c2=BLACK;
int d=r/5,i=0;
while(!kbhit())
{
setcolor(c1);
circle(x,y,r);
setfillstyle(1,c1);
floodfill(x,y,c1);
setcolor(c2);
line(x-2.5*d-1,y-2.5*d,x-2.5*d-1,y-2.5*d+i);
line(x-2.5*d,y-2.5*d,x-2.5*d,y-2.5*d+i);
line(x-2.5*d+1,y-2.5*d,x-2.5*d+1,y-2.5*d+i);
line(x+2.5*d-1,y-2.5*d,x+2.5*d-1,y-2.5*d+i);
line(x+2.5*d,y-2.5*d,x+2.5*d,y-2.5*d+i);
line(x+2.5*d+1,y-2.5*d,x+2.5*d+1,y-2.5*d+i);
arc(x,y+5*d,30,150,4*d);
delay(10000);
i++;
if(i==(3*d))
{
i=0;
setfillstyle(1,c2);
pieslice(x,y,0,360,r);
}
}
}
void ShowFace(int flag)
{
int x,y;
x=DateViewX+50;
y=DateViewY-20;
switch(flag)
{
case 1:
Laugh(x,y);
break;
case 2:
Smile(x,y);
break;
case 3:
Weep(x,y);
break;
default :
break;
}
}
void ShowResult(int t,int Hit,int Lose,float ratio)
{
int x,y,temp,Char_Count;
int flag;
char *str;
Char_Count=Hit+Lose;
x=ViewLeft+150;
y=ViewRigeht+100;
setcolor(WHITE);
rectangle(x,y,x+200,y+120);
line(x,y+30,x+200,y+30);
line(x,y+60,x+200,y+60);
line(x,y+90,x+200,y+90);
settextstyle(0,0,0);
setcolor(YELLOW);
sprintf(str,"Total Time: %d s",t);
outtextxy(x+20,y+10,str);
sprintf(str,"Hit : %d ",Hit);
outtextxy(x+20,y+40,str);
sprintf(str,"Lose : %d ",Lose);
outtextxy(x+20,y+70,str);
temp=(int)(ratio*100.00);
sprintf(str,"Hit ratio: %d/%",temp);
outtextxy(x+20,y+100,str);
settextstyle(1,0,2);
setcolor(WHITE);
if(temp==100&& Char_Count==nCount)
{
flag=1;
outtextxy(x-50,y+150,"Pretty Good!You have a gift!");
}
else
if(temp>=89)
{
flag=1;
outtextxy(x+50,y+150,"Well Done!");
}
else
if (temp>=80)
{
flag=2;
outtextxy(x+70,y+150,"Good!");
}
else
if(temp>=60)
{
flag=2;
outtextxy(x+50,y+150,"Not Bad!");
}
else
{
flag=2;
outtextxy(x-50,y+150,"Sorry!You did a bad work!");
}
ShowFace(flag);
}
void Play()
{
char c,key,*str;
int e=20,PreKey_Count=0,Hit_Count=0,Char_Count=0;
int i,x,x1,y1,y,gd=DETECT,gm;
int Start_T,End_T;
float f;
setbkcolor(BK_COLOR);
PlayViewPort();
ShowDate(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
Start_T=time(NULL);
while(key!=Esc&&Char_Count<nCount)
{
x=getX();
c=GenerateChar();
Char_Count++;
for(y=16;y<450;y++)
{
AutoDraw_Down(x,y,c,speed);
if(kbhit())
{
key=getch();
if(key==Esc)
break;
else
{
if(key==c)
{
x1=x;
for(y1=ViewBotton-10;y1>=y;y1--)/*从屏幕低端飞出相同字符直到掉下的字符*/
AutoDraw_Up(x1,y1,key,speed/10);
Hit_Count++;
setcolor(RED);
for(i=10;i<=30;i++)
{
circle(x1,y1,i);
delay(1000);
}
/*BombShow(x,y,c);*/
}
else
{
x1=x+e;
for(y1=ViewBotton-10;y1>=y;y1--)
AutoDraw_Up(x1,y1,key,speed/10);
}
PreKey_Count++;
}
break;
}
}
ShowDate(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
setfillstyle(1,0);
bar(ViewLeft+1,ViewTop+1,ViewRigeht-1,ViewBotton-1);
}
End_T=time(NULL);
result.time=Start_T-End_T;
result.Hit=Hit_Count;
result.Lose=Char_Count-Hit_Count;
if (Char_Count==0)
result.ratio=0;
else
result.ratio=Hit_Count/(float)Char_Count;
ShowResult(Char_Count,result.Hit,result.Lose,result.ratio);
getch();
sound(3000);
delay(10000);
nosound();
}
main()
{
int gd=DETECT,gm;
int c_x,c_y,result;
char C_Key;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"c:/tc/TC20H");
c_x=ChoiceX;
c_y=ChoiceY;
Begin:
cleardevice();
Interface();
Choice(c_x,c_y,BLUE);
Main_Menu();
result=ChooseMenu(0);
switch(result)
{
case Esc:
exit(0);
break;
case ChoiceY:
cleardevice();
sleep(1);
Loading();
Play();
goto Begin;
break;
case ChoiceY+MenuSinH:
cleardevice();
Interface();
Choice(c_x,c_y,C_COLOR);
Option_Menu();
SetResult();
goto Begin;
break;
case ChoiceY+2*MenuSinH:
exit(0);
break;
default:
break;
}
}