#include"stdio.h" #include"graphics.h" #include"stdlib.h" #include"bios.h" #define LEFT 0x4b00 #define RIGHT 0x4d00 #define ESC 0x011b /*存放方块的信息*/ struct box { int x; /*方块右上角横坐标*/ int y; /*方块右上角纵坐标*/ int key; /*方块是否被击中过,1表示被击中过,0表示没有被击中过*/ }a[10][20];
int k; /*用来判断游戏的结束方式,0:按ESC退出,1:正常退出*/
/*初始化图形*/ void initgr() { int gd=DETECT,gm; initgraph(&gd,&gm,""); } /*画出初始图*/ void picture() { int x,y,i,color; setcolor(WHITE); outtextxy(250,400,"ANY KEY ..."); while(!kbhit()) { settextstyle(0,0,4); for(i=1;i<=15;i++) { setcolor(i); outtextxy(100,180,"PING PANG BALL"); delay(5000); } } cleardevice(); setbkcolor(BLUE); setfillstyle(SOLID_FILL,RED); bar(300,400,348,404); /*画屏幕上端的方块*/ randomize(); for(y=0;y<=150;y=y+15) for(x=0;x<=640;x=x+32) { color=((rand()%5)+2); setfillstyle(1,color); bar(x,y,x+32,y+15); } sleep(1); } /*关闭图形*/ void closegr() { cleardevice(); closegraph(); }
/*游戏绍束*/ void end() { cleardevice(); setbkcolor(0); switch(k) { case 1: setcolor(RED); settextstyle(0,0,3); outtextxy(200,150,"YOU LOST !!"); sleep(2); cleardevice(); settextstyle(0,0,4); outtextxy(180,220,"GAME OVER"); sleep(2); break; case 0: setcolor(RED); settextstyle(0,0,4); outtextxy(180,220,"GAME OVER"); sleep(2); break; } }
/*爆裂效果*/ void bomb(int x,int y) { int r1,i; int pointX,pointY,point_color; setcolor(YELLOW); for(r1=1;r1<=4;r1++) { circle(x+16,y+7,r1); delay(1000); } delay(3000); setcolor(RED); for(r1=1;r1>=6;r1++) { circle(x+16,y+7,r1); delay(1000); } delay(3000); setcolor(BLUE); for(r1=6;r1>=1;r1--) { circle(x+16,y+7,r1); delay(1000); } for(i=1;i<=50;i++) { point_color=rand()%15; pointX=(rand()%32+x); pointY=(rand()%15+y); putpixel(pointX,pointY,point_color); delay(1000); } setfillstyle(1,BLUE); bar(x,y,x+32,y+15); }
/*开始游戏*/ void move() { int key; int i,j,boxX,boxY; int ballX,ballY,dX=1,dY=1; int barX=300,barY=400,x,y; randomize(); ballX=rand()%620+10; ballY=rand()%200+190; for(;;) /*受侦察的指点....*/ { /*小球的运动*/ while(!kbhit()) { if((ballX<=10)||(ballX>=630)) dX=dX*(-1); if((ballY<=10)||(ballY>=470)) dY=dY*(-1); setcolor(YELLOW); circle(ballX+=dX,ballY-=dY,5); setfillstyle(SOLID_FILL,YELLOW); floodfill(ballX,ballY,YELLOW); delay(2500); setcolor(BLUE); circle(ballX,ballY,5); setfillstyle(SOLID_FILL,BLUE); floodfill(ballX,ballY,BLUE); /*判断是否击中方块*/ for(boxY=0,i=0;boxY<=150;boxY=boxY+15,i++) for(boxX=0,j=0;boxX<=640;boxX=boxX+32,j++) { if((ballX<boxX+32)&&(ballX>boxX)&&((ballY<=(boxY+20))&&(ballY>=boxY-5))&&(a[i][j].key==0)) { dY=dY*(-1); setfillstyle(1,BLUE); bar(boxX,boxY,boxX+32,boxY+15); bomb(boxX,boxY); a[i][j].key=1; break; }
} /*判断反弹或出界*/ if((ballX>=barX)&&(ballX<=barX+60)&&(ballY>=390)) dY=dY*(-1); if(((ballX<barX)||(ballX>barX+60))&&(ballY>410)) { sleep(1); k=1; return; } } /*球拍的运动*/ key=bioskey(0); x=barX; y=barY; if(key==LEFT) { barX-=10; setfillstyle(SOLID_FILL,BLUE); bar(x,y,x+60,y+4); setfillstyle(SOLID_FILL,RED); bar(barX,barY,barX+60,barY+4); } else if(key==RIGHT) { barX+=10; setfillstyle(SOLID_FILL,BLUE); bar(x,y,x+60,y+4); setfillstyle(SOLID_FILL,RED); bar(barX,barY,barX+60,barY+4); } else if(key==ESC) { k=0; break; }
} } void main() { int i,j,x,y; for(i=0,y=0;i<10;i++) { x=0; for(j=0;j<20;j++) { a[i][j].x=x; a[i][j].y=y; a[i][j].key=0; x=x+32; } y=y+15; } initgr(); picture(); move(); end(); closegr(); } |