第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在蛇游戲中隨機更改食物的背景圖像

在蛇游戲中隨機更改食物的背景圖像

FFIVE 2023-03-03 09:32:36
我的想法是只有在食物被吃完后才改變背景圖像。然而,它只是在我列表中的圖像之間來回變化。我認為這是因為我在腳本中使用了 requestAnimationFrame。這是 script.jsimport { update as updateSnake, draw as drawSnake, getSnakeHead, snakeIntersection } from './snake.js'import { update as updateFood, draw as drawFood} from './food.js'import { outsideGrid } from './grid.js'let lastRenderTime = 0let gameOver = 0;let modal = document.getElementById('modal')let easy = document.getElementById('easy')let normal = document.getElementById('normal')let hard = document.getElementById('hard')let SNAKE_SPEEDeasy.onclick = function() {    modal.style.display = "none"    chooseDifficulty(easy)}normal.onclick = function() {    modal.style.display = "none"    chooseDifficulty(normal)}hard.onclick = function() {    modal.style.display = "none"    chooseDifficulty(hard)}const gameBoard = document.getElementById('game-board')function chooseDifficulty(difficulty) {    switch (difficulty) {        case easy:            SNAKE_SPEED = 5;            break;        case normal:            SNAKE_SPEED = 8;            break;        case hard:            SNAKE_SPEED = 12;            break;    }    }function main(currentTime) {    if (gameOver) {        if (confirm('You lost. Press OK to restart')) {            location.reload()        }        return    }    window.requestAnimationFrame(main)    const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000    if (secondsSinceLastRender < 1 / SNAKE_SPEED) return    lastRenderTime = currentTime;    update()    draw()}window.requestAnimationFrame(main)function update() {    updateSnake()    updateFood()    checkDeath()}function draw() {    gameBoard.innerHTML = ''    drawSnake(gameBoard)    drawFood(gameBoard)}function checkDeath() {    gameOver = outsideGrid(getSnakeHead()) || snakeIntersection()}如何在吃完食物后只改變一次食物的圖像?
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經(jīng)驗 獲得超3個贊

update您可以在 food.js 中更新您的 foodImg函數(shù)。


例如:(food.js)


import { onSnake, expandSnake } from './snake.js'

import { randomGridPosition } from './grid.js'

let food = getRandomPosition()

let foodList = ['assets/giraffe.png','assets/lion.png','assets/kangaroo.png'];

let foodImg = foodList[Math.floor(Math.random() * 3)]; // <---- here get random food image

const EXPANSION_RATE = 1

export function update() {

    if (onSnake(food)) {

        expandSnake(EXPANSION_RATE);

        foodImg = foodList[Math.floor(Math.random() * 3)]; // <-- here get new foodImg after food eaten.

        food = getRandomPosition();

    }

    

}


export function draw(gameBoard) {

        const foodElement = document.createElement("div")

        foodElement.style.gridRowStart = food.y

        foodElement.style.gridColumnStart = food.x

        foodElement.classList.add('food')

        // foodImg = foodList[Math.floor(Math.random() * 3)]

        foodElement.style.backgroundImage = 'url(' + foodImg + ')'

        gameBoard.appendChild(foodElement)

        

}


function getRandomPosition() {

    let newFoodPosition

    while (newFoodPosition == null || onSnake(newFoodPosition)) {

        newFoodPosition = randomGridPosition()

    }

    return newFoodPosition

}


查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號