我正在使用 java 和 myro 开发经典的突围游戏。程序加载球和桨,然后立即关闭程序。
我想要的是让 Myrobricks 的集合出现在窗口的顶部,然后球在随机方向移动并从桨上弹起。当它击中其中一个砖块时,砖块消失,球反弹。
这是代码:
import Myro.*;
import java.util.*;
import java.awt.*;
public class BreakOut
{
// declare the Scribbler object
private Scribbler robot;
private boolean intersects( MyroShape s1, MyroShape s2 )
{
if( s1.getTop() >= s2.getBottom() || s1.getBottom() <= s2.getTop() || s1.getLeft() >= s2.getRight() || s1.getRight() <= s2.getLeft() )
return false;
else
return true;
}
public void main()
{ //set a canvas that has 500 width, 500 height
final int WIDTH = 500;
final int HEIGHT = 500;
MyroCanvas myCanvas = new MyroCanvas( "Breakout",WIDTH, HEIGHT );
int brickX = 0;
int brickY = 60;
//creating the collection of bricks
Collection<MyroRectangle> bricks = new ArrayList<MyroRectangle>();
//create a rectangle(the paddle)
MyroRectangle paddle = new MyroRectangle( myCanvas, 205, 475, 90, 25 );
//make it visible
paddle.makeFilled();
paddle.setFillColor( Color.green );
paddle.visible();
//create a ball
MyroCircle ball = new MyroCircle( myCanvas, 250, 465, 10 );
ball.makeFilled();
ball.setFillColor( Color.blue );
ball.visible();
//choose a random Delta X(negative) & Y(not 0)
int deltaX = MyroUtils.randomInt( -4,-1 );
int deltaY = MyroUtils.randomInt(-4, 4);
//if it chooses zero, do another one
myCanvas.setAutoRepaint( false );
for ( int c = 0; c<9; c++ )
{
brickX = 0;
for (int r = 0; c<4; c++)
{
MyroRectangle rectangles = new MyroRectangle( myCanvas, brickX, brickY, 50, 20 );
int red = MyroUtils.randomInt (0, 255);
int green = MyroUtils.randomInt (0, 255);
int blue = MyroUtils.randomInt (0, 255);
rectangles.setFillColor( new Color (red, green, blue ));
rectangles.makeFilled();
rectangles.visible();
bricks.add( rectangles );
brickX = brickX + 50;
}
brickY = brickY + 20;
}
while (deltaY == 0)
{
deltaY = MyroUtils.randomInt(-4, 4);
}
boolean finished = false;
while (!finished)
{
ball.move( deltaX, deltaY );
MyroUtils.sleep( 0.01);
int bricksCount = 40;
//implementing the paddle that is controlled by the A and D keys
if( MyroListener.isKeyPressed() )
{
if( MyroListener.whichKey() == 'q' )
{
// quit
finished = true;
}
else if( MyroListener.whichKey() == 'a' )
{
paddle.move( -4,0 );
if( paddle.getLeft() < 0 )
{
paddle.move( 0, 0 );
}
}
else if( MyroListener.whichKey() == 'd' )
{
paddle.move( 4,0 );
if( paddle.getRight() > 500 )
{
paddle.move( 0, 0 );
}
}
}
if ( ball.getBottom() == 500 )
finished = true;
if ( ball.getBottom()==0 )
ball.move(deltaX,-deltaY);
if ( ball.getLeft()==0 || ball.getRight()==500 )
ball.move(-deltaX, deltaY);
if( intersects( ball, paddle ))
ball.move(deltaX,-deltaY);
myCanvas.repaint();
for (MyroRectangle rectangles : bricks)
{
if(rectangles.isVisible() && intersects( ball, rectangles ))
{
rectangles.invisible();
deltaY = -deltaY;
bricksCount--;
}
}
if( bricksCount == 0 )
{
finished = true;
MyroGUI.tellUser( "Congratulation!" );
}
myCanvas.setVisible( false );
}
}
}
关于这段代码有什么问题的任何想法?