我试图在Processing中创建游戏。基本上是它的乒乓球。 我有3节课。主要类(PongRunner),球类(PongBall)和球拍类(PongRacket)。
我正在尝试在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
它不起作用或在屏幕上显示任何内容。我怎样才能让它发挥作用?
答案 0 :(得分:0)
我可以发现两个主要问题:
size()
之前创建PongRacket()(不推荐)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;
}
}