欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SpringBoot和Vue寫一個2048小游戲

 更新時間:2024年08月23日 10:03:23   作者:戰(zhàn)族狼魂  
創(chuàng)建一個基于 Java Spring Boot 后端和 Vue 前端的 2048 游戲,可以按照以下步驟進(jìn)行,這個項目將包括后端(用來處理游戲邏輯)和前端(用來顯示游戲界面和與用戶交互),感興趣的小伙伴可以參考本文自己動手嘗試一下

1. 設(shè)置項目結(jié)構(gòu)

你需要創(chuàng)建一個包含兩個部分的項目:

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

2. 后端 (Spring Boot)

首先,創(chuàng)建一個新的 Spring Boot 項目。你可以使用 Spring Initializr 或者任何其他你喜歡的方式。

項目依賴

確保添加以下依賴:

  • Spring Web
  • Spring Boot DevTools

編寫游戲邏輯

在 src/main/java 下創(chuàng)建一個用于處理游戲邏輯的包,例如 com.example.game.

GameController.java

package com.example.game;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class GameController {
 
    private Game game = new Game();
 
    @GetMapping("/game")
    public int[][] getGameState() {
        return game.getBoard();
    }
 
    @PostMapping("/move")
    public int[][] makeMove(@RequestBody String direction) {
        switch (direction) {
            case "up":
                game.moveUp();
                break;
            case "down":
                game.moveDown();
                break;
            case "left":
                game.moveLeft();
                break;
            case "right":
                game.moveRight();
                break;
        }
        game.addRandomTile();
        return game.getBoard();
    }
}

Game.java

package com.example.game;
 
import java.util.Random;
 
public class Game {
    private int[][] board = new int[4][4];
    private Random random = new Random();
 
    public Game() {
        addRandomTile();
        addRandomTile();
    }
 
    public int[][] getBoard() {
        return board;
    }
 
    public void moveUp() {
        for (int col = 0; col < 4; col++) {
            int[] column = new int[4];
            int index = 0;
            for (int row = 0; row < 4; row++) {
                if (board[row][col] != 0) {
                    column[index++] = board[row][col];
                }
            }
            merge(column);
            for (int row = 0; row < 4; row++) {
                board[row][col] = column[row];
            }
        }
    }
 
    public void moveDown() {
        for (int col = 0; col < 4; col++) {
            int[] column = new int[4];
            int index = 0;
            for (int row = 3; row >= 0; row--) {
                if (board[row][col] != 0) {
                    column[index++] = board[row][col];
                }
            }
            merge(column);
            for (int row = 0; row < 4; row++) {
                board[3 - row][col] = column[row];
            }
        }
    }
 
    public void moveLeft() {
        for (int row = 0; row < 4; row++) {
            int[] newRow = new int[4];
            int index = 0;
            for (int col = 0; col < 4; col++) {
                if (board[row][col] != 0) {
                    newRow[index++] = board[row][col];
                }
            }
            merge(newRow);
            board[row] = newRow;
        }
    }
 
    public void moveRight() {
        for (int row = 0; row < 4; row++) {
            int[] newRow = new int[4];
            int index = 0;
            for (int col = 3; col >= 0; col--) {
                if (board[row][col] != 0) {
                    newRow[index++] = board[row][col];
                }
            }
            merge(newRow);
            for (int col = 0; col < 4; col++) {
                board[row][3 - col] = newRow[col];
            }
        }
    }
 
    private void merge(int[] row) {
        for (int i = 0; i < 3; i++) {
            if (row[i] != 0 && row[i] == row[i + 1]) {
                row[i] *= 2;
                row[i + 1] = 0;
                i++;
            }
        }
        int[] newRow = new int[4];
        int index = 0;
        for (int num : row) {
            if (num != 0) {
                newRow[index++] = num;
            }
        }
        System.arraycopy(newRow, 0, row, 0, 4);
    }
 
    public void addRandomTile() {
        int emptyTiles = 0;
        for (int[] row : board) {
            for (int tile : row) {
                if (tile == 0) {
                    emptyTiles++;
                }
            }
        }
        if (emptyTiles == 0) {
            return;
        }
        int randomTile = random.nextInt(emptyTiles);
        int value = random.nextInt(10) < 9 ? 2 : 4;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (board[i][j] == 0) {
                    if (randomTile == 0) {
                        board[i][j] = value;
                        return;
                    }
                    randomTile--;
                }
            }
        }
    }
}

3. 前端 (Vue.js)

創(chuàng)建一個新的 Vue 項目,你可以使用 Vue CLI 或者 Vite 等工具。

設(shè)置 Vue 項目

安裝 Axios 以便與后端進(jìn)行通信:

npm install axios

創(chuàng)建游戲界面

在 src/components 目錄下創(chuàng)建一個 Game.vue 組件:

<template>
  <div class="game">
    <div class="board">
      <div v-for="(row, rowIndex) in board" :key="rowIndex" class="row">
        <div v-for="(cell, colIndex) in row" :key="colIndex" class="cell">
          {{ cell || '' }}
        </div>
      </div>
    </div>
    <div class="controls">
      <button @click="move('up')">Up</button>
      <button @click="move('down')">Down</button>
      <button @click="move('left')">Left</button>
      <button @click="move('right')">Right</button>
    </div>
  </div>
</template>
 
<script>
import axios from 'axios';
 
export default {
  data() {
    return {
      board: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    };
  },
  mounted() {
    this.getBoard();
  },
  methods: {
    async getBoard() {
      const response = await axios.get('/game');
      this.board = response.data;
    },
    async move(direction) {
      const response = await axios.post('/move', direction);
      this.board = response.data;
    }
  }
};
</script>
 
<style>
.game {
  text-align: center;
}
 
.board {
  display: inline-block;
}
 
.row {
  display: flex;
}
 
.cell {
  width: 50px;
  height: 50px;
  background-color: #ccc;
  margin: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}
 
.controls {
  margin-top: 20px;
}
</style>

在 App.vue 中導(dǎo)入 Game.vue

<template>
  <div id="app">
    <Game />
  </div>
</template>
 
<script>
import Game from './components/Game.vue';
 
export default {
  name: 'App',
  components: {
    Game
  }
};
</script>
 
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  margin-top: 60px;
}
</style>

4. 運行和調(diào)試

  • 啟動 Spring Boot 項目。
  • 啟動 Vue 前端項目。

確保 Spring Boot 的默認(rèn)端口(8080)與 Vue 的默認(rèn)端口(通常是8081)不會沖突。

你現(xiàn)在應(yīng)該能夠在瀏覽器中訪問 Vue 前端,并與 Spring Boot 后端進(jìn)行交互,從而玩 2048 游戲。

到此這篇關(guān)于基于SpringBoot和Vue寫一個2048小游戲的文章就介紹到這了,更多相關(guān)SpringBoot Vue 2048游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java編程實現(xiàn)國際象棋棋盤

    java編程實現(xiàn)國際象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了java編程實現(xiàn)國際象棋棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • java案例實戰(zhàn)之字符串轉(zhuǎn)換為二進(jìn)制

    java案例實戰(zhàn)之字符串轉(zhuǎn)換為二進(jìn)制

    最近遇到個需求,要求編寫一個程序,從鍵盤錄入一個字符串,將字符串轉(zhuǎn)換為二進(jìn)制數(shù),下面這篇文章主要給大家介紹了關(guān)于java字符串轉(zhuǎn)換為二進(jìn)制的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • SpringBoot整合MybatisPlus的基本應(yīng)用詳解

    SpringBoot整合MybatisPlus的基本應(yīng)用詳解

    MyBatis-Plus (簡稱 MP)是一個 MyBatis的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為 簡化開發(fā)、提高效率而生,本文將給大家介紹一下SpringBoot整合MybatisPlus的基本應(yīng)用,需要的朋友可以參考下
    2024-05-05
  • Java如何對返回參數(shù)進(jìn)行處理

    Java如何對返回參數(shù)進(jìn)行處理

    這篇文章主要介紹了Java如何對返回參數(shù)進(jìn)行處理問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java使用opencv識別二維碼的完整步驟

    Java使用opencv識別二維碼的完整步驟

    OpenMV是一個開源,低成本,功能強大的機器視覺模塊,下面這篇文章主要給大家介紹了關(guān)于Java使用opencv識別二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Java 并發(fā)編程ArrayBlockingQueue的實現(xiàn)

    Java 并發(fā)編程ArrayBlockingQueue的實現(xiàn)

    這篇文章主要介紹了Java 并發(fā)編程ArrayBlockingQueue的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 運用springboot搭建并部署web項目的示例

    運用springboot搭建并部署web項目的示例

    這篇文章主要介紹了運用springboot搭建并部署web項目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Java指令重排序在多線程環(huán)境下的處理方法

    Java指令重排序在多線程環(huán)境下的處理方法

    指令重排在單線程環(huán)境下有利于提高程序的執(zhí)行效率,不會對程序產(chǎn)生負(fù)面影響,本文對多線程指令重排問題進(jìn)行復(fù)原,并針對指令重排給出相應(yīng)的解決方案,需要的朋友參考下吧
    2022-04-04
  • spring?boot實現(xiàn)圖片上傳到后臺的功能(瀏覽器可直接訪問)

    spring?boot實現(xiàn)圖片上傳到后臺的功能(瀏覽器可直接訪問)

    這篇文章主要介紹了spring?boot實現(xiàn)圖片上傳到后臺的功能(瀏覽器可直接訪問),需要的朋友可以參考下
    2022-04-04
  • java數(shù)組的初始化及操作詳解

    java數(shù)組的初始化及操作詳解

    在本文中小編給大家整理了關(guān)于java數(shù)組的初始化及操作的相關(guān)知識點內(nèi)容,需要的讀者們參考下。
    2019-07-07

最新評論