java - Java 突围游戏 - 移除砖块
问题描述
我已经在我的突围游戏中添加了多排砖块,但是我现在的任务是在它们被击中后移除这些砖块。我添加了一个 for 循环,它似乎从中间的一排砖块反弹,但没有移除任何东西。我在 ActivePart 类中工作。
class ActivePart
{
private boolean runGame = true;
public void stop()
{
runGame = false;
}
public void runAsSeparateThread()
{
final float S = 3; // Units to move (Speed)
try
{
synchronized ( Model.class ) // Make thread safe
{
GameObj ball = getBall(); // Ball in game
GameObj bat = getBat(); // Bat
List<GameObj> bricks = getBricks(); // Bricks
}
while (runGame)
{
synchronized ( Model.class ) // Make thread safe
{
float x = ball.getX(); // Current x,y position
float y = ball.getY();
// Deal with possible edge of board hit
if (x >= W - B - BALL_SIZE) ball.changeDirectionX();
if (x <= 0 + B ) ball.changeDirectionX();
if (y >= H - B - BALL_SIZE) // Bottom
{
ball.changeDirectionY(); addToScore( HIT_BOTTOM );
}
if (y <= 0 + M ) ball.changeDirectionY();
// As only a hit on the bat/ball is detected it is
// assumed to be on the top or bottom of the object.
// A hit on the left or right of the object
// has an interesting affect
boolean hit = false;
// *[3]******************************************************[3]*
// * Fill in code to check if a visible brick has been hit *
// * The ball has no effect on an invisible brick *
// **************************************************************
for ( int i = 0; i <= 60; i++ ){
GameObj brick1 = bricks.get(i);
if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
hit = true;
Debug.trace("BreakOut");
}
} // here is what i am working on
if (hit)
ball.changeDirectionY();
if ( ball.hitBy(bat) )
ball.changeDirectionY();
}
modelChanged(); // Model changed refresh screen
Thread.sleep( fast ? 2 : 20 );
ball.moveX(S); ball.moveY(S);
}
} catch (Exception e)
{
Debug.error("Model.runAsSeparateThread - Error\n%s",
e.getMessage() );
}
}
}
/**
* Model has changed so notify observers so that they
* can redraw the current state of the game
*/
public void modelChanged()
{
setChanged(); notifyObservers();
}
}
我不知道如何从游戏中移除实际的积木,它目前只是通过看起来而不是实际积木来反弹一条线。我认为我需要为每个单独的行循环。
加砖代码:
bricks = new ArrayList<>();
// *[1]******************************************************[1]*
// * Fill in code to place the bricks on the board *
// **************************************************************/
// pink first 2 rows
for (int i = 0; i < NBRICK_ROWS; i++){
int y = BRICK_Y_OFFSET + (i * (BRICK_HEIGHT + BRICK_SEP));
for (int j = 0; j < NBRICKS_PER_ROW; j++){
int x = (BRICK_X_OFFSET) + (j * (BRICK_WIDTH + BRICK_SEP));
bricks.add(new GameObj (x, y, BRICK_WIDTH, BRICK_HEIGHT, Colour.PINK));
}
}
在 drawActualPicture 方法的视图类中,这显示了砖块:
for(GameObj brick : bricks) {
displayGameObj( g, brick);
}
这就是我现在所拥有的:
if( // ball Y pos < brick Y pos = collision )
{
brick = null;
}
for ( int i = 0; i <= 60; i++ ){
GameObj brick1 = bricks.get(i);
if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
bricks.set(i, null); // The brick in the position of i when the ball collides, will be null
hit = true;
Debug.trace("BreakOut");
}
List<GameObj> toRemove = new ArrayList<GameObj>();
for(GameObj a: bricks){
if(a.getY() <= brick1.getY() - (BRICK_HEIGHT/2)){
toRemove.add(a);
}
}
bricks.removeAll(toRemove);
if (hit)
ball.changeDirectionY();
if ( ball.hitBy(bat) )
ball.changeDirectionY();
}
modelChanged(); // Model changed refresh screen
Thread.sleep( fast ? 2 : 20 );
ball.moveX(S); ball.moveY(S);
}
} catch (Exception e)
{
Debug.error("Model.runAsSeparateThread - Error\n%s",
e.getMessage() );
}
}
解决方案
为什么不直接破坏GameObj
砖块?
if(<ball collide with brick>)
{
brick = null;
}
for ( int i = 0; i <= 60; i++ ){
GameObj brick1 = bricks.get(i);
if ( brick1.hitBy(ball) ){
bricks.remove(i);
//hit = true;
ball.changeDirectionY();
}
}
推荐阅读
- reactjs - React Material UI:禁用时如何为按钮提供自定义颜色?
- java - 在java中比较来自不同数据库的两个数据集(两个结果集)
- swift - 如何快速更改按钮颜色和背景颜色
- progressive-web-apps - 我们是否需要更新 Play 商店中的 PWA 应用才能发布新的更改?
- python-3.x - 当我在 python 中使用 googletrans 时出现 JSONDecodeError
- ebay-api - Ebay API - 购物 API - GetShippingCosts:不支持 HTTPS?
- java - 使用 javax.ws.rs.core.Response 和断言的模拟方法
- active-directory - 针对自定义用户存储的桌面登录
- sql-server - 我可以为一个主表制作一个从表,比如主从数据库复制吗?
- css - 修复警告“同时定义标准属性‘box-shadow’以实现兼容性”