【问题标题】:Pong Game in JavaJava中的乒乓游戏
【发布时间】:2014-04-02 10:16:42
【问题描述】:

我正在尝试制作一个简单的乒乓球游戏,但我的球无法移动。我为球类和桨类创建了 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;

}

}

乒乓:

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

}

}

【问题讨论】:

    标签: java


    【解决方案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);
        }
    }
    

    【讨论】:

    • 这种工作很好,球现在移动了,但从右上角而不是右下角开始,粘在窗口顶部并向右滑出屏幕(不t 弹跳)。我不知道如何向您展示 GraphicsProgram 的功能,它只是我们在大学课堂上获得并一直在使用的资源包。
    • 您的问题可能与GOval.move(dx, dy) 相关。我不知道那个类是什么,或者它做什么。或者这可能是你的球的起始位置有问题。
    • 我不知道我还能做些什么,但这是我正在使用的 GraphicsProgram 东西。如果有任何帮助,这是我的程序从 eclipse 导出的内容:link
    • 如果我在 Pong 类中写下整个东西会有帮助吗?不使用球和桨
    • 您没有设置要添加到 GraphicsProgram 的 GOval 的 x 和 y 坐标 - link
    【解决方案2】:

    您正在从静态引用引用非静态方法,例如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. 像这样调用bounce方法。 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);
      }
      }
      

    【讨论】:

    • //使用 1. 如果我将弹跳固定为静态,我还需要将 ballHitBottom 和 Top 更改为静态,然后在 ballHitBottom 内部我需要将 getHeight() 更改为静态,我不能这样做/ //using 2. this 好像也不起作用,还不知道为什么,但是 this.move 有问题
    • 那么你必须在你的球类中创建一个GOval ball 球属性。在你的反弹方法中你可以做this.ball.move(dx,dy)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    相关资源
    最近更新 更多