I'm working with the classic breakout game with java and myro. The program loads the ball and the paddle, but then immediately closes the program.
What I want is for the the collection of Myrobricks to appear at the top of the window and then the ball moves in random directions and bouncing off the paddle. When it hits one of the bricks the brick disappear and the ball bounces.
Here is the code:
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 );
}
}
}
Any ideas as to what is wrong with this code?
I think the problem is :
Before that statement your canvas was visible, change it to:
to see the difference. Even if it's not the solution, you shouldn't leave that statement there,JPanel.setVisible()
method serves to set the panel visible to the user if set to true, and you are setting the panel to be invisible soon after it display itself. That doesn't close the running program if that was your intention.