Java中的乒乓游戏

时间:2014-04-02 10:16:43

标签: java

我试图做一个简单的乒乓球比赛,但我不能让我的球移动。我为球和桨创建了2个类,但我不知道如何在Pong类中调用移动和弹跳的方法(如果我从Ball类启动它可以正常工作)。

每当我尝试从Ball课程调用方法时,我都会得到:

Cannot make a static reference to the non-static method bounce(GOval) from the type Ball

如果我尝试快速修复,我只是从另一种方法得到另一个错误,直到我到达一个我无法改变,如getHeight();

如何让Ball的方法在Pong中工作? 我应该将所有方法移至Pong课程,只留下makeBall(); Ball内的内容吗?

我没有将球从球拍上移开或移动球拍的代码,但我稍后会对此进行处理。我只是想让球开始移动。

球:

package MyObjects;
import java.awt.Color;
import acm.graphics.GOval;
import acm.program.GraphicsProgram;


public class Ball extends GraphicsProgram{
    private static final double BALL_SIZE=10;
    private static final double SPEED=1;
    private static final double PAUSE = 1000/48.0;
    private static boolean HIT = false;
    public double dx=SPEED;
    public double dy=1;

    public void run(){
        GOval ball = makeBall();
        add(ball);
        bounce(ball);
    }

    public static GOval makeBall(){
        GOval result = new GOval (20,20,BALL_SIZE,BALL_SIZE);
        result.setFilled(true);
        result.setColor(Color.BLUE);
        return result;

    }

    public void bounce(GOval ball){ 
        while(true){
            ball.move(dx,dy);
            if(ballHitBottom(ball) && dy>=0){
                dy*=-1;
                if(HIT==false)
                HIT=true;   
            }
            if(ballHitTop(ball) && dy<=0){
                if(HIT){
                dy*=-1;
                }
            }
            pause(PAUSE);
        }
    }

    private boolean ballHitBottom(GOval ball){
        double bottomY=ball.getY() + ball.getHeight();
        return bottomY >= getHeight();
    }

    private boolean ballHitTop(GOval ball){
        double topY=ball.getY();
        return topY <= 0;
    }
}

桨:

package MyObjects;

import java.awt.Color;

import acm.graphics.GOval;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;

public class Paddle extends GraphicsProgram{
private static double HEIGHT=100;
private static double WIDTH=5;

public void run(){
    GRect paddle = makePaddle();
    add(paddle);
    paddle.sendToBack();
}

public static GRect makePaddle(){
    GRect result = new GRect(10,10,WIDTH,HEIGHT);
    result.setFilled(true);
    result.setColor(Color.BLACK);
    return result;

}

}

Pong:

import MyObjects.Ball;
import MyObjects.Paddle;
import acm.graphics.GOval;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;


public class Pong extends GraphicsProgram{
public void run(){
 GOval ball = Ball.makeBall();
 add(ball);
 GRect paddle = Paddle.makePaddle();
 add(paddle);

 Ball.bounce(ball);    // this won't work

}

}

2 个答案:

答案 0 :(得分:1)

我认为我们需要看看GraphicsProgram的功能,说实话。但我只是声明一个非静态的Ball实例:

public class Pong extends GraphicsProgram {
    public void run() {
        // you'll need to add a constructor to the Ball class
        Ball base = new Ball();
        GOval ball = base.makeBall();
        add(ball);
        GRect paddle = Paddle.makePaddle();
        add(paddle);
        base.bounce(ball);
    }
}

答案 1 :(得分:0)

您正在引用非静态方法来自静态参考,例如Ball.bounce(ball);。 有两种方法可以解决这个问题

  1. 您可以做的是将反弹方法的签名更改为静态。

    public static void bounce(GOval ball){ 
    while(true){
        ball.move(dx,dy);
        if(ballHitBottom(ball) && dy>=0){
            dy*=-1;
            if(HIT==false)
            HIT=true;   
        }
        if(ballHitTop(ball) && dy<=0){
            if(HIT){
            dy*=-1;
            }
        }
        pause(PAUSE);
    }
    }
    
  2. 像这样调用弹跳方法。 ball.bounce();并将反弹方法归为此。

    public void bounce(){ 
    while(true){
        this.move(dx,dy);
        if(ballHitBottom(this) && dy>=0){
            dy*=-1;
            if(HIT==false)
            HIT=true;   
        }
        if(ballHitTop(this) && dy<=0){
            if(HIT){
            dy*=-1;
            }
        }
        pause(PAUSE);
    }
    }
    
相关问题