近来太无聊,玩了一款割草游戏,里面有个活动感觉挺好玩的,像扫雷一样,寻找线索(灯泡),在这里使用JAVA语言也简单实现下游戏。
先上效果图,鼠标点击对应的块,可以展开相连的方块,点击黄色的正方块(灯泡线索)那么就刷新进入下一关。
附上代码(代码注释都有,很详细了,就不耽误大家玩游戏啦)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
/**
* 模仿弹壳特工队,绝地反击活动使用电池翻格子 小游戏
*/
public class MapGame {
public static class MapGameView extends JPanel {
private static final int WIDTH = 40;
private MapGame mapGame;
public MapGameView(MapGame mapGame) {
super();
this.mapGame = mapGame;
setPreferredSize(new Dimension(450, 330));
addMouseListener(new MouseAdapter() {
@Override
public synchronized void mouseClicked(MouseEvent e) {
int x = e.getX()/WIDTH;
int y = e.getY()/WIDTH;
mapGame.openBlock(y, x);
int sum=0;
for (Integer val : mapGame.score.values()) {
sum = sum + val;
}
System.out.println(sum+"得分:"+mapGame.score);
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 64; i++) {
int x = i % 8;
int y = i / 8;
Block[][] map = mapGame.map;
Block block = map[x][y];
final Color color = g.getColor();
if(block==null) {
//已经没有了
g.setColor(getBackground());
g.fill3DRect(x * WIDTH, y * WIDTH, WIDTH, WIDTH, false);
g.setColor(color);
continue;
}
if (!block.open) {
g.fill3DRect(x * WIDTH, y * WIDTH, WIDTH, WIDTH, true);
continue;
}
// 0-红色;1-蓝色;2-紫色;3-绿色;4-灯泡;
switch (block.value) {
case 0:
g.setColor(Color.RED);
g.fillOval(x * WIDTH, y * WIDTH, WIDTH, WIDTH);
break;
case 1:
g.setColor(Color.BLUE);
g.fillOval(x * WIDTH, y * WIDTH, WIDTH, WIDTH);
break;
case 2:
g.setColor(Color.CYAN);
g.fillOval(x * WIDTH, y * WIDTH, WIDTH, WIDTH);
break;
case 3:
g.setColor(Color.GREEN);
g.fillOval(x * WIDTH, y * WIDTH, WIDTH, WIDTH);
break;
case 4:
g.setColor(Color.ORANGE);
g.fill3DRect(x * WIDTH, y * WIDTH, WIDTH, WIDTH, true);
break;
default:
break;
}
g.setColor(color);
}
}
}
public static void main(String[] args) {
MapGame mapGame = new MapGame();
JFrame frame = new JFrame();
frame.setTitle("模仿弹壳特工队,绝地反击活动使用电池翻格子");
frame.getContentPane().add(new MapGameView(mapGame));
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
/*Scanner scanner = new Scanner(System.in);
while (true) {
mapGame.printMapGame();
String next = scanner.next();
String[] split = next.split("\\.");
int x = Integer.valueOf(split[0]);
int y = Integer.valueOf(split[1]);
mapGame.openBlock(x, y);
}*/
}
/**
* 每个小格子
*/
class Block {
/**
* 默认没有开启的
*/
boolean open;
/**
* 块内部打开后的东西, 0-红色;1-蓝色;2-紫色;3-绿色;4-灯泡过关;
*/
int value = (int) (Math.random() * 10 % 4);
@Override
public String toString() {
if (open) {
return "" + value;
} else {
return "█";
}
}
}
/**
* 得分
*/
private Map<Integer, Integer> score = new LinkedHashMap<Integer, Integer>(){
{
this.put(0, 0);
this.put(1, 0);
this.put(2, 0);
this.put(3, 0);
this.put(4, 0);
}
};
private Block[][] map = new Block[8][8];
public MapGame() {
init();
}
/**
* 初始化地图
*/
public void init() {
for (int i = 0; i < 64; i++) {
int x = i % 8;
int y = i / 8;
map[x][y] = new Block();
}
int xiansuoIndex = (int) (Math.random() * 100 % 64);
// 线索灯泡位置
map[xiansuoIndex % 8][xiansuoIndex / 8].value = 4;
while (true) {
int startIndex = (int) (Math.random() * 100 % 64);
// 开始的位置不能与过关的相同
if (startIndex != xiansuoIndex) {
map[startIndex % 8][startIndex / 8].open = true;
break;
}
}
}
/**
* 打开格子
*/
public void openBlock(int y, int x) {
if (map[x][y]==null || !map[x][y].open) {
// 当前的点击的必须是已经打开的
return;
}
// 相对应的得分增加
Integer valueSize = score.get(map[x][y].value);
if (valueSize == null) {
valueSize = 0;
}
score.put(map[x][y].value, valueSize + 1);
if (map[x][y].value == 4) {
// 如果是过关的灯泡,那么下一关,重新初始化格子
this.init();
return;
}
map[x][y] = null;
// 相连的格子都打开,下面代码就偷懒了,不判断边界,边缘使用异常捕捉了,
try {
map[x - 1][y + 1].open = true;
} catch (Exception e) {
}
try {
map[x - 1][y - 1].open = true;
} catch (Exception e) {
}
try {
map[x - 1][y].open = true;
} catch (Exception e) {
}
try {
map[x + 1][y - 1].open = true;
} catch (Exception e) {
}
try {
map[x][y - 1].open = true;
} catch (Exception e) {
}
try {
map[x][y + 1].open = true;
} catch (Exception e) {
}
try {
map[x + 1][y].open = true;
} catch (Exception e) {
}
try {
map[x + 1][y + 1].open = true;
} catch (Exception e) {
}
}
/**
* 打印输出当前地图游戏信息
*/
public void printMapGame() {
System.out.println("玩家得分:" + score.toString());
for (int i = 0; i < 8; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.println();
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map[y].length; x++) {
Block block = map[y][x];
System.out.print(block + " ");
}
System.out.println(" " + y);
}
}
}