嘿伙计们我正在建造这个简单的台球游戏,我希望标记为bBall的黑球与白球相同,标记为wBall,并且没有更远。也就是说,如果白球在击中黑球之前行进20个像素,我希望黑球行进20个像素然后停止。我怎么能完成这个?谢谢你的帮助。
处理2.0.3
ball wBall, bBall;
int click;
String msg;
Boolean moving = false;
float difx, dify;
float cdistance;
int steps = 40;
void setup(){
click=0;
size(800,400);
background(16,77,27);
wBall = new ball(35,#ffffff);
bBall = new ball(35,#000000);
msg="";
}
void mouseClicked(){
if(!moving){
click++;
}
}
void draw(){
background(16,77,27);
String msg;
fill(0,0,0);
ellipse(15,15,30,30);
ellipse(785,15,30,30);
ellipse(15,385,30,30);
ellipse(785,385,30,30);
ellipse(410,15,30,30);
ellipse(410,385,30,30);
msg="the count is "+click;
println("the count is "+click);
//Moving Balls\\
fill(255,255,255);
noStroke();
if(click==0){
wBall.xpos=mouseX;
wBall.ypos=mouseY;
}else if(click==1){
bBall.xpos=mouseX;
bBall.ypos=mouseY;
}else if(click==2){
difx = wBall.xpos-bBall.xpos;
dify = wBall.ypos-bBall.ypos;
}
else if(click==3){
cdistance = dist(wBall.xpos,wBall.ypos,bBall.xpos,bBall.ypos);
if (cdistance>bBall.ballDiam/2){
moving = true;
wBall.xpos-=difx/steps;
wBall.ypos-=dify/steps;
}
else{
moving = false;
click=4;
println("click"+click);
}
}else if(click==4){
if(cdistance<bBall.ballDiam){
moving = true;
bBall.xpos-=difx/steps;
bBall.ypos-=dify/steps;
}
}
wBall.update();
bBall.update();
}
class ball{
float xpos, ypos;
color myColor;
int ballDiam;
boolean visible = true;
ball(int tempdiam, color tempColor){
myColor=tempColor;
ballDiam=tempdiam;
}
void update(){
if(visible){
fill(myColor);
ellipse(xpos,ypos,ballDiam,ballDiam);
}
}
}
void keyPressed(){
if (key =='c'){
setup();
}
}
答案 0 :(得分:0)
一种方法是制作你的
else if (click==4) {
if (cdistance<bBall.ballDiam) {
moving = true;
bBall.xpos-=difx/steps;
bBall.ypos-=dify/steps;
}
}
到
else if (click==4) {
if (cdistance<bBall.ballDiam) {
if (dist(wBall.xpos, wBall.ypos, bBall.xpos, bBall.ypos) < sqrt(sq(difx)+sq(dify))) {
moving = true;
bBall.xpos-=difx/steps;
bBall.ypos-=dify/steps;
}
}
}
基本上只有移动球,只要它与白球的距离小于原始差异......
尽管如此,我觉得你正在艰难地接近这一点。也许看看为你的游戏引入速度和加速度(甚至摩擦力)计算,事情会更有意义......