#include<time.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#defineR10
#defineESC0x011b
#defineLOW0x5000
#defineFAST0x4800
intdraw_car(intx,inty,char*ch);
doubledalta=20,angle;
void*image;
intstep=20;
main()
{
intgdriver=DETECT,gmode;
staticintstartx=60;
staticintstarty=20;
intmaxy,maxx,l=1,n=1;
charch,play,str[2];
intsize,temch;
intok,y=50;
clrscr();
printf("\n\n\n\n\n*****************************************\n");
printf("1.playonlynumber0-9.\n");
printf("2.playonlychara-z.\n");
printf("3.exit\n");
printf("*****************************************\n");
while(1){
printf("\nPlsenterchoice:");
play=getche();
if((play=='1')||(play=='2'))
break;
if(play=='3')
return;
}
registerbgidriver(EGAVGA_driver);/*注冊vga驅動*/
initgraph(&gdriver,&gmode,"d:\\tc3\\bgi");/*初始化圖形界面*/
cleardevice();/*清除屏幕*/
setbkcolor(BLUE);/*將背景色設置成藍色*/
size=imagesize(startx,starty,startx+60,starty+60);/*計算生成60×60個象素的圖需要的字節(jié)數*/
image=(unsignedchar*)malloc(size);/*分配內存*/
maxx=getmaxx();
maxy=getmaxy();
randomize();
printf("PressESCtoexitgame!");
printf("%ctofastspeed,%ctolowspeed!",24,25);
do{
ok=0;
if(play=='1')
ch=random(9)+48;
if(play=='2')
ch=random(26)+65;
str[0]=ch;
do{
while(bioskey(1)==0)/*如果沒有按鍵就不停的循環(huán)*/
{
if(l==1)/*從左到右*/
{
n++;
angle=-1*(n*step)/M_PI*180/R;
if((int)(-1*angle)%360<dalta)
angle-=dalta;
if(n>(maxx-180)/step){
if(starty>maxy-100)
starty=20;
else
starty+=y;
l=0;
}
}
if(l==0)/*從右到左*/
{
--n;
angle=-1*(n*step)/R/M_PI*180;
if((int)(-1*angle)%360<dalta)
angle-=dalta;
if(n==1){
if(starty>maxy-100)
starty=20;
else
starty+=y;
l=1;
}
}
draw_car(startx+n*step,starty,str);
}
temch=bioskey(0);
if(temch==FAST){
step+=10;
y+=10;
}
if(temch==LOW){
step-=10;
y-=10;
}
ok=temch&0x00ff;
if((ok==ch)||(ok==ch+32)){
ok=1;
starty=20;
break;
}
}while(temch!=ESC);
if(ok!=1)
break;
}while(1);
free(image);
closegraph();
return;
}
intdraw_car(intx,inty,char*ch){
setcolor(YELLOW);/*設置前景色為黃色*/
rectangle(x,y,x+60,y+40);/*畫車廂*/
pieslice(x+15,y+50,angle,angle-dalta,10);/*畫輪上的小片扇形部分*/
pieslice(x+45,y+50,angle,angle-dalta,10);
setfillstyle(SOLID_FILL,YELLOW);/*設置填充模式為實填充,顏色為黃色*/
circle(x+15,y+50,10);/*畫車輪*/
circle(x+45,y+50,10);
circle(x+15,y+50,3);
circle(x+45,y+50,3);
setcolor(WHITE);
settextstyle(1,0,4);
outtextxy(x+25,y,ch);
getimage(x,y,x+60,y+60,image);/*獲取當前的圖片*/
delay(200);
putimage(x,y,image,XOR_PUT);/*使用異或模式將圖片顯示上去*/
return;
}