delaytime = ELEMENTARY;break; case 2 : delaytime = INTERMEDIATE;break; case 3 : delaytime = ADVANCED;break; default: delaytime = ELEMENTARY;break; } } void DrawBox(int x,int y) /* x, y为左上角坐标sizex,sizey为长和宽*/ { int sizx=sizex-1; int sizy=sizey-1; setcolor(15); line(x,y,x+sizx-1,y); line(x,y+1,x+sizx-2,y+1); line(x,y,x,y+sizy-1); line(x+1,y,x+1,y+sizy-2); setcolor(8); line(x+1,y+sizy,x+sizx,y+sizy); line(x+2,y+sizy-1,x+sizx,y+sizy-1); line(x+sizx-1,y+1,x+sizx-1,y+sizy); line(x+sizx,y+2,x+sizx,y+sizy); setcolor(7); putpixel(x,y+sizy,3); putpixel(x+1,y+sizy-1,3); putpixel(x+sizx,y,3); putpixel(x+sizx-1,y+1,3); setfillstyle(1, 7); bar(x+2,y+2,x+sizx-2,y+sizy-2); } /*绘制游戏界面*/ void DrawInterface() { int i,j; cleardevice(); setcolor(15); rectangle(99,49,413,451); for(i=0;i<6;i++) for(j=0;j<14;j++) { a[i][j].color=0; a[i][j].x=104+j*22; a[i][j].y=54+i*14;
DrawBox(104+j*22,54+i*14); } sizex=50,sizey=5; } /*鼠标光标显示*/ void DrawMouse(int x,int y) { DrawBox(x,y); } /*设置鼠标左右边界*/ void MouseSetX(int lx,int rx) { _CX=lx; _DX=rx; _AX=0x07; geninterrupt(0x33); } /*设置鼠标上下边界*/ void MouseSetMaxY(int uy,int dy) { _CX=uy; _DX=dy; _AX=0x08; geninterrupt(0x33); } /*设置鼠标当前位置*/ void MouseSetXY(int x,int y) { _CX=x; _DX=y; _AX=0x04; geninterrupt(0x33); } /*获取鼠标当前位置*/ void MouseGetXY() { _AX=0x03; geninterrupt(0x33); MouseX=_CX; MouseY=_DX; } void MouseStatus()/*鼠标按键情况*/ {
int x; int status; status=0;/*默认鼠标没又移动*/ x=MouseX; if(x==MouseX&&status==0) /*鼠标没动*/ { MouseGetXY(); if(MouseX!=x) if(MouseX+50<413) status=1; } if(status)/*移动情况才重新显示鼠标*/ { setfillstyle(1,0); bar(x,MouseY,x+sizex,MouseY+sizey); DrawMouse(MouseX,MouseY);/*新位置显示*/ } } void BallStrike() { int ballX; /*球的圆心*/ int ballY = MouseY-R; int i,j,t=0; randomize(); while(ballX=random(409)) if(ballX>=105 && ballX<=408) break; while(kbhit) { MouseStatus(); if(ballY<=(58-R)) /*碰上反弹*/ dy*=(-1); if(ballX>=(413-R)||ballX<=(108-R)) /*碰左右反弹*/ dx*=(-1); setcolor(WHITE); circle(ballX+=dx,ballY-=dy,R-1); delay(delaytime); setcolor(0); circle(ballX,ballY,R-1); for(i=0;i<6;i++) for(j=0;j<14;j++) /*判断是否传记撞击box*/ if(t<84&&a[i][j].color==0 && ballX>=a[i][j].x && ballX<=a[i][j].x+18 && ballY>=a[i][j].y && ballY<=a[i][j].y+10)
{ t++; dy*=(-1); a[i][j].color=1; setfillstyle(1,0); bar(a[i][j].x,a[i][j].y,a[i][j].x+18,a[i][j].y+10); } if(ballX==MouseX||ballX==MouseX-1||ballX==MouseX-2&&ballX==(MouseX+50+2)||ballX==(MouseX+50+1)||ballX==(MouseX+50)) /*碰板反弹*/ if(ballY>=(MouseY-R)) { dx*=(-1); dy*=(-1); /*原路返回*/ } if(ballX>MouseX && ballX<(MouseX+50)) /*碰板反弹*/ if(ballY>=(MouseY-R)) dy*=(-1); if(t==84) { sleep(1); cleardevice(); setcolor(RED); settextstyle(0,0,4); outtextxy(100,200,"You Win"); sleep(1); break; } if(ballY>MouseY) { sleep(1); cleardevice(); setcolor(RED); settextstyle(0,0,4); outtextxy(100,200,"Game Over"); sleep(1); break; } } } void main() { InitGraphics();