Java实现扫雷小游戏优化版

Posted orangemilk_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现扫雷小游戏优化版相关的知识,希望对你有一定的参考价值。

🍁🍁大家好!上一期我们使用GUI技术写了一个简单的扫雷小游戏,今天对这个Java应用程序更新迭代,增加了难度选择等功能,修复了已知的几个问题。成为初学者学习的好项目!Java实现扫雷小游戏【完整版】


🌕文章目录


🍊效果展示

🍁难度选择展示

🍁游戏界面展示


🍊代码展示

🍁主类:GameWin类

//主类
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();
    GameSelect gameSelect = new GameSelect();
    //是否开始,false未开始,true开始
    boolean begin=false;

    void launch()
        GameUtil.START_TIME=System.currentTimeMillis();
        this.setVisible(true);
        if(GameUtil.state==3)
            this.setSize(500,500);
        else 
            this.setSize(width,height);
        
        this.setLocationRelativeTo(null);
        this.setTitle("Java扫雷小游戏");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //鼠标事件
        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;
                        
                    case 1 :
                    case 2 :
                        if(e.getButton()==1)
                            if(e.getX()>GameUtil.OFFSET + GameUtil.SQUARE_LENGTH*(GameUtil.MAP_W/2)
                                    && e.getX()<GameUtil.OFFSET + GameUtil.SQUARE_LENGTH*(GameUtil.MAP_W/2) + GameUtil.SQUARE_LENGTH
                                    && e.getY()>GameUtil.OFFSET
                                    && e.getY()<GameUtil.OFFSET+GameUtil.SQUARE_LENGTH)
                                mapBottom.reGame();
                                mapTop.reGame();
                                GameUtil.FLAG_NUM=0;
                                GameUtil.START_TIME=System.currentTimeMillis();
                                GameUtil.state=0;
                            
                        
                        if(e.getButton()==2)
                            GameUtil.state=3;
                            begin=true;
                        
                        break;
                    case 3:
                        if(e.getButton()==1)
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            begin = gameSelect.hard();
                        
                        break;
                    default:
                
            
        );
        
        while (true)
            repaint();
            begin();
            try 
                Thread.sleep(40);
             catch (InterruptedException e) 
                e.printStackTrace();
            
        
    
    void begin()
        if(begin)
            begin=false;
            gameSelect.hard(GameUtil.level);
            dispose();
            GameWin gameWin = new GameWin();
            GameUtil.START_TIME = System.currentTimeMillis();
            GameUtil.FLAG_NUM=0;
            mapBottom.reGame();
            mapTop.reGame();
            gameWin.launch();
        
    

    @Override
    public void paint(Graphics g) 
        if(GameUtil.state==3)
            g.setColor(Color.lightGray);
            g.fillRect(0,0,500,500);
            gameSelect.paintSelf(g);
        else 
            offScreenImage = this.createImage(width, height);
            Graphics gImage = offScreenImage.getGraphics();
            //设置背景颜色
            gImage.setColor(Color.lightGray);
            gImage.fillRect(0, 0, width, height);

            mapBottom.paintSelf(gImage);
            mapTop.paintSelf(gImage);
            g.drawImage(offScreenImage, 0, 0, null);
        
    

    public static void main(String[] args) 
        GameWin gameWin = new GameWin();
        gameWin.launch();
    

🍁底层地图MapBottom类

//底层地图类
package com.sxt;
import java.awt.*;

public class MapBottom 
    BottomRay bottomRay = new BottomRay();
    BottomNum bottomNum = new BottomNum();
    
        bottomRay.newRay();
        bottomNum.newNum();
    

    //重置游戏
    void reGame()
        for (int i = 1; i <=GameUtil.MAP_W ; i++) 
            for (int j = 1; j <=GameUtil.MAP_H ; j++) 
                GameUtil.DATA_BOTTOM[i][j]=0;
            
        
        bottomRay.newRay();
        bottomNum.newNum();
    
    //绘制方法
    void paintSelf(Graphics g)
        g.setColor(Color.red);
        //画竖线
        for (int i = 0; i <= GameUtil.MAP_W; i++) 
            g.drawLine(GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET,
                    GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH);
        
        //画横线
        for (int i = 0; i <=GameUtil.MAP_H; i++)
            g.drawLine(GameUtil.OFFSET,
                    3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,
                    GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH);
        
        for (int i = 1; i <= GameUtil.MAP_W ; i++) 
            for (int j = 1; j <= GameUtil.MAP_H; j++) 
                //雷
                if (GameUtil.DATA_BOTTOM[i][j] == -1) 
                    g.drawImage(GameUtil.lei,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                
                //数字
                if (GameUtil.DATA_BOTTOM[i][j] >=0) 
                    g.drawImage(GameUtil.images[GameUtil.DATA_BOTTOM[i][j]],
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 15,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 5,
                            null);
                
            
        

        //绘制数字 剩余雷数,倒计时
        GameUtil.drawWord(g,""+(GameUtil.RAY_MAX-GameUtil.FLAG_NUM),
                GameUtil.OFFSET,
                2*GameUtil.OFFSET,30,Color.red);
        GameUtil.drawWord(g,""+(GameUtil.END_TIME-GameUtil.START_TIME)/1000,
                GameUtil.OFFSET + GameUtil.SQUARE_LENGTH*(GameUtil.MAP_W-1),
                2*GameUtil.OFFSET,30,Color.red);
        switch (GameUtil.state)
            case 0:
                GameUtil.END_TIME=System.currentTimeMillis();
                g.drawImage(GameUtil.face,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.MAP_W/2),
                        GameUtil.OFFSET,
                        null);
                break;
            case 1:
                g.drawImage(GameUtil.win,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.MAP_W/2),
                        GameUtil.OFFSET,
                        null);
                break;
            case 2:
                g.drawImage(GameUtil.over,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.MAP_W/2),
                        GameUtil.OFFSET,
                        null);
                break;

            default:
        
    

🍁顶层地图MapTop类

//顶层地图类
package com.sxt;
import java.awt.*;

public class MapTop 
    //格子位置
    int temp_x;
    int temp_y;

    //重置游戏
    void reGame()
        for (int i = 1; i <=GameUtil.MAP_W ; i++) 
            for (int j = 1; j <=GameUtil.MAP_H ; j++) 
                GameUtil.DATA_TOP[i][j]=0;
            
        
    

    //判断逻辑
    void logic()

        temp_x=0;
        temp_y=0;
        if(GameUtil.MOUSE_X>GameUtil.OFFSET && GameUtil.MOUSE_Y>3*GameUtil.OFFSET)
            temp_x = (GameUtil.MOUSE_X - GameUtil.OFFSET)/GameUtil.SQUARE_LENGTH+1;
            temp_y = (GameUtil.MOUSE_Y - GameUtil.OFFSET * 3)/GameUtil.SQUARE_LENGTH+1;
        

        if(temp_x>=1 && temp_x<=GameUtil.MAP_W
                && temp_y>=1 && temp_y<=GameUtil.MAP_H)
            if(GameUtil.LEFT)
                //覆盖,则翻开
                if(GameUtil.DATA_TOP[temp_x][temp_y]==0)
                    GameUtil.DATA_TOP[temp_x][temp_y]=-1;
                
                spaceOpen(temp_x,temp_y);
                GameUtil.LEFT=false;
            
            if(GameUtil.RIGHT)
                //覆盖则插旗
                if(GameUtil.DATA_TOP[temp_x][temp_y]==0)
                    GameUtil.DATA_TOP[temp_x][temp_y]=1;
                    GameUtil.FLAG_NUM以上是关于Java实现扫雷小游戏优化版的主要内容,如果未能解决你的问题,请参考以下文章

扫雷游戏(C语言实现)初级版和优化版(增加了自动展开标记地雷功能,同时排除了第一次排到地雷的情况)

2015扫雷游戏

洛谷 P2670 扫雷游戏

洛谷P2670 扫雷游戏

1-8-14:扫雷游戏地雷数计算

Noip(pj)2015 t2 扫雷游戏