类与类之间的吧变量传递问题该怎么解决呢?为什么小球不能动?还有初始位置的随机设置为什么无法通过编译?希望大佬解释一下,拜托
#include
#include
#include
#include
using namespace std;
#define MAXSTEP 9
class Box
{
private:
int a;
int b;
public:
Box(int high,int width)
{
a=high;
b=width;
initgraph(high, width);
}
int getHigh(){return a;}
int getWidth(){return b;}
};
class ball
{
private:
int R,X,Y,speed_x,speed_y;
int a,b;
COLORREF color;
int date_from_Box;
public:
ball(int r,int x,int y,int vx,int vy)
{
R=r;
X=x;
Y=y;
speed_x=vx;
speed_y=vy;
color=HSLtoRGB(float(rand()%360),1.0,0.5);
//初始位置随机
step.x=((rand()%2*2-1)*(rand()%MAXSTEP+1);
step.y=((rand()%2*2-1)*(rand()%MAXSTEP+1);
}
void show()
{
BeginBatchDraw();
cleardevice();
// 画渐变的天空(通过亮度逐渐增加)
float H = 190; // 色相
float S = 1; // 饱和度
float L = 0.7f; // 亮度
for(int y = 0; y < b; y++)
{
L += 0.0005f;
setlinecolor( HSLtoRGB(H, S, L) );
line(0, y, a-1, y);
}
//填充颜色 随机
setfillcolor(color);
color=HSLtoRGB(float(rand()%360),1,0.5);
solidellipse(X-R,Y-R,X+R,Y+R);
FlushBatchDraw();
EndBatchDraw();
}
void move()
{
//有一定阻力
if(speed_x!=0)
X+=speed_x;
speed_x-=0.01;
if(speed_x!=0)
Y+=speed_y;
speed_y-=0.01;
show();
}
void check_change()
{
//碰撞变色
if(X-R<=0 || X+R>=a){
speed_x*=-1;
color=HSLtoRGB(float(rand()%360),1,0.5);}
if(Y-R<=0 || Y+R>=b){
speed_y*=-1;
color=HSLtoRGB(float(rand()%360),1,0.5);}
}
};
int main()
{
int a,b;
cin>>a>>b;
initgraph(a,b);
ball myball(10,20,20,3,3);
while(1)
{
myball.check_change();
myball.move();
}
getch();
return 0;
}
出错在初始位置随机处
G:\1.cpp(42) : error C2958: the left parenthesis '(' found at 'G:\1.cpp(41)' was not matched correctly