所以我目前正在用java编写一个突破游戏的代码。现在,我已经设置好了,它会通过一个对话框提示玩家,询问玩家每行需要多少块砖(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 ----------------------------
/**
* 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;
}
}