21xrx.com
2024-11-25 12:03:50 Monday
登录
文章检索 我的文章 写文章
Java入门小游戏代码:打砖块游戏
2023-06-16 16:43:32 深夜i     --     --
Java编程 小游戏 打砖块

Java是一种跨平台的编程语言,越来越受到程序员和开发者的欢迎。如果你正在学习Java编程,那么做一个小游戏是一个不错的练习。今天,我们将在Java中创建一个小游戏:打砖块游戏。

首先,我们需要创建一个窗口,显示我们的游戏。然后我们需要创建一些砖块和一个球,并且让球弹来弹去,当球击中砖块时,砖块就会消失。当所有砖块都被消除后,游戏就结束了。

下面是打砖块游戏的完整源代码:

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

public class BrickBreaker extends JPanel implements KeyListener {

  private int ballX = 300;

  private int ballY = 400;

  private int ballSpeedX = 3;

  private int ballSpeedY = 5;

  private int paddleX = 250;

  private int paddleY = 480;

  private int paddleSpeed = 12;

  private int[][] bricks = new int[5][8];

  private int brickWidth = 70;

  private int brickHeight = 30;

  private int brickScore = 10;

  private int score = 0;

  private int lives = 3;

  public BrickBreaker() {

    super();

    addKeyListener(this);

    setFocusable(true);

    setFocusTraversalKeysEnabled(false);

    resetBricks();

  }

  public void resetBricks() {

    for (int i=0; i<5; i++) {

      for (int j=0; j<8; j++) {

        bricks[i][j] = 1;

      }

    }

  }

  public void gameOver() {

    System.out.println("Game Over");

    System.exit(0);

  }

  public void checkForCollisions() {

    if (ballX < 0 || ballX > getWidth())

      ballSpeedX = -ballSpeedX;

    if (ballY < 0)

      ballSpeedY = -ballSpeedY;

    if (ballY > getHeight()) {

      lives--;

      if (lives == 0) {

        gameOver();

      }

      ballX = 300;

      ballY = 400;

      return;

    }

    if (ballX >= paddleX && ballX <= paddleX + 70 && ballY >= paddleY && ballY <= paddleY + 10)

      ballSpeedY = -ballSpeedY;

    for (int i=0; i<5; i++) {

      for (int j=0; j<8; j++) {

        if (bricks[i][j] == 1 && ballX >= j * brickWidth && ballX <= j * brickWidth + brickWidth && ballY >= i * brickHeight && ballY <= i * brickHeight + brickHeight) {

          bricks[i][j] = 0;

          score += brickScore;

          if (score == 400) {

            System.out.println("You win!");

            System.exit(0);

          }

          if (ballX < j * brickWidth || ballX > j * brickWidth + brickWidth)

            ballSpeedX = -ballSpeedX;

           else

            ballSpeedY = -ballSpeedY;

        }

      }

    }

  }

  public void moveBall() {

    ballX += ballSpeedX;

    ballY += ballSpeedY;

    checkForCollisions();

  }

  public void movePaddleLeft() {

    paddleX -= paddleSpeed;

    if (paddleX < 0)

      paddleX = 0;

  }

  public void movePaddleRight() {

    paddleX += paddleSpeed;

    if (paddleX > getWidth() - 70) {

      paddleX = getWidth() - 70;

    }

  }

  public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {

      movePaddleLeft();

    } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

      movePaddleRight();

    }

  }

  public void keyReleased(KeyEvent e) {}

  public void keyTyped(KeyEvent e) {}

  public void paintComponent(Graphics g) {

    super.paintComponent(g);

    g.setColor(Color.BLACK);

    g.fillRect(0, 0, getWidth(), getHeight());

    g.setColor(Color.WHITE);

    g.drawString("Score: " + score, 10, 20);

    g.drawString("Lives: " + lives, getWidth() - 60, 20);

    for (int i=0; i<5; i++) {

      for (int j=0; j<8; j++) {

        if (bricks[i][j] == 1) {

          g.fillRect(j * brickWidth, i * brickHeight + 30, brickWidth, brickHeight);

        }

      }

    }

    g.setColor(Color.BLUE);

    g.fillOval(ballX, ballY, 10, 10);

    g.setColor(Color.RED);

    g.fillRect(paddleX, paddleY, 70, 10);

  }

  public static void main(String[] args) {

    JFrame frame = new JFrame("Brick Breaker");

    BrickBreaker app = new BrickBreaker();

    frame.setSize(600, 600);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    frame.add(app);

    while (true) {

      try {

        Thread.sleep(25);

      } catch (InterruptedException e) {}

      app.moveBall();

      app.repaint();

    }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复