扫雷游戏

这是一个自制的扫雷游戏,玩法与微软扫雷相同,但界面和功能还有待改进。虽然无法展示图片和音效,但代码已列出,包括entity模块的Boom、Entity、Number等类,ui模块的GameFrame、MainFrame等类,以及util模块的MusicPalyTool和Util工具包。
摘要由CSDN通过智能技术生成

做了一个像微软扫雷一样的游戏,基本玩法一致;
缺点:界面不太美观,功能也不是很强大
图片和音效都是没办法上传
话不多说,贴代码了

entity

Boom

package com.game.minesweeper.entity;

import com.game.minesweeper.util.Util;

public class Boom extends Entity {
   

    public Boom(int x, int y) {
        super(x, y, 28, 28, Util.getImage("img/boom.png"), 1);
    }

}

BoomList

package com.game.minesweeper.entity;

import java.util.Random;
import java.util.Vector;

public class BoomList extends Vector<Boom> {
   
    static BoomList list;
    // 保存炸弹位置地图
    public static boolean[][] map;

    public static BoomList getList(int x, int width, int hight) {
        if (list == null) {
            list = new BoomList();
            map = new boolean[width][hight];
            setBoom(x, width, hight);

        }
        return list;
    }

    // 随机设置count个地雷位置并加入list中
    public static void setBoom(int count, int width, int hight) {
        Random r = new Random();
        int c = 0;
        for (int i = 0; i < count; i++) {

            boolean flag = true;// 循环随机条件
            // 初次循环
            int x = r.nextInt(width);
            int y = r.nextInt(hight);
            if (list.size() != 0) {
                // 如果是空列表直接添加。不是空列表就找空白位置
                while (flag) {
                    // 重新随机位置
                    x = r.nextInt(width);
                    y = r.nextInt(hight);
                    // 判断是否与列表中某个重合
                    boolean flag1 = true;
                    for (int j = 0; j < list.size(); j++) {
                        if (x == list.get(j).x && y == list.get(j).y) {
                            flag1 = false;
                        }
                    }
                    if (flag1) {
                        flag = false;
                    }
                }
            }
            // 加入列表中
            list.add(new Boom(x, y));
            map[x][y] = true;

        }

    }

}

Entity

package com.game.minesweeper.entity;

import java.awt.Graphics;
import java.awt.Image;

public class Entity {
   
    // 坐标和高宽
    public int x;
    public int y;
    int width;
    int hight;
    Image img;
    // 为了不遮住线条,设置一个变量来改变位置
    int offset;

    public Entity(int x, int y, int width, int hight, Image img, int offset) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.hight = hight;
        this.img = img;
        this.offset = offset;
    }

    public void drawSelf(Graphics g) {
        g.drawImage(img, x * 30 + offset, y * 30 + offset, width, hight, null);
    }

}

Number

package com.game.minesweeper.entity;

import java.awt.Graphics;
import java.awt.Image;

public class Entity {
   
    // 坐标和高宽
    public int x;
    public int y;
    int width;
    int hight;
    Image img;
    // 为了不遮住线条,设置一个变量来改变位置
    int offset;

    public Entity(int x, int y, int width, int hight, Image img, int offset) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.hight = hight;
        this.img = img;
        this.offset = offset;
    }

    public void drawSelf(Graphics g) {
        g.drawImage(img, x * 30 + offset, y * 30 + offset, width, hight, null);
    }

}

NumList

package com.game.minesweeper.entity;

import java.util.ArrayList;

public class NumberList extends ArrayList<Number> {
   
    static NumberList list;
    BoomList blist;
    public static boolean[][] bMap;
    public static boolean[][] map;

    // 懒汉模式
    public static NumberList getList(int x, int width, int hight, BoomList blist) {
        if (list == null) {
            blist = BoomList.getList(x, width, hight);
            bMap = new boolean[width][hight];
            map = new boolean[width][hight];
            // 拿到炸弹图
            bMap = blist.map;
            list = new NumberList();
            // 表示数字
            setNumber(width, hight);
        }
        return list;
    }

    private static void setNumber(int width, int hight) {
        for (int i = 0; i < width; i++) {

            
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值