0

我正在使用 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 );
    }
}
}

关于这段代码有什么问题的任何想法?

4

1 回答 1

1

我认为问题是:

myCanvas.setVisible(假);

在该声明之前,您的画布可见,将其更改为:

myCanvas.setVisible(true);
看看有什么不同。即使这不是解决方案,您也不应该将该语句保留在那里,JPanel.setVisible()如果设置为 true,则该方法用于将面板设置为对用户可见,并且您正在将面板设置为在它显示后不久不可见。如果这是您的意图,那不会关闭正在运行的程序。

于 2013-04-20T00:59:43.543 回答