21xrx.com
2024-09-19 09:43:28 Thursday
登录
文章检索 我的文章 写文章
Java编程-打砖块游戏代码
2023-06-18 02:07:40 深夜i     --     --
Java编程 游戏开发 打砖块游戏

Java是一门非常流行的编程语言,在游戏开发领域也有着广泛的应用。在本文中,我们将教大家如何使用Java编写一个简单的打砖块游戏。

在开始编写代码之前,我们需要了解一些游戏开发所需的基础知识。其中包括画布的创建和使用、键盘和鼠标的监听、游戏物体的运动等。

在本游戏中,我们需要创建一个画布,并在画布上绘制出砖块、球和挡板。然后,我们需要监听键盘事件和鼠标事件,来实现游戏中球和挡板的运动。当球碰到砖块时,砖块会消失,分数会增加。

下面是一个简单的Java代码示例,您可以通过学习和改进此代码来完成自己的打砖块游戏:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class BrickBreaker extends JFrame implements ActionListener, KeyListener, MouseListener {

  private int width = 640;

  private int height = 480;

  private int score = 0;

  private Timer timer;

  private Ball ball;

  private Paddle paddle;

  private Brick[] bricks;

  public BrickBreaker() {

    setSize(width, height);

    setResizable(false);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setVisible(true);

    getContentPane().setBackground(Color.BLACK);

    setLayout(null);

    ball = new Ball(320, 240);

    paddle = new Paddle(270, 400);

    bricks = new Brick[20];

    for (int i = 0; i < bricks.length; i++) {

      bricks[i] = new Brick((i%5)*80+80, (i/5)*40+60);

    }

    addKeyListener(this);

    addMouseListener(this);

    timer = new Timer(10, this);

    timer.setCoalesce(true);

    timer.start();

  }

  public void paint(Graphics g) {

    super.paint(g);

    g.setColor(Color.WHITE);

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

    ball.paint(g);

    paddle.paint(g);

    for (Brick brick : bricks) {

      brick.paint(g);

    }

  }

  public void actionPerformed(ActionEvent e) {

    ball.move();

    if (ball.intersects(paddle)) {

      ball.bounceOffPaddle(paddle);

    }

    for (Brick brick : bricks) {

      if (ball.intersects(brick)) {

        ball.bounceOffBrick(brick);

        score += 1;

        brick.hit();

      }

    }

    if (ball.getY() > 480) {

      timer.stop();

      JOptionPane.showMessageDialog(this, "You Lose! Score: " + score);

      System.exit(0);

    }

    repaint();

  }

  public void keyPressed(KeyEvent e) {

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

      paddle.moveLeft();

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

      paddle.moveRight();

    }

  }

  public void keyReleased(KeyEvent e) {}

  public void keyTyped(KeyEvent e) {}

  public void mouseClicked(MouseEvent e) {

    int mouseX = e.getX();

    int mouseY = e.getY();

    if (e.getButton() == MouseEvent.BUTTON1) {

      paddle.move(mouseX);

    }

  }

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

  public void mousePressed(MouseEvent e) {}

  public void mouseReleased(MouseEvent e) {}

  public static void main(String[] args) {

    new BrickBreaker();

  }

}

class Ball {

  private int x;

  private int y;

  private int dx;

  private int dy;

  private int radius = 10;

  public Ball(int x, int y)

    this.x = x;

    this.y = y;

    dx = 0;

    dy = 5;

  

  public void paint(Graphics g) {

    g.setColor(Color.WHITE);

    g.fillOval(x-radius, y-radius, 2*radius, 2*radius);

  }

  public void move() {

    x += dx;

    y += dy;

    if (x-radius < 0 || x+radius > 640)

      dx = -dx;

    

    if (y-radius < 0)

      dy = -dy;

    

  }

  public void bounceOffPaddle(Paddle paddle) {

    dy = -dy;

    dx = dx + paddle.getVelocity();

  }

  public void bounceOffBrick(Brick brick)

    dy = -dy;

  

  public boolean intersects(Rectangle rect) {

    return rect.intersects(new Rectangle(x-radius, y-radius, 2*radius, 2*radius));

  }

  public int getY()

    return y;

  

}

class Paddle {

  private int x;

  private int y;

  private int width = 100;

  private int height = 10;

  private int velocity = 0;

  public Paddle(int x, int y)

    this.x = x;

    this.y = y;

  

  public void paint(Graphics g) {

    g.setColor(Color.WHITE);

    g.fillRect(x, y, width, height);

  }

  public void moveLeft()

    velocity = -5;

  

  public void moveRight()

    velocity = 5;

  

  public void move(int x)

    this.x = x - width/2;

  

  public void stop()

    velocity = 0;

  

  public int getVelocity()

    return velocity;

  

}

class Brick {

  private int x;

  private int y;

  private int width = 80;

  private int height = 40;

  private boolean destroyed = false;

  public Brick(int x, int y)

    this.x = x;

    this.y = y;

  

  public void paint(Graphics g) {

    if (!destroyed) {

      g.setColor(Color.RED);

      g.fillRect(x, y, width, height);

    }

  }

  public void hit()

    destroyed = true;

  

  public boolean isDestroyed()

    return destroyed;

  

  public boolean intersects(Rectangle rect) {

    return rect.intersects(new Rectangle(x, y, width, height));

  }

}

  
  

评论区

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