仿弹壳特工队,绝地反击活动使用电池翻格子小游戏(JAVA小游戏)

近来太无聊,玩了一款割草游戏,里面有个活动感觉挺好玩的,像扫雷一样,寻找线索(灯泡),在这里使用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);
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邓霖涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值