Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java to realize Minesweeper Mini Game

Shulou Source: shulou.com Published: 2022-05-31 17:44:38 10月02日 Update

This article mainly introduces the relevant knowledge of "how to use Java to achieve minesweeper Mini Game". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java to achieve minesweeper Mini Game" can help you solve the problem.

Effect display

Main class: GameWin class package com.sxt;import javax.swing.*;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class GameWin extends JFrame {int width = 2 * GameUtil.OFFSET + GameUtil.MAP_W * GameUtil.SQUARE_LENGTH; int height = 4 * GameUtil.OFFSET + GameUtil.MAP_H * GameUtil.SQUARE_LENGTH; Image offScreenImage = null; MapBottom mapBottom = new MapBottom (); MapTop mapTop = new MapTop () Void launch () {GameUtil.START_TIME=System.currentTimeMillis (); this.setVisible (true); this.setSize (width,height); this.setLocationRelativeTo (null); this.setTitle ("Java Minesweeping Mini Game"); this.setDefaultCloseOperation (EXIT_ON_CLOSE) / / Mouse event this.addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {super.mouseClicked (e)) Switch (GameUtil.state) {case 0: if (e.getButton () = = 1) {GameUtil.MOUSE_X = e.getX (); GameUtil.MOUSE_Y = e.getY (); GameUtil.LEFT = true } if (e.getButton () = = 3) {GameUtil.MOUSE_X = e.getX (); GameUtil.MOUSE_Y = e.getY (); GameUtil.RIGHT = true } / / remove break Listen for mouse events case 1: case 2: if (e.getButton () = = 1) {if (e.getX ()) > GameUtil.OFFSET + GameUtil.SQUARE_LENGTH* (GameUtil.MAP_W/2) & & e at any time .getX () GameUtil.OFFSET & & e.getY ()

Tags: Bottom layer jackers maps top level minesweeping Mini Game mines coordinates numbers methods programs tools problems mice programming victories events errors squares users Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Linux vpn Huawei