0
好的,我在嘗試在BlueJ上編程的遊戲遇到了一些困難。到目前爲止,我有一個彈跳的球,還有一個可以用鼠標移動的槳。我試圖讓球從我的球杆反彈,基本上我移動它的地方。如何彈動移動球的球
我有三個班,我知道它很長,但我真的可以使用一些幫助。
public class MAIN {
public void begin(){
Paddle = new Paddle(140,400,100,canvas);
ball = new Ball (180,50,canvas, Paddle)//ball can recognize paddle
}
public void onMousemove (Location p){
Paddle.move(p);
}
}
public class Paddle {
public Paddle (int x, int y, int s, DrawingCanvas c){
((JDrawingCanvas)c).setBackground(Color.green);
paddle = new FilledRect(x,y,s,10,c);
}
public void move (Location p){
paddle.moveTo(p.getX(),p.getY());
} //paddle, and to move paddle
}
public class Ball {
Drawing Canvas myCanvas;
FilledRect paddle;
FilledOval ball;
int dx = 3, dy = 5;
public Ball (int x, int y, int s, DrawingCanvas c, Paddle paddle){
myCanvas = c
((JDrawingCanvas)c).setBackground(Color.green);
ball = new FilledOval (x,y,s,s,c);
pad = paddle;
start();
}
public void run() //bounces the ball around
{
while(true){
if(ball.getX() <0 ||ball.getX() > myCanvas.getWidth()){
dx = dx;
}
if(ball.getY() <0 ||ball.getY() > myCanvas.getWidth()){
dy = dy;
}
move5();
pause(20);
}
public Rectangle getRectangle(){
return new Rectangle ((int)paddle.getX(), paddle.getY(),
paddle.getWidth(), paddle.getHeight();
}
public boolean collision (Ball p){
return getRectangle().intersects(p.getRectangle());
}
public void move5(){
ball.move(dx,dy)
}
public void collide(){
dx= -dx
dy=-dy
}
}
您應該可以使用與屏幕兩側相同的碰撞檢測來檢測球是否正在碰觸槳。 –