1
現在我確定你們都聽說過這些愚蠢的檯球遊戲問題,這應該有希望成爲我的最後一個,我已經得到了碰撞,但把它保持在屏幕上是一點點的問題。我該如何去做呢?保持對象在屏幕上
class Ball {
int xpos, ypos;
int ballDiam;
color myColor;
Ball(int tempdiam, color tempColor) {
ballDiam=tempdiam;
myColor=tempColor;
}
void update() {
fill(myColor);
ellipse(xpos, ypos, ballDiam, ballDiam);
}
}
Ball b1, b2;
int click;
String msg;
int steps = 30;
int difx,dify;
Boolean moving = false;
void setup() {
msg="";
click=0;
size(600, 300);
b2= new Ball(50, #000000);
b1 = new Ball(50, #ffffff);
}
void draw() {
background(#009900);
b1.update();
b2.update();
if (click==0) {
b1.xpos=mouseX;
b1.ypos=mouseY;
msg="click to place ball";
}
if (click==0) {
b1.xpos=mouseX;
b1.ypos=mouseY;
b1.update();
}
else if (click==1) {
b2.xpos=mouseX;
b2.ypos=mouseY;
b2.update();
difx = b1.xpos-b2.xpos;
dify = b1.ypos-b2.ypos;
msg="click to place eightball and shoot";
} else if(click==2){
b1.xpos-=difx/steps;
b1.ypos-=dify/steps;
b1.update();
//cdistance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
float distance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
if(distance>b2.ballDiam/2){
moving = true;
b1.xpos-=difx/steps;
b1.ypos-=dify/steps;
}
else{
moving = false;
msg="new";
click=3;
}
}
else if(click==3){
if (b1.xpos<b2.xpos && b1.ypos<b2.ypos){
b2.xpos+=5;
b2.ypos+=5;
}
if (b1.xpos>b2.xpos && b1.ypos>b2.ypos){
b2.xpos-=5;
b2.ypos-=5;
}
if (b1.xpos>b2.xpos && b1.ypos<b2.ypos){
b2.xpos-=5;
b2.ypos+=5;
}
if (b1.xpos<b2.xpos && b1.ypos>b2.ypos){
b2.xpos+=5;
b2.ypos-=5;
}
msg="click again to start over";
}
else if(click==4){
setup();
}
textSize(20);
text(msg, 0, height-5);
}
void mouseClicked() {
if(!moving){
click++;
}
}
你在用什麼語言工作? – hurnhu
我正在使用程序處理。 –
嘿請考慮upvoting和/或接受我的答案,如果它幫助你... –