如何在主Processing类中显示和使用对象



我正试图在Processing中创建一个游戏。基本上是乒乓球。我有三节课。主类(PongRunner)、球类(PogBall)和球拍类(PonRacket)。

我试图在PongRunner类中显示PongRacket对象,但它没有被显示。这是我的代码:

PongRacket类:

    // this is the class for the pong racket used in the game
    // it specifies the racket's attributes and methods
    public class PongRacket{
      // attributes of the pong racket
      private int racketWidth = (1/9)*width; // width of the racket
      private int racketHeight = (1/15)*height; // height of the racket
      private int startRacketXPosition = (4/9)*width; // x value for starting position of racket
      private int startRacketYPosition = (14/15)*height; // y value for starting position of racket
      // display the racket
      public void displayRacket(){
        fill(0, 0, 0); // color for racket is black
        rect(getStartRacketXPosition(), getStartRacketYPosition(), getRacketWidth(), getRacketHeight());
      }
      // move the racket
      public void moveRacket(){
      }
      // setters
      public void setRacketWidth(int myWidth){
        this.racketWidth = myWidth;
      }
      public void setRacketHeight(int myHeight){
        this.racketHeight = myHeight;
      }
      // getters
      public int getRacketWidth(){
        return this.racketWidth;
      }
      public int getRacketHeight(){
        return this.racketHeight;
      }
      public int getStartRacketXPosition(){
        return startRacketXPosition;
      }
      public int getStartRacketYPosition(){
        return startRacketYPosition;
      }
    } // end class PongRacket

PongRunner级

    //this is the main class that runs the game
    // it consists of a racket
    // why doesn't the racket get displayed?
    PongRacket racket = new PongRacket(); // creates the racket object
    void setup(){
      size(width, height);
      frame.setResizable(true); // the frame can be resized now
    }
    void draw(){
      background(255); // color for background is white
      racket.displayRacket(); // why isn't this getting displayed?
    }

每当我打电话:

    racket.displayRacket(); // inside the PongRunner class

它不工作,也不在屏幕上显示任何内容。我该如何让它发挥作用?

我可以发现两个主要问题:

  1. 在调用size()之前,您正在创建PongRacket()(不推荐)
  2. 你在做整数除法(1/9),(1/15),(4/9),和(14/15)。如果您打印ln();您会注意到每一个结果都是0(println(14/15);)。请改用浮动(println(14.0/15);

这里有一个基本的重构:

PongRacket racket;
void setup() {
  size(width, height);
  frame.setResizable(true); // the frame can be resized now
  racket = new PongRacket(); // creates the racket object
}
void draw() {
  background(255); // color for background is white
  racket.displayRacket(); // why isn't this getting displayed?
}
public class PongRacket {
  // attributes of the pong racket
  private float racketWidth = (1.0/9)*width; // width of the racket
  private float racketHeight = (1.0/15)*height; // height of the racket
  private float startRacketXPosition = (4.0/9)*width; // x value for starting position of racket
  private float startRacketYPosition = (14.0/15)*height; // y value for starting position of racket
    // display the racket
  public void displayRacket() {
    fill(0, 0, 0); // color for racket is black
    rect(getStartRacketXPosition(), getStartRacketYPosition(), getRacketWidth(), getRacketHeight());
  }
  // move the racket
  public void moveRacket() {
  }
  // setters
  public void setRacketWidth(int myWidth) {
    this.racketWidth = myWidth;
  }
  public void setRacketHeight(int myHeight) {
    this.racketHeight = myHeight;
  }
  // getters
  public float getRacketWidth() {
    return this.racketWidth;
  }
  public float getRacketHeight() {
    return this.racketHeight;
  }
  public float getStartRacketXPosition() {
    return startRacketXPosition;
  }
  public float getStartRacketYPosition() {
    return startRacketYPosition;
  }
} 

最新更新