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