java - 砖 block 位置,在打砖 block 游戏中,使用 Java 中的数组

标签 java arrays breakout

我一直在为 Uni(经典街机游戏 Breakout)开发 Java 项目,到目前为止已经成功创建了球棒和球对象,并且它们按预期工作。我想使用数组来实现砖墙,因为让每个砖 block 都有自己的对象会导致代码效率低下,但我在 Java 方面的经验并没有扩展到数组,而且我知道,与 Python 不同,它们很难工作。

我希望根据已建立的 x 和 y 参数为砖 block 指定不同的位置。

这是我要添加数组的模型类;

public class Model 
{
    // First, a collection of useful values for calculating sizes and layouts etc.

    public int B              = 6;      // Border round the edge of the panel
    public int M              = 40;     // Height of menu bar space at the top

    public int BALL_SIZE      = 30;     // Ball side
    public int BRICK_WIDTH    = 50;     // Brick size
    public int BRICK_HEIGHT   = 30;

    public int BAT_MOVE       = 5;      // Distance to move bat on each keypress
    public int BALL_MOVE      = 3;      // Units to move the ball on each step

    public int HIT_BRICK      = 50;     // Score for hitting a brick
    public int HIT_BOTTOM     = -200;   // Score (penalty) for hitting the bottom of the screen

    View view;
    Controller controller;

    public GameObj ball;                // The ball
    public ArrayList<GameObj> bricks;   // The bricks
    public GameObj bat;                 // The bat
    public int score = 0;               // The score

    // variables that control the game 
    public boolean gameRunning = true;  // Set false to stop the game
    public boolean fast = false;        // Set true to make the ball go faster

    // initialisation parameters for the model
    public int width;                   // Width of game
    public int height;                  // Height of game

    // CONSTRUCTOR - needs to know how big the window will be
    public Model( int w, int h )
    {
        Debug.trace("Model::<constructor>");  
        width = w; 
        height = h;
    }

    // Initialise the game - reset the score and create the game objects 
    public void initialiseGame()
    {       
        score = 0;
        ball   = new GameObj(width/2, height/2, BALL_SIZE, BALL_SIZE, Color.RED );
        bat    = new GameObj(width/2, height - BRICK_HEIGHT*3/2, BRICK_WIDTH*3, 
            BRICK_HEIGHT/4, Color.GRAY);
        bricks = new ArrayList<>();
        // ***HERE***

    }

这是我想添加到 View 类中以在 GUI 中绘制砖 block 的相应代码;

    public void drawPicture()
{
    // the ball movement is runnng 'i the background' so we have
    // add the following line to make sure
    synchronized( Model.class )   // Make thread safe (because the bal
    {
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // clear the canvas to redraw
        gc.setFill( Color.WHITE );
        gc.fillRect( 0, 0, width, height );

        // update score
        infoText.setText("BreakOut: Score = " + score);

        // draw the bat and ball
        displayGameObj( gc, ball );   // Display the Ball
        displayGameObj( gc, bat  );   // Display the Bat

        // ***HERE***

    }
}

可以查看当前状态的 .jar 项目文件 here .

顺便说一句, bat 有一个小错误,它击中任一侧时都不会停止,不确定让它保持在窗口参数范围内的最佳方法是什么。

提前致谢!!

最佳答案

您已经拥有了砖 block 的 ArrayList。创建一个函数将行/列坐标转换为数组的索引。假设您有一个 3x4 网格(3 行,4 列):

0 |_|_|B|_|
1 |_|_|_|_|
2 |_|_|_|_|
   0 1 2 3

您的 ArrayList 的大小为 12 (3x4)。

private int translateCoordToIndex(int row, int col) {
  return row * TOTAL_COLS + col
}

因此,对于图中 row=0,col=2 处的积木,其位置为 (0*4)+2 = 2,这意味着 ArrayList bricks 中的索引 2 .get(2)

您甚至可以将 ArrayList 更改为 GameObj 的通用数组 GameObj[]

或者,您可以使用 GameObj 的二维数组来表示砖 block 网格。

GameObj[][] bricks = new GameObj[rows][cols] 这样您就可以使用其行和列位置访问确切的砖 block - 例如bricks[0][0] 表示第一 block 砖。

两种方法几乎相同。

关于java - 砖 block 位置,在打砖 block 游戏中,使用 Java 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60074301/

相关文章:

java - DB2 jdbc 性能

c++ - 我应该如何将对象传递给类以检查碰撞?

java - 使用 tomcat 设置 web dav

java - 关于比较器条件

Python:关于 "numpy.ndarray"的两个问题

arrays - AutoIt 从数组中获取子数组

java - 在 Java 方法中更改我的背景

java - libgdx 中的突破球碰撞

java - weblogic.xml : cvc-complex-type. 2.4.a 中的错误:发现以元素 'prefer-application-packages' 开头的无效内容

java - 从 Android 编写的文本文件中读取数值