0
我正在使用Java和myro的經典突破遊戲。程序加載球和槳,但隨後立即關閉程序。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);
}
}
}
任何想法,什麼是錯的代碼?
是否有例外? – 2013-04-20 00:54:52
也許它只是簡單地結束'while(true)'很快。嘗試增加'MyroUtils.sleep(0.01);' – acdcjunior 2013-04-20 00:58:23