突破性Java游戏-卡在框架上打印积木

我目前正在研究一项任务,以重新创建Breakout游戏的简化版本。我已经为代码提供了基础,并且需要完成该应用程序,但是我仍然坚持将积木本身打印到面板上。

当使用方法paintBricks(g)时,有问题的部分位于BreakoutPanel类下,我不确定如何遍历Bricks本身,然后调用另一个类内的方法paint()。如果我尝试调用Brick.paint(g),它会给我消息“无法对非静态方法进行静态引用”。我对Java很陌生,因此任何提示或建议都将不胜感激。如果您需要整个代码,我也可以提供。

import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class BreakoutPanel extends JPanel implements ActionListener, KeyListener {

    static final long serialVersionUID = 2L;

    private boolean gameRunning = true;
    private int livesLeft = 3;
    private String screenMessage = "";
    private Ball ball;
    private Paddle paddle;
    private Brick[] bricks;

    public BreakoutPanel(Breakout game) {

        addKeyListener(this);
        setFocusable(true);

        Timer timer = new Timer(5, this);
        timer.start();

        //Create a new ball object and assign it to the appropriate variable
        ball = new Ball();
        //Create a new paddle object and assign it to the appropriate variable
        paddle = new Paddle();
        //Create a new bricks array (Use Settings.TOTAL_BRICKS)
        bricks = new Brick[Settings.TOTAL_BRICKS];
        //Call the createBricks() method
        createBricks();
    }

    private void createBricks() {
        int counter = 0;
        int x_space = 0;
        int y_space = 0;
        for(int x = 0; x < 4; x++) {
            for(int y = 0; y < 5; y++) {
                bricks[counter] = new Brick((x * Settings.BRICK_WIDTH) + Settings.BRICK_HORI_PADDING + x_space, (y * Settings.BRICK_HEIGHT) + Settings.BRICK_VERT_PADDING + y_space);
                counter++;
                y_space++;
            }
            x_space++;
            y_space = 0;
        }
    }

    public void paintBricks(Graphics g) {       
        //Loop through the bricks and call the paint() method
        for (int i = 0; i < Settings.TOTAL_BRICKS; i++) {
            for (int j = 0; j < Settings.TOTAL_BRICKS; j++) {
                Brick.paint(g);
         }
        }
    }

    private void update() {
        if(gameRunning) {
            // Update the ball and paddle
            collisions();
            repaint();
        }
    }

    private void gameOver() {
        //Set screen message
        screenMessage = "Game Over";
        stopGame();
    }

    private void gameWon() {
        // Set screen message
        screenMessage = "Winner winner chicken dinner!";
        stopGame();
    }
    ;
    private void stopGame() {
        gameRunning = false;
    }

    private void collisions() {
        // Check for loss
        if(ball.y > 450) {
            // Game over
            livesLeft--;
            if(livesLeft <= 0) {
                gameOver();
                return;
            } else {
                ball.resetPosition();
                ball.setYVelocity(-1);
            }
        }

        // Check for win
        boolean bricksLeft = false;
        for(int i = 0; i < bricks.length; i++) {
            // Check if there are any bricks left
            if(!bricks[i].isBroken()) {
                // Brick was found, close loop
                bricksLeft = true;
                break;
            }
        }
        if(!bricksLeft) {
            gameWon();
            return;
        }

        // Check collisions
        if(ball.getRectangle().intersects(paddle.getRectangle())) {
            // Simplified touching of paddle
            // Proper game would change angle of ball depending on where it hit the paddle
            ball.setYVelocity(-1);
        }

        for(int i = 0; i < bricks.length; i++) {
            if (ball.getRectangle().intersects(bricks[i].getRectangle())) {
                int ballLeft = (int) ball.getRectangle().getMinX();
                int ballHeight = (int) ball.getRectangle().getHeight();
                int ballWidth = (int) ball.getRectangle().getWidth();
                int ballTop = (int) ball.getRectangle().getMinY();

                Point pointRight = new Point(ballLeft + ballWidth + 1, ballTop);
                Point pointLeft = new Point(ballLeft - 1, ballTop);
                Point pointTop = new Point(ballLeft, ballTop - 1);
                Point pointBottom = new Point(ballLeft, ballTop + ballHeight + 1);

                if (!bricks[i].isBroken()) {
                    if (bricks[i].getRectangle().contains(pointRight)) {
                        ball.setXVelocity(-1);
                    } else if (bricks[i].getRectangle().contains(pointLeft)) {
                        ball.setXVelocity(1);
                    }

                    if (bricks[i].getRectangle().contains(pointTop)) {
                        ball.setYVelocity(1);
                    } else if (bricks[i].getRectangle().contains(pointBottom)) {
                        ball.setYVelocity(-1);
                    }
                    bricks[i].setBroken(true);
                }
            }
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        ball.paint(g);
        paddle.paint(g);
        paintBricks(g);

        // Draw lives left
        // TODO: Draw lives left in the top left hand corner



        // Draw screen message
        if(screenMessage != null) {
            g.setFont(new Font("Arial", Font.BOLD, 18));
            int messageWidth = g.getFontMetrics().stringWidth(screenMessage);
            g.drawString(screenMessage, (Settings.WINDOW_WIDTH / 2) - (messageWidth / 2), Settings.MESSAGE_POSITION);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        //Set the velocity of the paddle depending on whether the player is pressing left or right
        if(e.getKeyCode() == KeyEvent.VK_A) {
            paddle.setXVelocity(-2);
        } else if(e.getKeyCode() == KeyEvent.VK_D) {
            paddle.setXVelocity(2);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //Set the velocity of the paddle after the player has released the keys
         if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_D) {
             paddle.setXVelocity(0);
         }            
    }

    @Override
    public void keyTyped(KeyEvent arg0) {

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        update();
    }

}

我正在尝试从以下方法调用paint方法的类:


import java.awt.Graphics;

public class Brick extends Sprite {

    private boolean broken = false;

    public Brick(int x, int y) {
        //Set x using the parameter
        x = 0;
        //Set y using the parameter
        y = 0;
        //Set the width and height of the brick using Settings.BRICK_WIDTH/HEIGHT
        setWidth(Settings.BRICK_WIDTH);
        setHeight(Settings.BRICK_HEIGHT);
    }

    public boolean isBroken() {
        return broken;  //Return the correct variable
    }
    public void setBroken(boolean b) {
        //Set the broken variable using the parameter given
        b = true;
    }

    public void paint(Graphics g) {
        if(!broken) {
            g.fillRect(x, y, Settings.BRICK_WIDTH, Settings.BRICK_HEIGHT);
        }
    }
}

我还应该提到代码的其他部分尚未完全完成,它们不是我的问题的重点,因此可以忽略它们。

评论