用SpringBoot和vue写一个2048小游戏

打印 上一主题 下一主题

主题 1668|帖子 1668|积分 5004

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
创建一个基于 Java Spring Boot 后端和 Vue 前端的 2048 游戏,可以按照以下步调进行。这个项目将包括后端(用来处理处罚游戏逻辑)和前端(用来显示游戏界面和与用户交互)。
目录
1. 设置项目结构
2. 后端 (Spring Boot)
项目依赖
编写游戏逻辑
GameController.java
Game.java
3. 前端 (Vue.js)
设置 Vue 项目
创建游戏界面
在 App.vue 中导入 Game.vue
4. 运行和调试


1. 设置项目结构

你需要创建一个包含两个部分的项目:


  • 后端 (Java Spring Boot)
  • 前端 (Vue.js)
2. 后端 (Spring Boot)

首先,创建一个新的 Spring Boot 项目。你可以利用 Spring Initializr 或者任何其他你喜好的方式。
项目依赖

确保添加以下依赖:


  • Spring Web
  • Spring Boot DevTools
编写游戏逻辑

在 src/main/java 下创建一个用于处理处罚游戏逻辑的包,例如 com.example.game.
GameController.java

  1. package com.example.game;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class GameController {
  8.     private Game game = new Game();
  9.     @GetMapping("/game")
  10.     public int[][] getGameState() {
  11.         return game.getBoard();
  12.     }
  13.     @PostMapping("/move")
  14.     public int[][] makeMove(@RequestBody String direction) {
  15.         switch (direction) {
  16.             case "up":
  17.                 game.moveUp();
  18.                 break;
  19.             case "down":
  20.                 game.moveDown();
  21.                 break;
  22.             case "left":
  23.                 game.moveLeft();
  24.                 break;
  25.             case "right":
  26.                 game.moveRight();
  27.                 break;
  28.         }
  29.         game.addRandomTile();
  30.         return game.getBoard();
  31.     }
  32. }
复制代码
Game.java

  1. package com.example.game;
  2. import java.util.Random;
  3. public class Game {
  4.     private int[][] board = new int[4][4];
  5.     private Random random = new Random();
  6.     public Game() {
  7.         addRandomTile();
  8.         addRandomTile();
  9.     }
  10.     public int[][] getBoard() {
  11.         return board;
  12.     }
  13.     public void moveUp() {
  14.         for (int col = 0; col < 4; col++) {
  15.             int[] column = new int[4];
  16.             int index = 0;
  17.             for (int row = 0; row < 4; row++) {
  18.                 if (board[row][col] != 0) {
  19.                     column[index++] = board[row][col];
  20.                 }
  21.             }
  22.             merge(column);
  23.             for (int row = 0; row < 4; row++) {
  24.                 board[row][col] = column[row];
  25.             }
  26.         }
  27.     }
  28.     public void moveDown() {
  29.         for (int col = 0; col < 4; col++) {
  30.             int[] column = new int[4];
  31.             int index = 0;
  32.             for (int row = 3; row >= 0; row--) {
  33.                 if (board[row][col] != 0) {
  34.                     column[index++] = board[row][col];
  35.                 }
  36.             }
  37.             merge(column);
  38.             for (int row = 0; row < 4; row++) {
  39.                 board[3 - row][col] = column[row];
  40.             }
  41.         }
  42.     }
  43.     public void moveLeft() {
  44.         for (int row = 0; row < 4; row++) {
  45.             int[] newRow = new int[4];
  46.             int index = 0;
  47.             for (int col = 0; col < 4; col++) {
  48.                 if (board[row][col] != 0) {
  49.                     newRow[index++] = board[row][col];
  50.                 }
  51.             }
  52.             merge(newRow);
  53.             board[row] = newRow;
  54.         }
  55.     }
  56.     public void moveRight() {
  57.         for (int row = 0; row < 4; row++) {
  58.             int[] newRow = new int[4];
  59.             int index = 0;
  60.             for (int col = 3; col >= 0; col--) {
  61.                 if (board[row][col] != 0) {
  62.                     newRow[index++] = board[row][col];
  63.                 }
  64.             }
  65.             merge(newRow);
  66.             for (int col = 0; col < 4; col++) {
  67.                 board[row][3 - col] = newRow[col];
  68.             }
  69.         }
  70.     }
  71.     private void merge(int[] row) {
  72.         for (int i = 0; i < 3; i++) {
  73.             if (row[i] != 0 && row[i] == row[i + 1]) {
  74.                 row[i] *= 2;
  75.                 row[i + 1] = 0;
  76.                 i++;
  77.             }
  78.         }
  79.         int[] newRow = new int[4];
  80.         int index = 0;
  81.         for (int num : row) {
  82.             if (num != 0) {
  83.                 newRow[index++] = num;
  84.             }
  85.         }
  86.         System.arraycopy(newRow, 0, row, 0, 4);
  87.     }
  88.     public void addRandomTile() {
  89.         int emptyTiles = 0;
  90.         for (int[] row : board) {
  91.             for (int tile : row) {
  92.                 if (tile == 0) {
  93.                     emptyTiles++;
  94.                 }
  95.             }
  96.         }
  97.         if (emptyTiles == 0) {
  98.             return;
  99.         }
  100.         int randomTile = random.nextInt(emptyTiles);
  101.         int value = random.nextInt(10) < 9 ? 2 : 4;
  102.         for (int i = 0; i < 4; i++) {
  103.             for (int j = 0; j < 4; j++) {
  104.                 if (board[i][j] == 0) {
  105.                     if (randomTile == 0) {
  106.                         board[i][j] = value;
  107.                         return;
  108.                     }
  109.                     randomTile--;
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
复制代码
3. 前端 (Vue.js)

创建一个新的 Vue 项目,你可以利用 Vue CLI 或者 Vite 等工具。
设置 Vue 项目

安装 Axios 以便与后端进行通讯:
  1. npm install axios
复制代码
创建游戏界面

在 src/components 目录下创建一个 Game.vue 组件:
  1. <template>
  2.   <div class="game">
  3.     <div class="board">
  4.       <div v-for="(row, rowIndex) in board" :key="rowIndex" class="row">
  5.         <div v-for="(cell, colIndex) in row" :key="colIndex" class="cell">
  6.           {{ cell || '' }}
  7.         </div>
  8.       </div>
  9.     </div>
  10.     <div class="controls">
  11.       <button @click="move('up')">Up</button>
  12.       <button @click="move('down')">Down</button>
  13.       <button @click="move('left')">Left</button>
  14.       <button @click="move('right')">Right</button>
  15.     </div>
  16.   </div>
  17. </template>
  18. <script>
  19. import axios from 'axios';
  20. export default {
  21.   data() {
  22.     return {
  23.       board: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
  24.     };
  25.   },
  26.   mounted() {
  27.     this.getBoard();
  28.   },
  29.   methods: {
  30.     async getBoard() {
  31.       const response = await axios.get('/game');
  32.       this.board = response.data;
  33.     },
  34.     async move(direction) {
  35.       const response = await axios.post('/move', direction);
  36.       this.board = response.data;
  37.     }
  38.   }
  39. };
  40. </script>
  41. <style>
  42. .game {
  43.   text-align: center;
  44. }
  45. .board {
  46.   display: inline-block;
  47. }
  48. .row {
  49.   display: flex;
  50. }
  51. .cell {
  52.   width: 50px;
  53.   height: 50px;
  54.   background-color: #ccc;
  55.   margin: 5px;
  56.   display: flex;
  57.   justify-content: center;
  58.   align-items: center;
  59.   font-size: 18px;
  60.   font-weight: bold;
  61. }
  62. .controls {
  63.   margin-top: 20px;
  64. }
  65. </style>
复制代码
在 App.vue 中导入 Game.vue

  1. <template>
  2.   <div id="app">
  3.     <Game />
  4.   </div>
  5. </template>
  6. <script>
  7. import Game from './components/Game.vue';
  8. export default {
  9.   name: 'App',
  10.   components: {
  11.     Game
  12.   }
  13. };
  14. </script>
  15. <style>
  16. #app {
  17.   font-family: Avenir, Helvetica, Arial, sans-serif;
  18.   -webkit-font-smoothing: antialiased;
  19.   -moz-osx-font-smoothing: grayscale;
  20.   text-align: center;
  21.   margin-top: 60px;
  22. }
  23. </style>
复制代码
4. 运行和调试



  • 启动 Spring Boot 项目。
  • 启动 Vue 前端项目。
确保 Spring Boot 的默认端口(8080)与 Vue 的默认端口(通常是8081)不会冲突。
你现在应该可以或许在浏览器中访问 Vue 前端,并与 Spring Boot 后端进行交互,从而玩 2048 游戏。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万有斥力

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表