我定义了五块板子,标写有ABCDE,现在的问题是,
我的球碰到板子不会上升,会穿过,每过一波,
BE的板子可以带着球一起走,然后下一波是C的板子可以带着球走,其他的板子都会穿过
下面是代码,请大佬看一下谢谢
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define High 640
#define Width 480
#define barNumber 5
int bar_x[barNumber],bar_y[barNumber]; //板子坐标
int bar_high,bar_width; //板子长宽
int radius;
int ball_x,ball_y; //球坐标
int ball_v; //球速度
int bar_v; //板子速度
char input;
TCHAR s1[] = _T("1");
void startup() //初始化
{
bar_high = High/20;
bar_width = Width/(barNumber-1);
radius = 20;
ball_x = Width/2;
ball_y = radius;
ball_v = 1;
for(int i=0;i<barNumber;i++)
{
bar_x[i] = rand()%Width;
bar_y[i] = rand()%High;
}
initgraph(Width,High);
BeginBatchDraw();
}
void show()
{
setcolor(RED);
setfillcolor(RED);
fillcircle(ball_x,ball_y,radius);
int bar_left,bar_top,bar_right,bar_bottom;
for(int i=0;i<barNumber;i++)
{
setcolor(BLUE);
setfillcolor(BLUE);
bar_left = bar_x[i] - bar_width/2;
bar_top = bar_y[i] - bar_high/2;
bar_right = bar_x[i] + bar_width/2;
bar_bottom = bar_y[i] + bar_high/2;
bar(bar_left,bar_top,bar_right,bar_bottom);
setcolor(WHITE);
outtextxy(bar_x[i],bar_y[i],i+65);
}
FlushBatchDraw();
Sleep(50);
}
void clean()
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
int bar_left,bar_top,bar_right,bar_bottom;
for(int i=0;i<barNumber;i++)
{
bar_left = bar_x[i] - bar_width/2;
bar_top = bar_y[i] - bar_high/2;
bar_right = bar_x[i] + bar_width/2;
bar_bottom = bar_y[i] + bar_high/2;
bar(bar_left,bar_top,bar_right,bar_bottom);
}
}
void withinput() //输入相关
{
if(kbhit())
{
input = getch();
if(input=='a')
ball_x = ball_x-15;
if(input=='d')
ball_x = ball_x+15;
}
}
void withoutinput() //输入无关
{
static int speed = 0;
static int k = 0;
speed++;
for(int i=0;i<barNumber;i++)
{
if( ball_y == bar_y[i]-bar_high/2-radius )
{
if(ball_x>=bar_x[i]-bar_width/2 && ball_x<=bar_x[i]+bar_width/2)
{
ball_v = -5;
break;
}
else
ball_v = 1;
}
}
ball_y = ball_y + ball_v;
for(int i=0;i<barNumber;i++)
{
if(bar_y[i]>(-bar_high/2)-5)
bar_y[i] = bar_y[i] - 5;
else
{
bar_x[i] = rand()%Width;
bar_y[i] = High - bar_high/2;
}
}
}
int main()
{
startup();
while(1)
{
clean();
withinput();
withoutinput();
show();
}
}