【发布时间】:2017-06-13 02:22:57
【问题描述】:
好的,所以我在尝试在 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
}
}
【问题讨论】:
-
您应该能够使用与屏幕侧面相同的碰撞检测来检测球是否接触球拍。