我获得了一个非面向对象的Pong程序,该程序通过处理运行并被告知将球变成一个单独的类中的对象。我试图将与球有关的所有代码部分移动到一个单独的" Ball" class和在ProcessingExample09类中创建一个球对象但是破坏了整个程序。我不确定如何去做,我所做的一切都在努力!任何人都可以浏览一下这个主要代码并给我一个关于如何将球变成单独物体的提示吗?谢谢!!
/**
* Test program #9: Simple pong game.
* This program uses a ball that moves "on its own" and a paddle that is
* controlled by the user.
*/
package lecture11_processing;
import processing.core.PApplet;
public class ProcessingExample09 extends PApplet
{
//dimensions of the canvas
int xMax = 500;
int yMax = 300;
//rectangle properties
//size
float rxSize = 100;
float rySize = 5;
//position
float rx = xMax/2 - rxSize/2;
float ry = yMax - rySize;
//increment size
float rxi = 2;
//ball properties
//initial position
float bx = xMax/2;
float by = yMax/2;
//size
float bxSize = 10;
float bySize = 10;
//step size in ball's movement
float bxi = random((float)1.5,3);
float byi = -random((float)1.5,3);
//state of the game
boolean gameOn = true;
public void setup()
{
size(xMax,yMax);
}
public void draw()
{
//redraw the background in each iteration of the draw method
background(127,127,127);
stroke(255, 255,255);
fill(255,255,255);
rect(0,0, 5, yMax);
rect(xMax-5,0, 5, yMax);
rect(0,0, xMax, 5);
//move the rectangle if the player uses left and right arrow keys
//if the rectangle is at one of the sides, it does not move past
//the side of the window
if ( keyPressed && key == CODED )
{
if ( keyCode == LEFT )
rx = rx - rxi;
else if (keyCode == RIGHT)
rx = rx + rxi;
if (rx < 0 ) rx = 0;
else if (rx+rxSize > xMax) rx = xMax-rxSize;
}
rect(rx, ry, rxSize, rySize);
//the ball moves until it is lost
if (gameOn)
{
//the ball bounces back from the two sides and the wall
if (bx >= xMax-5 || bx <= 0+5)
bxi = -bxi;
if (by <= 0+5)
byi = -byi;
//but the bottom has to be protected by the sliding
//rectangle or the ball is lost
if (by >= yMax - 5) {
//check if the rectangle is there
if ( bx >= rx && bx <= rx+rxSize)
byi = -byi;
else
gameOn = false;
}
bx = bx+bxi;
by = by+byi;
ellipse(bx, by, bxSize, bySize);
}
else
{
textAlign(CENTER);
text ("The ball is lost!\n"
+ "Try again.", 250, 150);
}
}
}
编辑:这是我的尝试
ProcessingExample09.java:
package lecture11_processing;
import processing.core.PApplet;
public class ProcessingExample09 extends PApplet
{
//dimensions of the canvas
int xMax = 500;
int yMax = 300;
//rectangle properties
//size
float rxSize = 100;
float rySize = 5;
//position
float rx = xMax/2 - rxSize/2;
float ry = yMax - rySize;
//increment size
float rxi = 2;
//state of the game
boolean gameOn = true;
public void setup()
{
size(xMax,yMax);
}
public void draw()
{
//redraw the background in each iteration of the draw method
background(127,127,127);
stroke(255, 255,255);
fill(255,255,255);
rect(0,0, 5, yMax);
rect(xMax-5,0, 5, yMax);
rect(0,0, xMax, 5);
//move the rectangle if the player uses left and right arrow keys
//if the rectangle is at one of the sides, it does not move past
//the side of the window
if ( keyPressed && key == CODED )
{
if ( keyCode == LEFT )
rx = rx - rxi;
else if (keyCode == RIGHT)
rx = rx + rxi;
if (rx < 0 ) rx = 0;
else if (rx+rxSize > xMax) rx = xMax-rxSize;
}
rect(rx, ry, rxSize, rySize);
Ball [] ball = new Ball[5];
for (int i = 0; i < 1; i++){
Ball x = new Ball();
ball[i] = x;
}
}
}
Ball.java:
package lecture11_processing;
public class Ball extends ProcessingExample09 {
public Ball(){
//ball properties
//initial position
float bx = xMax/2;
float by = yMax/2;
//size
float bxSize = 10;
float bySize = 10;
//step size in ball's movement
float bxi = random((float)1.5,3);
float byi = -random((float)1.5,3);
//the ball moves until it is lost
if (gameOn)
{
//the ball bounces back from the two sides and the wall
if (bx >= xMax-5 || bx <= 0+5)
bxi = -bxi;
if (by <= 0+5)
byi = -byi;
//but the bottom has to be protected by the sliding
//rectangle or the ball is lost
if (by >= yMax - 5) {
//check if the rectangle is there
if ( bx >= rx && bx <= rx+rxSize)
byi = -byi;
else
gameOn = false;
}
bx = bx+bxi;
by = by+byi;
ellipse(bx, by, bxSize, bySize);
}
else
{
textAlign(CENTER);
text ("The ball is lost!\n"
+ "Try again.", 250, 150);
}
}
}
答案 0 :(得分:1)
通常的方法是通过逐步的重构过程。
第一步:你需要一个球对象 - 所以创建一个球类 - 它可以是空的开始 - 一切都应该仍然有效。
第二步:您需要一个球实例 - 所以创建一个实例作为处理示例的成员 - 一切都应该仍然有效。
第三步:将适当的成员从ProcessingExample09
移到您的球类中。每次搬家后,一切都应该有效。
现在你有一个球类 - 它在这个阶段通常非常愚蠢,所以你开始寻找可以创建/移动到球类的函数。