21xrx.com
2024-11-08 23:16:11 Friday
登录
文章检索 我的文章 写文章
Java代码:打砖块小游戏
2023-06-14 15:20:22 深夜i     --     --
Java GUI编程 小游戏

最近本人在学习Java GUI编程,为了巩固所学,就尝试编写了一个简单的打砖块小游戏,代码如下:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Timer;

import java.util.TimerTask;

public class BreakBricksGame extends JFrame implements ActionListener, KeyListener {

  private MyCanvas myCanvas; 

  private Timer timer;

  private TimerTask task;

  private int ballX = 150;  

  private int ballY = 200;  

  private int ballD = 20;  

  private int ballVx = 5; 

  private int ballVy = -5;  

  private int paddleX = 150;  

  private int paddleY = 400;  

  private int paddleWidth = 60;  

  private int paddleHeight = 10;  

  private boolean isRunning = false;

  public BreakBricksGame() {

    super("打砖块小游戏");

    myCanvas = new MyCanvas();

    add(myCanvas, BorderLayout.CENTER);

    JPanel panel = new JPanel();

    JButton buttonStart = new JButton("开始");

    JButton buttonPause = new JButton("暂停");

    JButton buttonQuit = new JButton("退出");

    buttonStart.addActionListener(this);

    buttonPause.addActionListener(this);

    buttonQuit.addActionListener(this);

    panel.add(buttonStart);

    panel.add(buttonPause);

    panel.add(buttonQuit);

    add(panel, BorderLayout.SOUTH);

    addKeyListener(this);

    setFocusable(true);

    setSize(400, 500);

    setLocationRelativeTo(null);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);

  }

  public void actionPerformed(ActionEvent e) {

    String command = e.getActionCommand();

    if (command.equals("开始")) {

      if (!isRunning) {

        timer = new Timer();

        task = new TimerTask() {

          public void run() {

            ballX += ballVx;

            ballY += ballVy;

            if (ballX < 0 || ballX + ballD > myCanvas.getWidth())

              ballVx = -ballVx;

            

            if (ballY < 0 || ballY + ballD > myCanvas.getHeight())

              ballVy = -ballVy;

            

            if (ballY + ballD > paddleY &&

                ballX + ballD > paddleX &&

                ballX < paddleX + paddleWidth)

              ballVy = -ballVy;

            

            myCanvas.repaint();

          }

        };

        timer.schedule(task, 0, 20);

        isRunning = true;

      }

    } else if (command.equals("暂停")) {

      if (isRunning) {

        task.cancel();

        isRunning = false;

      }

    } else if (command.equals("退出")) {

      System.exit(0);

    }

  }

  public void keyPressed(KeyEvent e) {

    int keyCode = e.getKeyCode();

    if (keyCode == KeyEvent.VK_LEFT) {

      if (paddleX > 0)

        paddleX -= 20;

      

    } else if (keyCode == KeyEvent.VK_RIGHT) {

      if (paddleX + paddleWidth < myCanvas.getWidth()) {

        paddleX += 20;

      }

    }

    myCanvas.repaint();

  }

  public void keyReleased(KeyEvent e)

  

  public void keyTyped(KeyEvent e)

  

  class MyCanvas extends Canvas {

    public void paint(Graphics g) {

      g.setColor(Color.BLUE);

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

      g.setColor(Color.RED);

      g.fillRect(paddleX, paddleY, paddleWidth, paddleHeight);

    }

  }

  public static void main(String[] args) {

    new BreakBricksGame();

  }

}

运行上面的代码后,可以看到一个简单的打砖块小游戏界面,下面是关于本游戏的一些简单介绍和操作方法:

- 程序启动后,按“开始”按钮会启动小球运动的定时器,小球会自动来回移动,如果碰到左右边界或者下边界会反弹;

- 如果小球与挡板相撞,小球也会反弹;

- 挡板可以使用方向键左右移动,碰到左右边界会停止移动;

- 如果按下“暂停”按钮会暂停定时器,小球和挡板都会停止运动;

- 如果按下“退出”按钮会退出游戏程序。

  
  

评论区

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