激情六月丁香婷婷|亚洲色图AV二区|丝袜AV日韩AV|久草视频在线分类|伊人九九精品视频|国产精品一级电影|久草视频在线99|在线看的av网址|伊人99精品无码|午夜无码视频在线

高校合作1:010-59833514 ?咨詢電話:400-810-1418 服務(wù)與監(jiān)督電話:400-810-1418轉(zhuǎn)接2

js怎么開發(fā)游戲(怎樣用JavaScript開發(fā)一個(gè)Web版的迷宮游戲?這是第一講)

發(fā)布時(shí)間:2023-11-27 17:40:33 瀏覽量:110次

?怎樣用JavaScript開發(fā)一個(gè)Web版的迷宮游戲?這是第一講

js怎么開發(fā)游戲(怎樣用JavaScript開發(fā)一個(gè)Web版的迷宮游戲?這是第一講)

為了減小篇幅,我計(jì)劃分2-3篇來寫,不足之處,歡迎指正。

在這里,我教大家如何實(shí)現(xiàn)一個(gè)簡單的迷宮游戲。這個(gè)迷宮很簡單,但可以玩。最終效果如下圖所示。

初始界面

挑戰(zhàn)成功界面

這是我?guī)啄昵皩懙挠螒?,代碼可能寫的不是很好,當(dāng)時(shí)用的語言是typescript,改成JS版很容易。

我是用畫布實(shí)現(xiàn)的,要實(shí)現(xiàn)這個(gè)游戲,主要需要這些步驟:

我們需要根據(jù)視口寬度,行數(shù)和列數(shù),以及墻的厚度來生成迷宮。我們需要根據(jù)設(shè)備像素比來更新畫布的寬和高,并計(jì)算路的寬度。在這里,我將路稱為單元格。

 private updateSize (options: UiOptions = {}) {
    this.cols = options.cols || this.cols || 16
    const width = this.cvs.offsetWidth * this.pixRatio
    const maxWallWidth = width / (this.cols * 2 + 1)
    const wallWidth = Math.min(maxWallWidth, options.wallWidth || this.pixRatio * 5)
    const cellWidth = (width - (this.cols + 1) * wallWidth) / this.cols
    const maxHeight = (this.cvs.parentElement?.offsetHeight as number) * this.pixRatio
    const maxRows = Math.floor((maxHeight - wallWidth) / (cellWidth + wallWidth))
    this.rows = Math.min(maxRows, options.rows || this.cols)
    this.realRows = this.rows * 2 - 1
    this.realCols = this.cols * 2 - 1
    this.cellWidth = cellWidth
    this.wallWidth = wallWidth
    this.cvs.width = this.gameCvs.width = width
    this.cvs.height = this.gameCvs.height = this.rows * (cellWidth + wallWidth) + wallWidth
  }

我們需要根據(jù)行數(shù)和列數(shù),生成一個(gè)初始的網(wǎng)格,這個(gè)網(wǎng)格是一個(gè)二維數(shù)組,包含:墻和單元格。我們需要一個(gè)標(biāo)識(shí),來確定格子的類型。

private genGrid () {
    const grid: Block[][] = []
    const { realRows, realCols } = this
    for (let row = 0; row < realRows; row++) {
      grid[row] = []
      for (let col = 0; col < realCols; col++) {
        grid[row][col] = {
          row,
          col,
          type: row % 2 || col % 2 ? BlockType.WALL : BlockType.CELL
        }
      }
    }
    return grid
  }

有了尺寸和網(wǎng)格,我們還需要生成入口和出口位置。這個(gè)位置是根據(jù)列數(shù),以及單元格和墻的寬度,隨機(jī)生成的。代碼如下:

js怎么開發(fā)游戲(怎樣用JavaScript開發(fā)一個(gè)Web版的迷宮游戲?這是第一講)

private getStartPoint () {
    const col = getRandInt(0, this.cols - 1)
    const { cellWidth, wallWidth } = this
    return {
      x: (cellWidth + wallWidth) * (col + 1 / 2),
      y: wallWidth + cellWidth / 2 - this.pixRatio / 2
    }
  }

private getEndPoint () {
    const col = getRandInt(0, this.cols - 1)
    const { cellWidth, wallWidth } = this
    return {
      x: (cellWidth + wallWidth) * col + wallWidth / 2,
      y: this.cvs.height - this.wallWidth
    }
  }

在迷宮圖中,我們的可移動(dòng)目標(biāo)是一個(gè)圓形物體。需要給它一個(gè)初始位置,這個(gè)位置就是入口。還需要提供一個(gè)半徑,用于繪制,這個(gè)圓的直徑必須小于單元格寬度。

this.ball = { ...this.startPoint, r: this.cellWidth * .32 }

感謝閱讀!以上就是本篇文章的全部內(nèi)容,童鞋們都看懂了嗎?下篇文章,我將給大家講解迷宮圖的生成,以及怎樣在迷宮中移動(dòng)目標(biāo),還會(huì)涉及到碰撞檢測等。

#頭條創(chuàng)作挑戰(zhàn)賽# #前端##程序員#

js怎么開發(fā)游戲(怎樣用JavaScript開發(fā)一個(gè)Web版的迷宮游戲?這是第一講)

熱門課程推薦

熱門資訊

請(qǐng)綁定手機(jī)號(hào)

x

同學(xué)您好!

您已成功報(bào)名0元試學(xué)活動(dòng),老師會(huì)在第一時(shí)間與您取得聯(lián)系,請(qǐng)保持電話暢通!
確定