import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/**
* 2048-java版
* @author Wu
*
*/
public class Game2048 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel scoresPan;
private JPanel mainPan;
private JPanel tipsPan;
private JLabel MaxScoresLabel;
private JLabel ScoresLabel;
private JLabel tips;
private JTextField MaxScoresText;
private JLabel ScoresText;
private JLabel[][] nums;
private Icon icon2;
private int times = 16; // 记录剩余空方块数目
private int scores = 0; // 记录分数
private int l1, l2, l3, l4;
Font font = new Font("", Font.BOLD, 14);
Font font2 = new Font("", Font.BOLD, 30);
Random random = new Random();
public static void main(String[] args) {
Game2048 w = new Game2048();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setBackground(Color.WHITE);
w.setVisible(true);
}
public Game2048() {
super();
this.setResizable(false);
this.getContentPane().setLayout(null);
this.setTitle("2048");
this.setSize(410, 550);
scoresPan = new JPanel();
scoresPan.setBounds(10, 10, 370, 50);
this.getContentPane().add(scoresPan);
scoresPan.setLayout(null);
MaxScoresLabel = new JLabel("最高分:");
MaxScoresLabel.setFont(font);
MaxScoresLabel.setBounds(30, 10, 60, 30);
scoresPan.add(MaxScoresLabel);
MaxScoresText = new JTextField("暂不可用");
MaxScoresText.setFont(font);
MaxScoresText.setEditable(false);
MaxScoresText.setBounds(90, 10, 90, 30);
scoresPan.add(MaxScoresText);
ScoresLabel = new JLabel("分 数:");
ScoresLabel.setFont(font);
ScoresLabel.setBounds(250, 10, 60, 30);
scoresPan.add(ScoresLabel);
ScoresText = new JLabel("0");
ScoresText.setFont(font);
ScoresText.setBounds(310, 10, 90, 30);
scoresPan.add(ScoresText);
mainPan = new JPanel();
mainPan.setBounds(10, 70, 380, 380);
mainPan.setBackground(Color.GRAY);
this.getContentPane().add(mainPan);
mainPan.setLayout(null);
nums = new JLabel[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nums[i][j] = new JLabel();
nums[i][j].setFont(font2);
nums[i][j].setHorizontalAlignment(SwingConstants.CENTER);
nums[i][j].setText("");
nums[i][j].setBounds(94 * j + 4, 94 * i + 4, 90, 90);
nums[i][j].setOpaque(true);
setColor(i, j, "");
mainPan.add(nums[i][j]);
}
}
tipsPan = new JPanel();
tipsPan.setBounds(30, 460, 300, 40);
this.getContentPane().add(tipsPan);
tipsPan.setLayout(null);
tips = new JLabel("Tips:使用上、下、左、右键盘控制");
tips.setFont(font);
tips.setBounds(40, 10, 400, 30);
tipsPan.add(tips);
MaxScoresText.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int code = e.getKeyCode(); // 获取按键代码
int a; // a 的引入是为了防止连加的情况出现
String str;
String str1;
int num;
switch (code) {
case KeyEvent.VK_LEFT:// 如果按键代码是左方向键
for (int i = 0; i < 4; i++) {
a = 5;
for (int k = 0; k < 3; k++) {
for (int j = 1; j < 4; j++) { // 遍历16个方块
str = nums[i][j].getText(); // 获取当前方块标签文本字符
str1 = nums[i][j - 1].getText(); // 获取当前左1方块标签文本字符
if (str1.compareTo("") == 0) { // 如果左1方块文本为空字符
nums[i][j - 1].setText(str); // 字符左移
setColor(i, j - 1, str);
nums[i][j].setText(""); // 当前方块字符置空
setColor(i, j, "");
} else if ((str.compareTo(str1) == 0)
&& (j != a) && (j != a - 1)) { // 如果当前方块和左1方块文本字符相等
num = Integer.parseInt(str);
scores += num;
times++;
str = String.valueOf(2 * num);
nums[i][j - 1].setText(str); // 左1方块文本字符变为两方块之和
setColor(i, j - 1, str);
nums[i][j].setText(""); // 当前方块字符置空
setColor(i, j, "");
a = j;
}
}
}
}
l1 = 1; // 用于判断游戏是否失败
CreateNum();
break;
case KeyEvent.VK_RIGHT:
for (int i = 0; i < 4; i++) {
a = 5;
for (int k = 0; k < 3; k++) {
for (int j = 2; j >= 0; j--) {
str = nums[i][j].getText();
str1 = nums[i][j + 1].getText();
if (str1.compareTo("") == 0) {
nums[i][j + 1].setText(str);
setColor(i, j + 1, str);
nums[i][j].setText("");
setColor(i, j, "");
} else if (str.compareTo(str1) == 0 && j != a
&& j != a + 1) {
num = Integer.parseInt(str);
scores += num;
times++;
str = String.valueOf(2 * num);
nums[i][j + 1].setText(str);
setColor(i, j + 1, str);
nums[i][j].setText("");
setColor(i, j, "");
a = j;
}
}
}
}
l2 = 1;
CreateNum();
break;
case KeyEvent.VK_UP:
for (int j = 0; j < 4; j++) {
a = 5;
for (int k = 0; k < 3; k++) {
for (int i = 1; i < 4; i++) {
str = nums[i][j].getText();
str1 = nums[i - 1][j].getText();
if (str1.compareTo("") == 0) {
nums[i - 1][j].setText(str);
setColor(i - 1, j, str);
nums[i][j].setText("");
setColor(i, j, "");
} else if (str.compareTo(str1) == 0 && i != a
&& i != a - 1) {
num = Integer.parseInt(str);
scores += num;
times++;
str = String.valueOf(2 * num);
nums[i - 1][j].setText(str);
setColor(i - 1, j, str);
nums[i][j].setText("");
setColor(i, j, "");
a = i;
}
}
}
}
l3 = 1;
CreateNum();
break;
case KeyEvent.VK_DOWN:
for (int j = 0; j < 4; j++) {
a = 5;
for (int k = 0; k < 5; k++) {
for (int i = 2; i >= 0; i--) {
str = nums[i][j].getText();
str1 = nums[i + 1][j].getText();
if (str1.compareTo("") == 0) {
nums[i + 1][j].setText(str);
setColor(i + 1, j, str);
nums[i][j].setText("");
setColor(i, j, "");
} else if (str.compareTo(str1) == 0 && i != a
&& i != a + 1) {
num = Integer.parseInt(str);
scores += num;
times++;
str = String.valueOf(2 * num);
nums[i + 1][j].setText(str);
setColor(i + 1, j, str);
nums[i][j].setText("");
setColor(i, j, "");
a = i;
}
}
}
}
l4 = 1;
CreateNum();
break;
default:
break;
}
ScoresText.setText(String.valueOf(scores));
}
});
CreateNum();
CreateNum();
}
public void CreateNum() {
int i, j;
boolean r = false;
String str;
if (times > 0) {
while (!r) {
i = random.nextInt(4);
j = random.nextInt(4);
str = nums[i][j].getText();
if ((str.compareTo("") == 0)) {
nums[i][j].setIcon(icon2);
nums[i][j].setText("2");
setColor(i, j, "2");
times--;
r = true;
l1 = l2 = l3 = l4 = 0;
}
}
} else if (l1 > 0 && l2 > 0 && l3 > 0 && l4 > 0) {
tips.setText(" GAME OVER !");
}
}
public void setColor(int i, int j, String str) {
switch (str) {
case "":
nums[i][j].setBackground(Color.LIGHT_GRAY);
break;
case "2":
nums[i][j].setBackground(new Color(240, 240, 240));
break;
case "4":
nums[i][j].setBackground(new Color(221, 221, 172));
break;
case "8":
nums[i][j].setBackground(ne
java版仿2048代码
需积分: 10 89 浏览量
2014-08-02
21:06:08
上传
评论
收藏 9KB ZIP 举报
andy_wu_11
- 粉丝: 0
- 资源: 3
最新资源
- 隐藏文件展示工具,用来展示被病毒隐藏的文件
- 基于Matlab的图像分割系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB指纹门禁GUI设计源码+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的仪表指数识别系统霍夫曼变换+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 疲劳驾驶检测专识别GUI源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于matlab的虫害侵蚀系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的教室人数统计系统 可以统计正脸情况下的人数+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计
- 基于MATLAB的水果分级系统,带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的车票发票识别系统带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈