JAVA学习,写的一个点灯小游戏
程序员文章站
2022-04-09 10:48:17
...
之前写的一个点灯小游戏,没什么技术含量。
截图:
代码:
类Start:
import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.UIManager; public class Start { public static void main(String[] args) { // 使用Windows的界面风格 try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } new MainFrame().setVisible(true); } }
类MainFrame:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class MainFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final int PER_PIECE_SIZE = 50; private static final int GAMEAREA_X = 10, GAMEAREA_Y = 20; private static final int MAX_PIECES_DEGREE = (Toolkit.getDefaultToolkit().getScreenSize().height - 100) / PER_PIECE_SIZE; private GamePanel gamePanel; private JPanel configPanel; private JTextField sizeTF; private JButton startBtn; private JLabel stepLabel; private int light[][]; private int steps; private boolean isWin; private Random rand = new Random(System.currentTimeMillis()); public MainFrame() { setTitle("点灯游戏"); setSize(400, 300); //setLayout(null); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gamePanel = new GamePanel(); gamePanel.setBorder(BorderFactory.createTitledBorder("规则:将所有灯调至同种颜色")); gamePanel.addMouseListener(new MyMouseListener()); add(gamePanel, BorderLayout.CENTER); configPanel = new JPanel(); add(configPanel, BorderLayout.SOUTH); sizeTF = new JTextField("4"); sizeTF.setColumns(6); sizeTF.addKeyListener(new MyKeyListener()); configPanel.add(sizeTF); startBtn = new JButton("开始"); startBtn.addActionListener(this); configPanel.add(startBtn); stepLabel = new JLabel(); configPanel.add(stepLabel); start(); } public void init(int rows) { light = new int[rows][]; for (int i = 0; i < rows; i++) { light[i] = new int[rows]; } for (int i = 0; i < rows; i++) { int r = Math.abs(rand.nextInt() % (rows * rows)); light[r / rows][r % rows] = 1; } } public void start() { int rows = 0; try { rows = Integer.valueOf(sizeTF.getText().trim()); if (rows < 2) { JOptionPane.showMessageDialog(null, "大点行么?", "干", JOptionPane.INFORMATION_MESSAGE); sizeTF.setText(""); sizeTF.requestFocus(); return; } else if (rows > MAX_PIECES_DEGREE) { JOptionPane.showMessageDialog(null, "小点行么,你屏幕放的下?", "干", JOptionPane.INFORMATION_MESSAGE); sizeTF.setText(""); sizeTF.requestFocus(); return; } } catch (Exception e1) { JOptionPane.showMessageDialog(null, "不要乱输入,OK?", "无语", JOptionPane.ERROR_MESSAGE); sizeTF.setText(""); sizeTF.requestFocus(); return; } init(rows); int width = PER_PIECE_SIZE * rows + 20; int height = PER_PIECE_SIZE * rows + 30; gamePanel.setSize(width, height); gamePanel.repaint(); steps = 0; stepLabel.setText(""); setSize(width + 10, height + 70); repaint(); setLocationRelativeTo(null); isWin = false; } @Override public void actionPerformed(ActionEvent e) { start(); } public void click(int x, int y) { int rows = light.length; for (int i = x - 1; i <= x + 1; i++) { if (i < 0 || i > rows - 1 || i == x) continue; light[y][i] = light[y][i] == 0 ? 1 : 0; //System.out.println("m" + i + " " + y); } for (int j = y - 1; j <= y + 1; j++) { if (j < 0 || j > rows - 1 || j == y) continue; light[j][x] = light[j][x] == 0 ? 1 : 0; } light[y][x] = light[y][x] == 0 ? 1 : 0; } public boolean win() { if (isWin == true) return true; int first = light[0][0]; for (int i = 0; i < light.length; i++) { for (int j = 0; j < light[i].length; j++) { if (light[i][j] != first) return false; } } isWin = true; return true; } class MyKeyListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { start(); } } } class GamePanel extends JPanel { private static final long serialVersionUID = 1L; @Override public void paint(Graphics g) { super.paint(g); if (light == null) return; int rows = light.length; for (int i = 0; i < rows; i++) { for (int j = 0; j < rows; j++) { if (light[i][j] == 0) g.setColor(Color.BLACK); else g.setColor(Color.WHITE); g.fillRect(j * PER_PIECE_SIZE + GAMEAREA_X, i * PER_PIECE_SIZE + GAMEAREA_Y, PER_PIECE_SIZE - 1, PER_PIECE_SIZE - 1); } } g.setColor(Color.GRAY); int tmp; for (int i = 0; i <= rows; i++) { tmp = PER_PIECE_SIZE * i - 1; g.drawLine(GAMEAREA_X - 1, tmp + GAMEAREA_Y, PER_PIECE_SIZE * rows + GAMEAREA_X - 1, tmp + GAMEAREA_Y); g.drawLine(tmp + GAMEAREA_X, GAMEAREA_Y - 1, tmp + GAMEAREA_X, PER_PIECE_SIZE * rows + GAMEAREA_Y - 1); } g.setColor(Color.RED); if (isWin) { g.drawString("You Win! steps:" + steps, 20, 65); } } } class MyMouseListener extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { if (isWin || light == null) return; int rows = light.length; int x = e.getX() - 10; int y = e.getY() - 20; if (x < 0 || x >= PER_PIECE_SIZE * rows || y < 0 || y >= PER_PIECE_SIZE * rows) return; x /= PER_PIECE_SIZE; y /= PER_PIECE_SIZE; //System.out.println(x + " " +y); switch (e.getButton()) { case MouseEvent.BUTTON1: click(x, y); break; case MouseEvent.BUTTON2: return; case MouseEvent.BUTTON3: light[y][x] = light[y][x] == 0 ? 1 : 0; break; } win(); steps++; stepLabel.setText("步数:" + steps); gamePanel.repaint(); } } }
上一篇: android 开源的日期、时间组件