gpt4 book ai didi

Java 和 Myro 突破游戏计划提前结束

转载 作者:行者123 更新时间:2023-12-01 14:39:34 28 4
gpt4 key购买 nike

我正在使用 java 和 myro 开发经典的突破游戏。该程序加载球和 Racket ,但随后立即关闭程序。

我想要的是 Myrobricks 的集合出现在窗口顶部,然后球以随机方向移动并从桨上弹起。当它击中其中一 block 砖 block 时,砖 block 就会消失并且球会弹起。

这是代码:

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 );
}
}
}

关于这段代码有什么问题你有什么想法吗?

最佳答案

我认为问题是:

myCanvas.setVisible( false );

在该语句之前,您的 Canvas 可见,将其更改为:

myCanvas.setVisible( true );
看看差异。即使这不是解决方案,您也不应该将该语句留在那里, JPanel.setVisible() 方法用于将面板设置为用户可见(如果设置为 true),并且您将面板设置为显示出来后很快就看不见了。如果您有意这样做的话,这不会关闭正在运行的程序。

关于Java 和 Myro 突破游戏计划提前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com