Home > Back-end >  Please help have a look at where the code logic errors, hope to correct and advise a solution
Please help have a look at where the code logic errors, hope to correct and advise a solution

Time:09-17

This is a small brick game code
 package Game; 

Import the Java. The awt. Point;
Import the Java. The awt. Event. KeyEvent;

The import com. Rupeng. Game. GameCore;

Public class HitBricks implements Runnable
{

@ Override
Public void the run ()
{
//initialize the game interface
GameCore. SetGameTitle (" make bricks little game ");
GameCore. SetGameSize (1200, 800);
GameCore. LoadBgView (" lvsebg. JPG ");

//initialize the game's baffle
GameImage imgBoard=new GameImage (" board. JPG ");
ImgBoard. SetPostion (470, 720);

//initialize the game
Ball1 GameSprite spriteBall=new GameSprite (" ");
SpriteBall. SetPostion (300, 500);
SpriteBall. PlayAnimate (" rotate ");
Int ballVX=GameCore. Rand (1, 3);
Int ballVY GameCore.=rand (2, 0);;

//initialize the game's brick
BrickInfo [] [] bricks=new BrickInfo [10] [12].
for(int i=0; i{
BrickInfo brickRows=bricks [] [I];
for(int j=0; j{
BrickRows [j]=new BrickInfo ();
Boolean a=GameCore. Rand (0, 2)==0? True, false;
BrickRows [j] setHasBrick (a);
}
}
for(int i=0; i{
BrickInfo brickRows=bricks [] [I];
for(int j=0; j{
BrickInfo BrickInfo=brickRows [j];
If (brickInfo isHasBrick ())
{
GameImage imgBrick=new GameImage (" brick. JPG ");
ImgBrick. SetPostion (j * 100, I * 20);
BrickRows [j] setImgBrick (imgBrick);
}

}
}

//game loop start
While (true)
{
//key about control of the game's baffle
Int keycode=GameCore. GetPressedKeyCode ();
If (keycode==KeyEvent. VK_LEFT & amp; & ImgBoard. GetPostion (). X> 0)
{
ImgBoard. MoveLeft (15);
}
Else if (keycode==KeyEvent. VK_RIGHT & amp; & ImgBoard. GetPostion (). X + imgBoard. GetSize () width{
ImgBoard. MoveRight (15);
}

//the direction of the ball in the game speed control
Point posBall=spriteBall. GetPostion ();
Int xBall=posBall. X;
Int yBall=posBall. Y;
XBall +=ballVX * 6;
YBall +=ballVY * 6;

SpriteBall. SetPostion (xBall yBall);
If (xBall==0 | | xBall + spriteBall. GetSize () width> .=GameCore getGameWidth ())
{
BallVX=- ballVX;
}

If (yBall==0 | | imgBoard. IsIntersectWith (spriteBall))
{
BallVY=- ballVY;
}


//ball and brick after the collision of logical processing
For (BrickInfo [] brickRow: bricks)
{
For (BrickInfo brick: brickRow)
{
If (brick. IsHasBrick () & amp; & Brick. GetImgBrick (.) isIntersectWith (spriteBall))
{
Brick. SetHasBrick (false);
Brick. GetImgBrick (). Hide ();
BallVY=- ballVY;
}

}
}



If (yBall==GameCore. GetGameHeight ())
{
GameCore. Alert (" GAME OVER ");
}

}


}

Public static void main (String [] args)
{
GameCore. Start (new HitBricks ());

}

}

//the game bricks information
The class BrickInfo
{
Boolean hasBrick;
GameImage imgBrick;
Public BrickInfo ()
{

}
Public Boolean isHasBrick ()
{
Return hasBrick;
}
Public void setHasBrick (Boolean hasBrick)
{
Enclosing hasBrick=hasBrick;
}

Public GameImage getImgBrick ()
{
Return imgBrick;
}
Public void setImgBrick (GameImage imgBrick)
{
Enclosing imgBrick=imgBrick;
}

}





The running after the ball if above and brick will directly touch the border after the collision, as sometimes touch the right border,
Novice learning JAVA, for the first time I hope greatly help me solve the thank you, o great god told the solution,

CodePudding user response:

Where is the center of the ball? The lower left corner? Do you have about crossing the line judge, not up and down,

CodePudding user response:

The
refer to the original poster dotaxyh123 response:
this is a small brick game code

The running after the ball if above and brick will directly touch the border after the collision, as sometimes touch the right border,
Novice learning JAVA, for the first time I hope greatly help me solve the thank you, god told the solution,



Should not be code problem, it must be collision logic problems, have a good look at the algorithm of collision

CodePudding user response:

Related code logic, there is no boundary processing code
1, the boundary position to set up and down, up and down (maxY/minY), (or so) maxX/minX
2, use (Y +/- half brick height), or so can be used (X coordinate +/- half brick width) judgment
  • Related