实现2048小游戏的游戏操作设计,实现键盘监听,方向键控制数字移动java源代码
时间: 2024-03-25 07:37:22 浏览: 60
好的,以下是Java实现2048小游戏的游戏操作设计和键盘监听的示例代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game2048 extends JFrame implements KeyListener {
private static final int ROWS = 4; // 行数
private static final int COLS = 4; // 列数
private int[][] grid = new int[ROWS][COLS]; // 游戏棋盘
private JLabel[][] labels = new JLabel[ROWS][COLS]; // 显示数字的标签
private boolean gameOver = false; // 游戏是否结束
// 构造方法
public Game2048() {
// 初始化游戏界面
setTitle("2048小游戏");
setLayout(new GridLayout(ROWS, COLS));
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
setFocusable(true);
setResizable(false);
// 初始化棋盘和标签
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
grid[i][j] = 0;
labels[i][j] = new JLabel("", SwingConstants.CENTER);
labels[i][j].setOpaque(true);
labels[i][j].setBackground(Color.WHITE);
add(labels[i][j]);
}
}
// 随机生成两个数字
generateNumber();
generateNumber();
}
// 生成一个数字
private void generateNumber() {
// 找到空位置
int[] empty = new int[ROWS * COLS];
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == 0) {
empty[count++] = i * COLS + j;
}
}
}
if (count == 0) {
// 棋盘已满,游戏结束
gameOver = true;
return;
}
// 随机选择一个空位置
int index = (int) (Math.random() * count);
int pos = empty[index];
int row = pos / COLS;
int col = pos % COLS;
// 随机生成数字
int value = (int) (Math.random() * 2) * 2 + 2;
grid[row][col] = value;
labels[row][col].setText(String.valueOf(value));
// 更新界面
labels[row][col].setBackground(getColor(value));
}
// 获取数字对应的颜色
private Color getColor(int value) {
switch (value) {
case 2: return new Color(0xeee4da);
case 4: return new Color(0xede0c8);
case 8: return new Color(0xf2b179);
case 16: return new Color(0xf59563);
case 32: return new Color(0xf67c5f);
case 64: return new Color(0xf65e3b);
case 128: return new Color(0xedcf72);
case 256: return new Color(0xedcc61);
case 512: return new Color(0xedc850);
case 1024: return new Color(0xedc53f);
case 2048: return new Color(0xedc22e);
default: return Color.WHITE;
}
}
// 判断游戏是否结束
private boolean isGameOver() {
if (!gameOver) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == 0) {
return false;
}
if (i > 0 && grid[i][j] == grid[i - 1][j]) {
return false;
}
if (j > 0 && grid[i][j] == grid[i][j - 1]) {
return false;
}
}
}
return true;
}
return false;
}
// 处理键盘按下事件
public void keyPressed(KeyEvent e) {
if (!gameOver) {
int keyCode = e.getKeyCode();
boolean moved = false;
switch (keyCode) {
case KeyEvent.VK_UP:
for (int j = 0; j < COLS; j++) {
for (int i = 1; i < ROWS; i++) {
if (grid[i][j] != 0) {
int k = i - 1;
while (k >= 0 && grid[k][j] == 0) {
k--;
}
if (k >= 0 && grid[k][j] == grid[i][j]) {
grid[k][j] *= 2;
grid[i][j] = 0;
labels[k][j].setText(String.valueOf(grid[k][j]));
labels[i][j].setText("");
labels[k][j].setBackground(getColor(grid[k][j]));
moved = true;
} else if (k + 1 != i) {
grid[k + 1][j] = grid[i][j];
grid[i][j] = 0;
labels[k + 1][j].setText(String.valueOf(grid[k + 1][j]));
labels[i][j].setText("");
labels[k + 1][j].setBackground(getColor(grid[k + 1][j]));
moved = true;
}
}
}
}
break;
case KeyEvent.VK_DOWN:
for (int j = 0; j < COLS; j++) {
for (int i = ROWS - 2; i >= 0; i--) {
if (grid[i][j] != 0) {
int k = i + 1;
while (k < ROWS && grid[k][j] == 0) {
k++;
}
if (k < ROWS && grid[k][j] == grid[i][j]) {
grid[k][j] *= 2;
grid[i][j] = 0;
labels[k][j].setText(String.valueOf(grid[k][j]));
labels[i][j].setText("");
labels[k][j].setBackground(getColor(grid[k][j]));
moved = true;
} else if (k - 1 != i) {
grid[k - 1][j] = grid[i][j];
grid[i][j] = 0;
labels[k - 1][j].setText(String.valueOf(grid[k - 1][j]));
labels[i][j].setText("");
labels[k - 1][j].setBackground(getColor(grid[k - 1][j]));
moved = true;
}
}
}
}
break;
case KeyEvent.VK_LEFT:
for (int i = 0; i < ROWS; i++) {
for (int j = 1; j < COLS; j++) {
if (grid[i][j] != 0) {
int k = j - 1;
while (k >= 0 && grid[i][k] == 0) {
k--;
}
if (k >= 0 && grid[i][k] == grid[i][j]) {
grid[i][k] *= 2;
grid[i][j] = 0;
labels[i][k].setText(String.valueOf(grid[i][k]));
labels[i][j].setText("");
labels[i][k].setBackground(getColor(grid[i][k]));
moved = true;
} else if (k + 1 != j) {
grid[i][k + 1] = grid[i][j];
grid[i][j] = 0;
labels[i][k + 1].setText(String.valueOf(grid[i][k + 1]));
labels[i][j].setText("");
labels[i][k + 1].setBackground(getColor(grid[i][k + 1]));
moved = true;
}
}
}
}
break;
case KeyEvent.VK_RIGHT:
for (int i = 0; i < ROWS; i++) {
for (int j = COLS - 2; j >= 0; j--) {
if (grid[i][j] != 0) {
int k = j + 1;
while (k < COLS && grid[i][k] == 0) {
k++;
}
if (k < COLS && grid[i][k] == grid[i][j]) {
grid[i][k] *= 2;
grid[i][j] = 0;
labels[i][k].setText(String.valueOf(grid[i][k]));
labels[i][j].setText("");
labels[i][k].setBackground(getColor(grid[i][k]));
moved = true;
} else if (k - 1 != j) {
grid[i][k - 1] = grid[i][j];
grid[i][j] = 0;
labels[i][k - 1].setText(String.valueOf(grid[i][k - 1]));
labels[i][j].setText("");
labels[i][k - 1].setBackground(getColor(grid[i][k - 1]));
moved = true;
}
}
}
}
break;
default:
break;
}
if (moved) {
generateNumber();
if (isGameOver()) {
gameOver = true;
JOptionPane.showMessageDialog(this, "游戏结束!");
}
}
}
}
// 处理键盘释放事件
public void keyReleased(KeyEvent e) {
}
// 处理键盘输入事件
public void keyTyped(KeyEvent e) {
}
// 主方法
public static void main(String[] args) {
Game2048 game = new Game2048();
game.setVisible(true);
}
}
```
以上代码实现了2048小游戏的游戏操作设计和键盘监听,包括生成数字、判断游戏结束、处理键盘按下事件等功能。你可以将代码复制到Java开发工具中运行,体验一下这个小游戏。
阅读全文