0
我正在爲我的Java課設計一個檯球遊戲。我遇到檯球碰撞問題。球撞到對方,偶爾滑入對方,卡住了。我似乎無法確定這個錯誤的原因。我希望有人能幫助我找出問題的原因。我的代碼如下。謝謝。檯球撞到彼此
float cueX = 200;
float cueY = 225;
float cueDeltaX;
float cueDeltaY;
float ballWidth = 25;
float score = 0;
Billiards[] billiards = new Billiards[3];
void setup()
{
size (850, 450);
background(0);
fill(#29B748);
rect(0, 0, 599, 599);
billiards[0] = new Billiards(600, 225, 0, 0, false, "", 0);
billiards[1] = new Billiards(625, 211, 0, 0, false, "", 1);
billiards[2] = new Billiards(625, 239, 0, 0, false, "", 2);
//billiards[3] = new Billiards(625, 250, 0, 0, false, "", 2);
}
void draw()
{
background(0);
fill(#FFFFFF);
stroke(#A6A7A6);
text("DeltaX: " + cueDeltaX + " Delta Y: " + cueDeltaY, 20, 20);
text(score, 500, 20);
fill(#29B748);
rect(25, 25, 799, 399);
poolCueLines();
drawCue();
moveCue();
cueBounce();
cueFriction();
drawBilliards();
billiards[0].collision();
billiards[0].moveBall();
billiards[0].billiardBounce();
billiards[0].billiardFriction();
billiards[1].collision();
billiards[1].moveBall();
billiards[1].billiardBounce();
billiards[1].billiardFriction();
billiards[2].collision();
billiards[2].moveBall();
billiards[2].billiardBounce();
billiards[2].billiardFriction();
}
void poolCueLines() {
if (mousePressed)
{
stroke(#FFFFFF);
line(cueX, cueY, mouseX, mouseY);
}
}
void mouseReleased()
{
cueDeltaX = (cueX - mouseX)/50;
cueDeltaY = (cueY - mouseY)/50;
}
void drawCue() {
noStroke();
fill(0);
fill(#FFFFFF);
stroke(#A6A7A6);
ellipse(cueX, cueY, ballWidth, ballWidth);
noFill();
}
void moveCue() {
cueX += cueDeltaX;
cueY += cueDeltaY;
}
void cueBounce() {
if (cueX > width-25-ballWidth/2 || cueX < 25 + ballWidth/ 2) {
cueDeltaX = -cueDeltaX;
cueDeltaX = cueDeltaX * 0.6;
if (cueX < 25+ ballWidth/2) {
cueX = 26 + ballWidth/2;
} else {
cueX = width-26-ballWidth/2;
}
}
if (cueY > height-25-ballWidth/2 || cueY < 25 + ballWidth/ 2) {
cueDeltaY = -cueDeltaY;
cueDeltaY = cueDeltaY * 0.6;
if (cueY < 25+ ballWidth/2) {
cueY = 26 + ballWidth/2;
} else {
cueY = height-26-ballWidth/2;
}
}
}
void drawBilliards() {
//Yellow Ball 1
fill(#ffff00);
stroke(#A6A7A6);
ellipse(billiards[0].ballXpos, billiards[0].ballYpos, ballWidth, ballWidth);
//Blue 2
fill(#000099);
stroke(#A6A7A6);
ellipse(billiards[1].ballXpos, billiards[1].ballYpos, ballWidth, ballWidth);
//Red 3
fill(#ff0000);
stroke(#A6A7A6);
ellipse(billiards[2].ballXpos, billiards[2].ballYpos, ballWidth, ballWidth);
}
void cueFriction() {
cueDeltaX = cueDeltaX * 0.995;
cueDeltaY = cueDeltaY * 0.995;
}
class Billiards
{
float ballXpos;
float ballYpos;
float deltaXball;
float deltaYball;
int billiardsNum;
Billiards(float tempXpos, float tempYpos, float deltaXbill, float deltaYbill, boolean stripe, String stripeColor, int billiardNum) {
ballXpos = tempXpos;
ballYpos = tempYpos;
deltaXball = deltaXbill;
deltaYball = deltaYbill;
billiardsNum = billiardNum;
}
void collision() {
if (cueX > ballXpos-ballWidth && cueX < ballXpos+ballWidth) {
if (cueY < ballYpos+ballWidth && cueY > ballYpos-ballWidth) {
cueDeltaX = -cueDeltaX * 0.8;
deltaXball = -cueDeltaX * 0.6;
cueDeltaY = -cueDeltaY * 0.8;
deltaYball = -cueDeltaY * 0.6;
}
}
int ballNum = 0;
for (int i=0; i < 3; i++) {
if (billiards[ballNum].ballXpos > ballXpos-ballWidth && billiards[ballNum].ballXpos < ballXpos+ballWidth) {
if (billiards[ballNum].ballYpos < ballYpos+ballWidth && billiards[ballNum].ballYpos > ballYpos-ballWidth) {
if (billiardsNum == ballNum) {
} else {
//if (billiards[ballNum].deltaXball < 0.2 || billiards[ballNum].deltaYball < 0.2) {
if (deltaXball > 0){
billiards[ballNum].ballXpos += -3;
}else if (deltaXball < 0){
billiards[ballNum].ballXpos += 3;
}
if (deltaYball > 0){
billiards[ballNum].ballXpos += -3;
}else if (deltaYball < 0){
billiards[ballNum].ballXpos += 3;
}
billiards[ballNum].deltaXball = -billiards[ballNum].deltaXball * 0.8;
deltaXball = -billiards[ballNum].deltaXball * 0.6;
billiards[ballNum].deltaYball = -billiards[ballNum].deltaYball * 0.8;
deltaYball = -billiards[ballNum].deltaYball * 0.6;
//}
//} else {
// billiards[ballNum].deltaXball = -billiards[ballNum].deltaXball * 0.8;
// deltaXball = -billiards[ballNum].deltaXball * 0.6;
// billiards[ballNum].deltaYball = -billiards[ballNum].deltaYball * 0.8;
// deltaYball = -billiards[ballNum].deltaYball * 0.6;
//}
}
}
}
ballNum += 1;
}
}
void moveBall() {
ballXpos += deltaXball;
ballYpos += deltaYball;
}
void billiardBounce() {
if (ballXpos > width-25-ballWidth/2 || ballXpos < 25 + ballWidth/ 2) {
deltaXball = -deltaXball;
deltaXball = deltaXball * 0.6;
if (ballXpos < 25+ ballWidth/2) {
ballXpos = 26 + ballWidth/2;
} else {
ballXpos = width-26-ballWidth/2;
}
}
if (ballYpos > height-25-ballWidth/2 || ballYpos < 25 + ballWidth/ 2) {
deltaYball = -deltaYball;
deltaYball = deltaYball * 0.6;
if (ballYpos < 25+ ballWidth/2) {
ballYpos = 26 + ballWidth/2;
} else {
ballYpos = height-26-ballWidth/2;
}
}
}
void billiardFriction() {
deltaXball = deltaXball * 0.995;
deltaYball = deltaYball * 0.995;
}
}
請提供重現問題的驅動程序以及適用的跟蹤輸出在有問題的對象上。在這裏至少應該有一些打印語句來查明發生了什麼,如果不是確切地說是怎麼出錯的話。 – Prune
@Prune雖然我同意這是一個太多的代碼,但請注意,這是[tag:processing],而不是Java。此類是驅動程序,因爲Processing會自動爲您調用某些功能。它沒有像Java那樣的'main()'方法。 –