Java and Myro breakout game program closes early -
i'm working classic breakout game java , myro. program loads ball , paddle, closes program.
what want the collection of myrobricks appear @ top of window , ball moves in random directions , bouncing off paddle. when hits 1 of bricks brick disappear , ball bounces.
here code:
import myro.*; import java.util.*; import java.awt.*; public class breakout { // declare 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 canvas 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 collection of bricks collection<myrorectangle> bricks = new arraylist<myrorectangle>(); //create rectangle(the paddle) myrorectangle paddle = new myrorectangle( mycanvas, 205, 475, 90, 25 ); //make visible paddle.makefilled(); paddle.setfillcolor( color.green ); paddle.visible(); //create ball myrocircle ball = new myrocircle( mycanvas, 250, 465, 10 ); ball.makefilled(); ball.setfillcolor( color.blue ); ball.visible(); //choose random delta x(negative) & y(not 0) int deltax = myroutils.randomint( -4,-1 ); int deltay = myroutils.randomint(-4, 4); //if chooses zero, 1 mycanvas.setautorepaint( false ); ( int c = 0; c<9; c++ ) { brickx = 0; (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 paddle controlled , 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(); (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 wrong code?
i think problem :
mycanvas.setvisible( false );
before statement canvas visible, change to:
mycanvas.setvisible( true );see difference. if it's not solution, shouldn't leave statement there,
jpanel.setvisible()
method serves set panel visible user if set true, , setting panel invisible after display itself. doesn't close running program if intention.
Comments
Post a Comment