您好,欢迎访问三七文档
1.课程设计的具体工作内容:1)弹球区域下方为横板接球,上方为砖块。2)通过键盘的左、右光标键控制接球横版的移动。3)小球落到横板水平线,横板未接注小球,本局结束。4)小球弹起碰到的砖块自动打碎、消失。5)每隔一定时间(例如5s,可适当调整),砖块上方增加一行,原有砖块下移,砖块增加到横板水平线本局结束结束。6)小球与横板为完全弹性碰撞,不考虑能量损耗。7)每次游戏开始后有三局(或更多,可适当调整),三局都结束游戏重新开始。2.打砖块小游戏C语言源代码:#includegraphics.h#includestdlib.h#includedos.h#includeconio.h#defineR4/*球半径*/structbox{intx;inty;intcolor;}a[6][14];intKeystate;intMouseExist;intMouseButton;intMouseX;intMouseY=400;intdx=1,dy=1;/*计算球的反弹*/intsizex=18,sizey=10;/*box的宽度和长度*/voiddraw(intx,inty)/*x,y为左上角坐标sizex,sizey为长和宽*/{intsizx=sizex-1;intsizy=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);}voidpicture()/*画box*/{inti,j;setcolor(15);rectangle(99,49,413,451);for(i=0;i6;i++)for(j=0;j14;j++){a[i][j].color=0;a[i][j].x=104+j*22;a[i][j].y=54+i*14;draw(104+j*22,54+i*14);}sizex=50,sizey=5;}/*鼠标光标显示*/voidMouseOn(intx,inty){draw(x,y);}/*隐藏鼠标*/voidMouseOff(){intx,y;x=MouseX;y=MouseY;setfillstyle(1,0);bar(x,y,x+sizex,y+sizey);}/*鼠标是否加载MouseExist:1=加载0=未加载MouseButton:鼠标按键数目*/voidMouseLoad(){_AX=0x00;geninterrupt(0x33);MouseExist=_AX;MouseButton=_BX;}/*鼠标状态值初始化*/voidMouseReset(){_AX=0x00;geninterrupt(0x33);}/*设置鼠标左右边界lx:左边界gx:右边界*/voidMouseSetX(intlx,intrx){_CX=lx;_DX=rx;_AX=0x07;geninterrupt(0x33);}/*设置鼠标上下边界uy:上边界dy:下边界*/voidMouseSetY(intuy,intdy){_CX=uy;_DX=dy;_AX=0x08;geninterrupt(0x33);}/*设置鼠标当前位置x:横向坐标y:纵向坐标*/voidMouseSetXY(intx,inty){_CX=x;_DX=y;_AX=0x04;geninterrupt(0x33);}/*设置鼠标速度(缺省值:vx=8,vy=1)值越大速度越慢*/voidMouseSpeed(intvx,intvy){_CX=vx;_DX=vy;_AX=0x0f;geninterrupt(0x33);}/*获取鼠标当前位置*/voidMouseGetXY(){_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;}voidMouseStatus()/*鼠标按键情况*/{intx;intstatus;status=0;/*默认鼠标没又移动*/x=MouseX;if(x==MouseX&&status==0)/*鼠标没动*/{MouseGetXY();if(MouseX!=x)if(MouseX+50413)status=1;}if(status)/*移动情况才重新显示鼠标*/{setfillstyle(1,0);bar(x,MouseY,x+sizex,MouseY+sizey);MouseOn(MouseX,MouseY);/*新位置显示*/}}voidMove(){intballX;/*球的圆心*/intballY=MouseY-R;inti,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(YELLOW);circle(ballX+=dx,ballY-=dy,R-1);delay(2500);setcolor(0);circle(ballX,ballY,R-1);for(i=0;i6;i++)for(j=0;j14;j++)/*判断是否传记撞击box*/if(t84&&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(ballXMouseX&&ballX(MouseX+50))/*碰板反弹*/if(ballY=(MouseY-R))dy*=(-1);if(t==84){sleep(1);cleardevice();setcolor(RED);settextstyle(0,0,4);outtextxy(100,200,Win);sleep(1);break;}if(ballYMouseY){sleep(1);cleardevice();setcolor(RED);settextstyle(0,0,4);outtextxy(100,200,GameOver);sleep(1);break;}}}voidmain(){intgd=DETECT,gm;initgraph(&gd,&gm,c:\\tc);picture();MouseSetX(100,362);/*设置鼠标移动的范围*/MouseSetY(MouseY,MouseY);/*鼠标只能左右移动*/MouseSetXY(150,MouseY);/*鼠标的初始位置*/MouseOn(MouseX,MouseY);/*第一次显示鼠标*/Move();closegraph();}
本文标题:C打砖块源代码
链接地址:https://www.777doc.com/doc-5252455 .html