だから私は現在Javaでブレイクアウトゲームのコードを書いています。現在、1 行に必要なレンガの数 (4 ~ 40 の数字) をプレイヤーに尋ねるダイアログ ボックスが表示されるように設定しています。めちゃくちゃになる部分は、ボールとパドルとの衝突全体です。私はプログラミングに非常に慣れていないので、信じられないほど簡単であれば許してください。博士を使用しています。ジャバ。
//------------------------------- animate -------------------------
/**
* this should send the ball back after hitting the paddle.
*
*/
public void animate( )
{
int dX = ball.getXLocation( );
int dY = ball.getYLocation( );
while( true )
{
ball.move( );
if ( ball.boundsIntersects( paddle) )
{
dY= -1*dY;
ball.setLocation( dX,dY );
}
for(int i = 0; i < bricks.size(); i++)
{
if (ball.boundsIntersects(bricks.get(i)))
{
dY = -dY;
ball.setLocation(dX,dY);
bricks.get(i).hide();
bricks.remove(i);
System.out.println("brick");
}
}
これが私のボールクラスの move メソッドです。恐ろしいコードでもう一度申し訳ありません。
//------------------------------- move ----------------------------
/**
* This will move the ball by deltaX and deltaY
* and bounce the ball off the edges of the Frame.
*
*/
public void move( )
{
int dX = this.getXLocation( ) + deltaX;
int dY = this.getYLocation( ) + deltaY;
this.setLocation( dX, dY );
if( getYLocation( ) < 0 )
{
setLocation( dX, 0 );
deltaY *=-1;
}
else if( getYLocation( ) > ( 500 + size ) )
{
setLocation( dX, 500-size);
deltaY *=-1;
}
if( getXLocation() < 0 )
{
setLocation( dX , dY );
deltaX *=-1;
}
else if( getXLocation( ) > ( 500 + size ) )
{
setLocation( 500-size, dY );
deltaX *=-1;
}
}