java项目---2048
发布人:shili8
发布时间:2025-01-20 01:47
阅读次数:0
**Java项目——2048**
###介绍2048 是一个经典的数字游戏,玩家需要将数字滑动到相应的位置,以便合并成更大的数字。这个项目是用 Java 实现的一个简单的2048 游戏。
###依赖和环境* Java11 或以上版本* Maven 或 Gradle 构建工具(本示例使用 Maven)
###项目结构
markdownjava-2048/ src/ main/ java/ com/ example/ Java2048Game.javaGamePanel.javaTile.javaMain.javaresources/ index.htmlstyles.csspom.xml
### GamePanel.java这是游戏的主面板类,负责渲染和更新游戏界面。
javaimport javax.swing.*; import java.awt.*; public class GamePanel extends JPanel { private int[][] gameBoard; private int tileSize =100; public GamePanel(int size) { this.setPreferredSize(new Dimension(size * tileSize, size * tileSize)); this.gameBoard = new int[size][size]; initializeGame(); } private void initializeGame() { for (int i =0; i < gameBoard.length; i++) { for (int j =0; j < gameBoard[i].length; j++) { gameBoard[i][j] =0; } } addNewTile(); } private void addNewTile() { int randomRow = (int) (Math.random() * gameBoard.length); int randomCol = (int) (Math.random() * gameBoard[randomRow].length); if (gameBoard[randomRow][randomCol] ==0) { gameBoard[randomRow][randomCol] = (int) (Math.random() *4 +2); } else { addNewTile(); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i =0; i < gameBoard.length; i++) { for (int j =0; j < gameBoard[i].length; j++) { int value = gameBoard[i][j]; if (value !=0) { g.setColor(getColor(value)); g.fillRect(j * tileSize, i * tileSize, tileSize, tileSize); g.setColor(Color.WHITE); g.setFont(new Font("Arial", Font.BOLD,24)); String text = Integer.toString(value); int textWidth = g.getFontMetrics().stringWidth(text); int textHeight = g.getFontMetrics().getHeight(); g.drawString(text, j * tileSize + (tileSize - textWidth) /2, i * tileSize + tileSize - (textHeight +5)); } } } } private Color getColor(int value) { switch (value) { case2: return new Color(238,228,218); case4: return new Color(237,224,200); case8: return new Color(242,177,121); case16: return new Color(245,149,99); case32: return new Color(244,123,100); case64: return new Color(237,94,91); case128: return new Color(237,204,97); case256: return new Color(242,200,74); case512: return new Color(245,205,65); case1024: return new Color(237,194,52); case2048: return new Color(237,180,46); default: return Color.BLACK; } } public void moveUp() { for (int i =0; i < gameBoard.length -1; i++) { for (int j =0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] ==0) continue; int nextRow = i +1; while (nextRow < gameBoard.length && gameBoard[nextRow][j] !=0) { int temp = gameBoard[nextRow][j]; gameBoard[nextRow][j] = gameBoard[i][j]; gameBoard[i][j] = temp; nextRow++; } } } for (int i =0; i < gameBoard.length -1; i++) { for (int j =0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] == gameBoard[i +1][j]) { gameBoard[i][j] *=2; gameBoard[i +1][j] =0; addNewTile(); } } } } public void moveDown() { for (int i = gameBoard.length -1; i >0; i--) { for (int j =0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] ==0) continue; int prevRow = i -1; while (prevRow >=0 && gameBoard[prevRow][j] !=0) { int temp = gameBoard[prevRow][j]; gameBoard[prevRow][j] = gameBoard[i][j]; gameBoard[i][j] = temp; prevRow--; } } } for (int i = gameBoard.length -1; i >0; i--) { for (int j =0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] == gameBoard[i -1][j]) { gameBoard[i][j] *=2; gameBoard[i -1][j] =0; addNewTile(); } } } } public void moveLeft() { for (int i =0; i < gameBoard.length; i++) { for (int j =0; j < gameBoard[i].length -1; j++) { if (gameBoard[i][j] ==0) continue; int nextCol = j +1; while (nextCol < gameBoard[i].length && gameBoard[i][nextCol] !=0) { int temp = gameBoard[i][nextCol]; gameBoard[i][nextCol] = gameBoard[i][j]; gameBoard[i][j] = temp; nextCol++; } } } for (int i =0; i < gameBoard.length; i++) { for (int j =0; j < gameBoard[i].length -1; j++) { if (gameBoard[i][j] == gameBoard[i][j +1]) { gameBoard[i][j] *=2; gameBoard[i][j +1] =0; addNewTile(); } } } } public void moveRight() { for (int i =0; i < gameBoard.length; i++) { for (int j = gameBoard[i].length -1; j >0; j--) { if (gameBoard[i][j] ==0) continue; int prevCol = j -1; while (prevCol >=0 && gameBoard[i][prevCol] !=0) { int temp = gameBoard[i][prevCol]; gameBoard[i][prevCol] = gameBoard[i][j]; gameBoard[i][j] = temp; prevCol--; } } } for (int i =0; i < gameBoard.length; i++) { for (int j = gameBoard[i].length -1; j >0; j--) { if (gameBoard[i][j] == gameBoard[i][j -1]) { gameBoard[i][j] *=2; gameBoard[i][j -1] =0; addNewTile(); } } } } public void resetGame() { for (int i =0; i < gameBoard.length; i++) { for (int j =0; j < gameBoard[i].length; j++) { gameBoard[i][j] =0; } } addNewTile(); } public int[][] getGameBoard() { return gameBoard; } }
### Tile.java这是一个简单的类,用于表示游戏中的每个数字。
javapublic class Tile { private int value; public Tile(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }
### Main.java这是游戏的主类,负责启动和管理游戏。
javaimport javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("2048 Game"); frame