您的位置:首页 > 编程语言 > Java开发

java小游戏2048实现

2015-02-26 13:24 671 查看
上课的时候见一女童鞋在玩一个叫2048的游戏,其实我是觉得玩游戏挺无聊的,

上去对她说:“这有什么好玩的咯!,太幼稚了,我都能写一个”

谁知道她来一句:“好啊,你写一个我就玩你的不玩这个了!”


既然美女都开口了就不好意思不写啦!回去花了6个小时写了个PC版的2048

虽然不能再手机上玩呵呵但也让我嘚瑟了一下, 其实还蛮简单的。

就以个2维数组,4个方向进行加法 在空白位置出现随机的2就行了

算法哪里耽误了点时间,主要还是做界面麻烦点

分享一下代码!

主窗口

package jyy.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainFrame extends JFrame {
	public static JPanel viewJp;
	public static Graphics g;
	JPanel controlJP = new JPanel();
	public int[][] map =new int[4][4];
	public int SIZE = 100;
	public int posY = 0;
	public int posX = 0;

	public MainFrame() {
		this.setTitle("瑶哥版2048");
		this.setLayout(null);
		initControlJp();
		initViewJp();
		this.setSize(450, 520);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public void initViewJp() {

		viewJp = new JPanel() {
			@Override
			public void paint(Graphics g) {
				flash(g);
			}
		};

		viewJp.setBounds(0, 50, 480, 480);
		this.add(viewJp);
	}

	public void initControlJp() {
		
		JLabel jl=new JLabel("瑶哥版2048");
		jl.setFont(new Font("微软雅黑", Font.PLAIN, 26));
		jl.setBounds(160, 10, 300, 40);
		controlJP.setBounds(0, 0, 480, 50);
		controlJP.setLayout(null);
		controlJP.add(jl);
		this.add(controlJP);
	}

	/**
	 * 绘制Map中的内容
	 * 
	 * @param g
	 */
	public void flash(Graphics g) {
		g.setColor(new Color(250, 248, 239));
		g.fillRect(0, 0, 450, 450);
		g.setFont(new Font("微软雅黑", Font.BOLD, 25));
		for (int y = 0; y < map.length; y++) {
			for (int x = 0; x < map.length; x++) {

				if (map[y][x] == 0) {
					g.setColor(Color.white);
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);
				}
				else if (map[y][x] == 2) {
					g.setColor(new Color(220, 210, 199));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 4) {
					g.setColor(new Color(225, 229, 189));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 8) {
					g.setColor(new Color(225, 142, 109));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 16) {
					g.setColor(new Color(254, 112, 84));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 32) {
					g.setColor(new Color(244, 122, 94));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 64) {
					g.setColor(new Color(238, 91, 61));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 128) {
					g.setColor(new Color(238, 207, 107));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 256) {
					g.setColor(new Color(238, 205, 88));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 512) {
					g.setColor(new Color(239, 201, 68));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 1024) {
					g.setColor(new Color(238, 198, 50));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}
				else if (map[y][x] == 2048) {
					g.setColor(new Color(238, 196, 30));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);

				}else {
					g.setColor(new Color(60, 220, 0));
					g.fillRoundRect(x * SIZE + (x * 5) + 5, y * SIZE + (y * 5)
							+ 5, SIZE, SIZE, 10, 10);
				}

				g.setColor(Color.black);
				if (map[y][x] != 0) {

					g.drawString(map[y][x] + "", x * SIZE + (x * 5) + 25, y
							* SIZE + (y * 5) + 65);
				}
				System.out.print(map[y][x] + "\t");
			}
			System.out.println();
		}
		System.out.println("------------------------------------");
	}

	public void addListener(KeyListener listener) {
		this.addKeyListener(listener);
	}

}


控制器

package jyy.control;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import jyy.view.MainFrame;

public class GameControl implements KeyListener {
	Random r = new Random();
	MainFrame mf;
	int SCOPE = 4;
	final private int LEFT = 37;
	final private int UP = 38;
	final private int RIGHT = 39;
	final private int DOWN = 40;

	/**
	 * 开始一个新的游戏
	 */

	public void newGame() {
		mf = new MainFrame();
		flashMap(mf.map);
		mf.repaint();
		mf.addListener(this);
	}

	/**
	 * 在每一次完成按键事件后产生一个新的 2
	 * 
	 * @param map
	 */
	private void flashMap(int[][] map) {
		ArrayList<String> temp = new ArrayList<String>();
		for (int x = 0; x < map.length; x++) {
			for (int y = 0; y < map.length; y++) {
				if (map[x][y] == 0) {
					temp.add(x + "," + y);
				}
			}
		}
		// 当size不为0那么证明游戏还没有结束
		if (temp.size() != 0) {
			int index = r.nextInt(temp.size());
			int posX = Integer.parseInt(temp.get(index).split(",")[0]);
			int posY = Integer.parseInt(temp.get(index).split(",")[1]);
			map[posX][posY] = 2;
			mf.map = map;
		} else {
			System.out.println("游戏结束!");
			System.exit(1);
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * 移动数组
	 */
	public void keyPressed(KeyEvent e) {
		move(e.getKeyCode());
		flashMap(mf.map);
		mf.repaint();
	}

	private void move(int keyCode) {
		switch (keyCode) {
		case LEFT:
			moveLeft();
			break;
		case RIGHT:
			moveRight();
			break;
		case UP:
			moveUp();
			break;
		case DOWN:
			moveDown();
			break;
		}
	}

	private void moveDown() {
		System.out.println("开始向下移动");
		for (int x = 0; x < SCOPE; x++) {
			// 最下面一位不需要移动位置所以跳过
			for (int y =SCOPE-2; y >-1; y--) {
				// 如果本身是位置的值是0那么不需要移动
				if (mf.map[y][x] == 0) {
					continue;
				}
				int point = findUpDownPoint(x, y, DOWN);

				int temp = mf.map[y][x];
				// 如果返回的位置上的点是零或者是自己那么不进行加法运算
				if (mf.map[point][x] == 0 || point == y) {
					mf.map[y][x] = 0;
					mf.map[point][x] = temp;
					// 如果返回的位置不是自己而且不是零则需要进行加法运算
				} else {
					mf.map[y][x] = 0;
					mf.map[point][x] = temp * 2;
				}
			}
		}

	}

	private void moveUp() {
		System.out.println("开始向上移动");
		for (int x = 0; x < SCOPE; x++) {
			// 最上面一位不需要移动位置所以跳过
			for (int y = 1; y <SCOPE; y++) {
				// 如果本身是位置的值是0那么不需要移动
				if (mf.map[y][x] == 0) {
					continue;
				}
				int point = findUpDownPoint(x, y, UP);

				int temp = mf.map[y][x];
				// 如果返回的位置上的点是零或者是自己那么不进行加法运算
				if (mf.map[point][x] == 0 || point == y) {
					mf.map[y][x] = 0;
					mf.map[point][x] = temp;
					// 如果返回的位置不是自己而且不是零则需要进行加法运算
				} else {
					mf.map[y][x] = 0;
					mf.map[point][x] = temp * 2;
				}
			}
		}

	}

	private int findUpDownPoint(int x, int y, int direction) {
		Integer point = null;
		if (direction ==UP) {
			// 寻找能向上前进的最大距离
			for (int i = y - 1; i > -1; i--) {
				if (mf.map[i][x] == 0 || mf.map[i][x] == mf.map[y][x]) {
					point = i;
					continue;
				} else {
					if (point == null) {
						return y;
					} else {
						return point;
					}
				}
			}
		} else if (direction == DOWN) {
			// 寻找能向下边前进的最大距离
			for (int i = y + 1; i < SCOPE; i++) {
				if (mf.map[i][x] == 0 || mf.map[i][x] == mf.map[y][x]) {
					point = i;
					continue;
				} else {
					if (point == null) {
						return y;
					} else {
						return point;
					}
				}
			}
		}
		// 如果没有找到返回原位置
		if (point == null) {
			return y;
		} else {
			return point;
		}
	}

	private void moveRight() {
		System.out.println("开始向右移动");
		for (int y = 0; y < SCOPE; y++) {
			// 最后一位不需要移动位置所以跳过
			for (int x = SCOPE - 1; x > -1; x--) {
				// 如果本身是位置的值是0那么不需要移动
				if (mf.map[y][x] == 0) {
					continue;
				}
				int point = findRightLeftPoint(x, y, RIGHT);

				int temp = mf.map[y][x];
				// 如果返回的位置上的点是零或者是自己那么不进行加法运算
				if (mf.map[y][point] == 0 || point == x) {
					mf.map[y][x] = 0;
					mf.map[y][point] = temp;
					// 如果返回的位置不是自己而且不是零则需要进行加法运算
				} else {
					mf.map[y][x] = 0;
					mf.map[y][point] = temp * 2;
				}
			}
		}

	}

	private void moveLeft() {
		System.out.println("开始向左移动");
		for (int y = 0; y < SCOPE; y++) {
			// 第一位不需要移动位置所以直接从第二位开始判断
			for (int x = 1; x < SCOPE; x++) {
				// 如果本身是位置的值是0那么不需要移动
				if (mf.map[y][x] == 0) {
					continue;
				}
				int point = findRightLeftPoint(x, y, LEFT);
				int temp = mf.map[y][x];

				// 如果返回的位置上的点是零或者是自己那么不进行加法运算
				if (mf.map[y][point] == 0 || point == x) {
					mf.map[y][x] = 0;
					mf.map[y][point] = temp;
					// 如果返回的位置不是自己而且不是零则需要进行加法运算
				} else {
					mf.map[y][x] = 0;
					mf.map[y][point] = temp * 2;
				}
			}
		}
	}

	private int findRightLeftPoint(int x, int y, int direction) {
		Integer point = null;
		if (direction == LEFT) {
			// 寻找能向左边前进的最大距离
			for (int i = x - 1; i > -1; i--) {
				if (mf.map[y][i] == 0 || mf.map[y][i] == mf.map[y][x]) {
					point = i;
					continue;
				} else {
					if (point == null) {
						return x;
					} else {
						return point;
					}
				}
			}
		} else if (direction == RIGHT) {
			// 寻找能向右边前进的最大距离
			for (int i = x + 1; i < SCOPE; i++) {
				if (mf.map[y][i] == 0 || mf.map[y][i] == mf.map[y][x]) {
					point = i;
					continue;
				} else {
					if (point == null) {
						return x;
					} else {
						return point;
					}
				}
			}
		}
		// 如果没有找到返回原位置
		if (point == null) {
			return x;
		} else {
			return point;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: