c语言实现类似弹力球效果.zip
立即下载
资源介绍:
c语言实现类似弹力球效果.zip
/* 【自学去】网站收集 http://www.zixue7.com */
#include
#include
#include
#include
#include
#include
#include
#define R 4
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
struct wall/*墙*/
{ int x;
int y;
int color;
}a[20][20];
int Keystate;
int MouseX;
int MouseY=400;
int dx=1,dy=1; /*计算球的反弹*/
int sizex=20,sizey=10; /*墙的宽度和长度*/
int Ide,Key;
void draw(int x,int y) /*画组成墙的砖*/
{
int sizx=sizex-1;
int sizy=sizey-1;
setcolor(15);
line(x,y,x+sizx,y);
line(x,y+1,x+sizx,y+1);
line(x,y,x,y+sizy);
line(x+1,y,x+1,y+sizy);
setcolor(4);
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);
setfillstyle(1,12);
bar(x+2,y+2,x+sizx-2,y+sizy-2);
}
void picture(int r,int l) /*画墙*/
{ int i,j;
setcolor(15);
rectangle(100,50,482,461);
for(i=0;i=476);/*随机产生小球的位置*/
while(kbhit)
{
MouseStatus();
if(ballY<=(59-R)) /*碰上反弹*/
dy*=(-1);
if(ballX>=(482-R)||ballX<=(110-R)) /*碰左右反弹*/
dx*=(-1);
setcolor(YELLOW);
circle(ballX+=dx,ballY-=dy,R-1);
delay(2500);
setcolor(0);/*将球移动后留下的印记用背景色覆盖*/
circle(ballX,ballY,R-1);
for(i=0;i=a[i][j].x && ballX<=a[i][j].x+20
&& 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+20,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==l*r)/*判断是否将墙壁完全清除*/
{ sleep(1);
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(100,200,"Win");
sleep(5);
break;
}
if(ballY>MouseY)
{ sleep(1);
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(130,200,"Game Over");
sleep(5);
break;
}
}
dx=1,dy=1; /*dx、dy重新置1*/
sizex=20,sizey=10;
}
void Rule()/*游戏规则*/
{
int n;
char *s[5]={"move the mouse right or left to let the ball rebound","when the ball bounce the wall","the wall will disappear","when all the wall disappear","you will win!"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<5;n++)
outtextxy(150,170+n*20,s[n]);
}
void DrawMenu(int j)/*菜单中的选项*/
{
int n;
char *s[4]={"1.Mession One","2.Mession two","3.rule","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<4;n++)
outtextxy(250,170+n*20,s[n]);
setcolor(RED);/*选中那个菜单,那个菜单变为红色*/
outtextxy(250,170+j*20,s[j]);
}
void MainMenu()/*主菜单*/
{
void JudgeIde();
setbkcolor(BLACK);
cleardevice();
Ide=0,Key=0;
DrawMenu(Ide);
do
{
if(bioskey(1)) /*有键按下则处理按键*/
{
Key=bioskey(0);
switch(Key)
{
case Key_Down:
{
Ide++;Ide=Ide%4;
DrawMenu(Ide);
break;
}
case Key_Up:
{
Ide--;
Ide=(Ide+4)%4;
DrawMenu(Ide);
break;
}
}
}
}while(Key!=Key_Enter);
JudgeIde(); /*调用judgeIde*/
}
void JudgeIde()
{
switch(Ide)
{
case 0:
cleardevice();
picture(6,15);
MouseSetX(101,431); /*设置鼠标移动的范围*/
MouseSetY(MouseY,MouseY); /*鼠标只能左右移动*/
MouseSetXY(150,MouseY); /*鼠标的初始位置*/
Play(6,15);
MainMenu();
break;
case 1:{
cleardevice();
picture(9,15);
MouseSetX(101,431);
MouseSetY(MouseY,MouseY);
MouseSetXY(150,MouseY);
Play(9,15);
MainMenu();
break;}
case 2:{
cleardevice();
Rule();
sleep(8);
MainMenu();
break;}
case 3:cleardevice();
settextstyle(0,0,4);
outtextxy(150,200,"goodbye!");
sleep(5);
exit(0);
}
}
void main()
{int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
MainMenu();
closegraph();
}