this.add(tips); //定义音频 AePlayWave apw=new AePlayWave(\后弦 - 有你就很幸福_new.wav\ apw.start(); Create(); // 初始时在随机位置产生一个2号或4号方块 Create(); //记录一下当前的状态 for(int i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ if( \ else temp1 [i][j] = temp2[i][j] = Integer.parseInt(texts[i][j].getText()); } } }
// 设置标签颜色 public void setColor(int i, int j, String str) { if (\ texts[i][j].setBackground(new Color(120,221,247)); return; } int result = Integer.parseInt(str); switch (result) { case 2: texts[i][j].setBackground(new Color(240,240,0)); break; case 4: texts[i][j].setBackground(Color.pink); break; case 8: texts[i][j].setBackground(new Color(240,150,10)); break; case 16: texts[i][j].setBackground(Color.green); break; case 32: texts[i][j].setBackground(Color.magenta); break; case 64: texts[i][j].setBackground(new Color(155,10,190)); break; case 128: texts[i][j].setBackground(Color.blue); break; case 256: texts[i][j].setBackground(Color.gray); break; case 512: texts[i][j].setBackground(Color.cyan); break; case 1024: texts[i][j].setBackground(Color.DARK_GRAY); break; case 2048: texts[i][j].setBackground(Color.red); break; case 4096: texts[i][j].setBackground(Color.white); break; default: break; } } // 按键输入事件的处理方法 protected void do_label_keyPressed(final KeyEvent e) { int code = e.getKeyCode();// 获取按键代码 int a; // a的引入是为了防止连加的情况出现 String str; String str1; int num;
14
字符 字符相等 表示形式 块之和
switch (code) {
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A: // 如果按键代码是左方向键或者A键 biaoji = 0; // 标记左边能不能合并 for (int i = 0; i < 4; i++) { a = 5; for (int k = 0; k < 3; k++) { // 让左边每2个能合并的全合并了 for (int j = 1; j < 4; j++) {// 遍历16个方块 str = texts[i][j].getText();// 获取当前方块标签文本字符 str1 = texts[i][j - 1].getText();// 获取当前左1方块标签文本
if (str1.compareTo(\如果左1方块文本为空字符 if (str.compareTo(\ biaoji = 1; // 如果向左能移,就标记一下 texts[i][j - 1].setText(str);// 字符左移 setColor(i, j - 1, str); texts[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); // 返回 int参数的字符串
texts[i][j - 1].setText(str);// 左1方块文本字符变为两方
setColor(i, j - 1, str); texts[i][j].setText(\当前方块字符置空 setColor(i, j, \ a = j; biaoji = 1; } } } } p1 = 1; Create(); break;
case KeyEvent.VK_RIGHT: case KeyEvent.VK_D: biaoji = 0;
15
for (int i = 0; i < 4; i++) { a = 5; for (int k = 0; k < 3; k++) { for (int j = 2; j >= 0; j--) { str = texts[i][j].getText(); str1 = texts[i][j + 1].getText();
if (str1.compareTo(\ if (str.compareTo(\ biaoji = 1; texts[i][j + 1].setText(str); setColor(i, j + 1, str); texts[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); texts[i][j + 1].setText(str); setColor(i, j + 1, str); texts[i][j].setText(\ setColor(i, j, \ a = j; biaoji = 1; } } } } p2 = 1; Create(); break;
case KeyEvent.VK_UP: case KeyEvent.VK_W: biaoji = 0; for (int j = 0; j < 4; j++) { a = 5; for (int k = 0; k < 3; k++) { for (int i = 1; i < 4; i++) { str = texts[i][j].getText(); str1 = texts[i - 1][j].getText();
if (str1.compareTo(\ if (str.compareTo(\
16
biaoji = 1; texts[i - 1][j].setText(str); setColor(i - 1, j, str); texts[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); texts[i - 1][j].setText(str); setColor(i - 1, j, str); texts[i][j].setText(\ setColor(i, j, \ a = i; biaoji = 1; } } } } p3 = 1; Create(); break;
case KeyEvent.VK_DOWN: case KeyEvent.VK_S: biaoji = 0; for (int j = 0; j < 4; j++) { a = 5; for (int k = 0; k < 5; k++) { for (int i = 2; i >= 0; i--) { str = texts[i][j].getText(); str1 = texts[i + 1][j].getText();
if (str1.compareTo(\ if (str.compareTo(\ biaoji = 1; texts[i + 1][j].setText(str); setColor(i + 1, j, str); texts[i][j].setText(\ setColor(i, j, \
} else if (str.compareTo(str1) == 0 && i != a && i != a + 1) { num = Integer.parseInt(str); scores += num;
17
times++; str = String.valueOf(2 * num); texts[i + 1][j].setText(str); setColor(i + 1, j, str); texts[i][j].setText(\ setColor(i, j, \ a = i; biaoji = 1; } } } } p4 = 1; Create(); break; case KeyEvent.VK_Z: if (p1 <= 0 || p2 <= 0 || p3 <= 0 || p4 <= 0){ for(int i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ if(temp1[i][j] == 0) texts[i][j].setText(\ else texts[i][j].setText( String.valueOf(temp1[i][j]) ); temp2[i][j] = temp1[i][j]; setColor(i, j, texts[i][j].getText()); } } scores = temp1scores; temp2scores = temp1scores; } break; default: break; } if(maxscores<=scores) { maxscores=scores; Recorder.setMaxscores_2048(maxscores); Recorder.keepRecording(); } currentScores.setText(\ labelMaxScores.setText(\最高得分标签 } // 鼠标移动的输入事件的处理方法
18