#include "graphics.h"
#include "conio.h"
#include "stdlib.h"
#include "bios.h"
#include "process.h"
#include "dos.h"
#include "stdio.h"
#include "alloc.h"
void choicelevel();
void choicewindow();
int key();
void choicebar();
void moveword();
void shotword();
void quitwindow();
/* define Keys scan code */
#define Key_DOWN 80
#define Key_UP 72
#define Key_A 30
#define Key_B 48
#define Key_C 46
#define Key_E 18
#define Key_ENTER 28
int level; /*定义等级变量*/
int score; /*定义得分变量*/
main()
{
int j,word;
int gd=DETECT,gm;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
cleardevice();
setbkcolor(CYAN);
randomize();
while(1)
{
choicelevel() ; /*选择等级*/
clearviewport();
score=0;
#if 0
for(j=0;j<5;j++) /*控制每次游戏字母落下个数*/
{
word=random(26)+97;
moveword(word);
}
#endif
quitwindow();
}
}
void choicelevel() /* 选择打字的速度等级 */
{
int x,y,ky,size;
char ch,test[15]; /*ch 为按键的变量*/
void * choicebuf;
setviewport(198,168,442,312,0);
size=imagesize(0,0,243,143);
choicebuf=malloc(size);
do
{
choicewindow(choicebuf);
x=132; /* 红框的x 轴,y轴开端 */
y=48;
choicebar(x,y);
do
{
ky=key();
switch(ky)
{
case Key_A:
{
y=16;
ky=Key_ENTER;
break;
}
case Key_B:
{
y=48;
ky=Key_ENTER;
break;
}
case Key_C:
{
y=80;
ky=Key_ENTER;
break;
}
case Key_E:
{
y=112;
ky=Key_ENTER;
break;
}
case Key_DOWN : /* 选择框下移 */
{
if(y<=96)
{
clearviewport();
putimage(0,0,choicebuf,COPY_PUT);
y+=32;
choicebar(x,y);
}
break;
}
case Key_UP: /* 选择框上移 */
{
if(y>=48)
{
clearviewport();
putimage(0,0,choicebuf,COPY_PUT);
y-=32;
choicebar(x,y);
}
break;
}
default: break;
}
} while (ky!=Key_ENTER);
switch(y)
{
case 16:
{
level=1000;
ch='E';
break;
}
case 48:
{
level=2000;
ch='E';
break;
}
case 80:
{
level=3000;
ch='E';
break;
}
case 112:
{
ch='E';
exit(0); /* 退出游戏 */
}
default: break;
}
if(ch=='E') break;
}while(1);
free(choicebuf);
}
void choicewindow(void *buf) /*定义选择画面的函数*/
{
int size;
char test[15];
size=imagesize(0,0,243,143);
buf=malloc(size);
setviewport(198,168,442,312,0); /*x=244,y=144*/
setcolor(WHITE);
setfillstyle(SOLID_FILL,LIGHTGREEN);
rectangle(0,0,243,143);
floodfill(20,17,WHITE);
setcolor(RED);
settextstyle(0,0,2);
outtextxy(36,16,"Level: A");
outtextxy(36,48," B");
outtextxy(36,80," C");
setcolor(12);
settextstyle(0,0,2);
outtextxy(132,112,"Exit");
getimage(0,0,243,143,buf); /*用buf传递无选择框的choiceviewport*/
return;
}
void choicebar(int x,int y) /* 产生以x,y为开端,64*16的红框 */
{
textcolor(WHITE);
rectangle(x,y,x+63,y+15);
return;
}
int key() /* 得到按键的扫描码 */
{
union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg);
return(rg.h.ah);
}
#if 0
void moveword(char word) /*使产生的字母落下*//*利用视觉暂停产生动画*/
{
unsigned size;
int flag=1;
int x,y=1; /*word 的y轴初始位置*/
char *wordbuf; char s; /*s是测试*/
setviewport(0,0,639,479,0);
settextstyle(0,0,2);
setbkcolor(CYAN);
setcolor(RED);
x=random(200)+200;
while(y<471)
{
size=imagesize(x,y,x+15,y+15);
wordbuf=malloc(size);
getimage(x,y,x+15,y+15,wordbuf);
outtextxy(x,y,&word);
delay(level); /* 字母落下的延迟时间,用于选择等级*/
if(bioskey(1))
if((s=getch())==word)
{
score++; /*正确得加一分*/
shotword(x,y);
flag=0;
}
putimage(x,y,wordbuf,COPY_PUT);
if(flag==0) break;
y+=1;
}
clearviewport();
}
void shotword(int x,int y) /*打掉字母*/
{
int arrowx=600,arrowy=470;
unsigned size;
char *buffer;
float k;
k=(float)(arrowy-y)/(arrowx-x);
setcolor(LIGHTMAGENTA);
while(y!=1)
{
if(arrowx==x) break;
size=imagesize(arrowx,arrowy,arrowx-7,arrowy-7);
buffer=malloc(size);
getimage(arrowx,arrowy,arrowx-7,arrowy-7,buffer);
outtextxy(arrowx-7,arrowy-7,"o");
delay(400);
putimage(arrowx-7,arrowy-7,buffer,COPY_PUT);
arrowx--;arrowy=k*(arrowx-600)+470;
}
}
#endif
void quitwindow() /*定义退出界面函数*/
{
char str[15]; /*声明用于存放字符串的数组*/
setcolor(WHITE);
setviewport(198,168,442,312,0); /*x=244,y=144*/
setfillstyle(SOLID_FILL,LIGHTGREEN);
rectangle(0,0,243,143);
floodfill(20,17,WHITE);
setcolor(RED);
settextstyle(0,0,3);
outtextxy(14,25,"GAME OVER");
settextstyle(0,0,2);
sprintf(str,"Score: %d",score); /*格式化输出记分的值*/
outtextxy(58,74,str);
setcolor(12);
settextstyle(0,0,1);
outtextxy(14,115,"press any key to continue...");
getch();
clearviewport();
}