import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
public class Game2048 extends JFrame {
// 移动方向
final public static int MOVE_UP = 0xf37;
final public static int MOVE_DOWN = 0xf36;
final public static int MOVE_LEFT = 0xf35;
final public static int MOVE_RIGHT = 0xf34;
// 游戏状态
final public static int GAME_OVER = 0xf33;
final public static int GAME_CONTINUE = 0xf32;
final public static int GAME_WIN = 0xf31;
// 按钮事件
final public static int BUTTON_NEW_GAME = 0xf30;
final public static int BUTTON_ABOUT = 0xf28;
final public static int BUTTON_EXIT = 0xf27;
/**
* 行
*/
private int column;
/**
* 列
*/
private int row;
/**
* 游戏状态
*/
private int gameState;
/**
* 网格集
*/
private HashMap<Point, Cube> viewList = new HashMap<>();
/**
* 计分板
*/
private JMenuItem scoreBoard;
/**
* 计步器
*/
private JMenuItem arithmometer;
/**
* 计步
*/
private int count;
/**
* 新游戏加流程
*/
private int gameLv;
/**
* main函数
*
* @param args
*/
public static void main(String[] args) {
Game2048 game = new Game2048(400, 500);
game.setTitle("2048");
game.setLocationRelativeTo(null);
game.setVisible(true);
game.newGame();
}
/**
* 构造一个默认大小的界面
*/
public Game2048() {
this(400, 500);
}
/**
* 构造一个指定宽高的界面
*
* @param width
* 宽
* @param height
* 高
*/
public Game2048(int width, int height) {
column = width / 100;
row = height / 100;
this.setLayout(new GridLayout(row, column));
// 事件监听
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(width, height);
// 利用button 绘制网格
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
viewList.put(new Point(i, j), new Cube());
this.add(getView(i, j).getView());
}
}
// 设置按键监听
this.addKeyListener(new MyKeyListener(this));
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("游戏");
JMenuItem item1 = new JMenuItem("新游戏");
item1.addMouseListener(new MyMouseListener(this,
Game2048.BUTTON_NEW_GAME));
JMenuItem item2 = new JMenuItem("退出");
item2.addMouseListener(new MyMouseListener(this, Game2048.BUTTON_EXIT));
jm.add(item1);
jm.add(item2);
JMenu jm2 = new JMenu("关于");
JMenuItem item3 = new JMenuItem("关于");
item3.addMouseListener(new MyMouseListener(this, Game2048.BUTTON_ABOUT));
jm2.add(item3);
scoreBoard = new JMenuItem();
arithmometer = new JMenuItem();
jmb.add(jm);
jmb.add(jm2);
jmb.add(scoreBoard);
jmb.add(arithmometer);
this.setJMenuBar(jmb);
}
/**
* 向上移动
*/
public void up() {
for (int x = 1; x < row; x++) {
for (int i = 0; i < column; i++) {
move(Game2048.MOVE_UP, x, i, true);
}
}
createCube();
for (int x = 1; x < row; x++) {
for (int i = 0; i < column; i++) {
move(Game2048.MOVE_UP, x, i, false);
}
}
addCount();
}
/**
* 向下移动
*/
public void down() {
for (int x = row - 2; x >= 0; x--) {
for (int y = 0; y < column; y++) {
move(Game2048.MOVE_DOWN, x, y, true);
}
}
createCube();
for (int x = row - 2; x >= 0; x--) {
for (int y = 0; y < column; y++) {
move(Game2048.MOVE_DOWN, x, y, false);
}
}
addCount();
}
/**
* 向左移动
*/
public void left() {
for (int y = 1; y < column; y++) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_LEFT, x, y, true);
}
}
createCube();
for (int y = 1; y < column; y++) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_LEFT, x, y, false);
}
}
addCount();
}
/**
* 向右移动
*/
public void right() {
for (int y = column - 2; y >= 0; y--) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_RIGHT, x, y, true);
}
}
createCube();
for (int y = column - 2; y >= 0; y--) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_RIGHT, x, y, false);
}
}
addCount();
}
/**
* 移动
*
* @param move_way
* 移动方向
* @param x
* 横坐标
* @param y
* 纵坐标
*/
private void move(int move_way, int x, int y, boolean isAdd) {
switch (move_way) {
case Game2048.MOVE_DOWN: {
for (; x < row - 1; x++) {
swap(getView(x + 1, y), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_LEFT: {
for (; y > 0; y--) {
swap(getView(x, y - 1), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_RIGHT: {
for (; y < column - 1; y++) {
swap(getView(x, y + 1), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_UP: {
for (; x > 0; x--) {
swap(getView(x - 1, y), getView(x, y), isAdd);
}
}
break;
}
}
/**
* 单向交换实现移动
*
* @param next
* 移动至目标位置
* @param now
* 需要移动的目标
* @param isAdd
* 是否是第一次移动
*/
private void swap(Cube next, Cube now, boolean isAdd) {
if (isAdd) {
if (now.getNum() != 0 && next.getNum() == 0) {
next.setText(now.getNum());
now.setText(0);
next.setIsAdded(now.isAdded());
now.setIsAdded(false);
} else if (!now.isAdded() && !next.isAdded()
&& next.getNum() == now.getNum() && now.getNum() != 0) {
next.setText(now.getNum() * 2);
now.setText(0);
next.setIsAdded(true);
now.setIsAdded(false);
}
} else {
if (next.getNum() == 0) {
next.setText(now.getNum());
now.setText(0);
}
now.setIsAdded(false);
next.setIsAdded(false);
}
}
/**
* 获取指定控件
*
* @param x
* @param y
* @return Cube
*/
private Cube getView(int x, int y) {
return viewList.get(new Point(x, y));
}
/**
* 生成随机控件 随机位置
*/
private void createCube() {
int x;
int y;
do {
x = (int) (Math.random() * 1000 % row);
y = (int) (Math.random() * 10

好运仔dzl

- 粉丝: 1w+
最新资源
- 中职物流服务与管理专业信息化教学现状及对策.docx
- PLC课程设计分析方案-全自动洗衣机.doc
- 信息化条件下初中英语掌握式教学法探究.docx
- 辽宁工业大学电气工程及其自动化专业.docx
- 网络媒介生态环境与和谐社会的构建.docx
- 大数据文秘人员提升信息素养的策略.docx
- (源码)基于Unity游戏引擎的飞机大战游戏.zip
- 区块链技术运作原理与前景解析.docx
- 当前我国电子商务发展的主要特点.docx
- 《网络营销》第1章-网络营销概论.ppt
- 大数据时代企业人力资源管理变革的探讨.docx
- 谈互联网技术背景下英语家庭作业途径的多样性.docx
- 论人工智能在电气设备中的应用及其前景.docx
- 单片机交通灯研究分析报告.doc
- 互联网背景下母亲教育的新特点与新策略摭探.docx
- 医院计算机网络运行维护探究.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


