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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

元素未在畫布上渲染

元素未在畫布上渲染

神不在的星期二 2023-07-20 15:52:12
我正在制作一款乒乓球游戲,一切都很順利,直到我編寫了評(píng)分系統(tǒng),該系統(tǒng)有兩個(gè)變量,一個(gè)用于玩家的得分,另一個(gè)用于人工智能的得分。當(dāng)球經(jīng)過(guò)球拍時(shí),它會(huì)檢測(cè)到它擊中了哪堵墻,并將右側(cè)得分變量加 1,然后提醒得分?,F(xiàn)在,當(dāng)我運(yùn)行游戲時(shí),畫布元素只是空白。我想知道這里是否有人能弄清楚發(fā)生了什么事。這是我的代碼。<canvas id='my' width='640' height='480'></canvas><script>  var canvas = document.getElementById("my");  var ctx = canvas.getContext("2d");  function paddle(x, y, width, height) {    this.x = x;    this.y = y;    this.width = width;    this.height = height;    this.speedModifier = 0;    this.hasCollidedWith = function(ball) {      var paddleLeftWall = this.x;      var paddleRightWall = this.x + this.width;      var paddleTopWall = this.y;      var paddleBottomWall = this.y + this.height;      if (ball.x > paddleLeftWall &&        ball.x < paddleRightWall &&        ball.y > paddleTopWall &&        ball.y < paddleBottomWall) {        return true;      }      return false;    };    this.move = function(keyCode) {      var nextY = this.y;      if (keyCode == 40) {        nextY += 5;        this.speedModifer = 1.5;      } else if (keyCode == 38) {        nextY += -5;        this.speedModifier = 1.5;      } else {        this.speedModifier = 0;      }      nextY = nextY < 0 ? 0 : nextY;      nextY = nextY + this.height > 480 ? 480 - this.height : nextY;      this.y = nextY;    };  }  var player = new paddle(5, 200, 25, 100);  var ai = new paddle(610, 200, 25, 100);  var ball = {    x: 320,    y: 240,    radius: 7,    xSpeed: 2,    ySpeed: 0,    var playerscore = 0    var aiscore = 0    reverseX: function() {      this.xSpeed *= -1;    },    reverseY: function() {      this.ySpeed *= -1;    },
查看完整描述

1 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

錯(cuò)誤似乎出現(xiàn)在您的reset函數(shù)中 - 您已進(jìn)入新行而沒(méi)有連接該行的其余部分;


reset: function() {

        alert('The score is now '

        playerscore + ' to ' + aiscore);

        this.x = 20;

        this.y = 24 30;

        this.xSpeed = 2;

        this.ySpeed = 0;

        

    },

應(yīng)該


reset: function() {

        alert('The score is now ' + playerscore + ' to ' + aiscore);

        this.x = 20;

        this.y = 24 30;

        this.xSpeed = 2;

        this.ySpeed = 0;

    },

更新

發(fā)布此答案后,我發(fā)現(xiàn)了更多問(wèn)題;


this.y = 24 30;

應(yīng)為 24或30;


this.y = 24;

這些在你的球?qū)ο笾卸急诲e(cuò)誤地定義了;


var 玩家分?jǐn)?shù) = 0 var aiscore = 0


應(yīng)該


playerscore: 0,

aiscore: 0,

var canvas = document.getElementById("my");

var ctx = canvas.getContext("2d");


function paddle(x, y, width, height) {

    this.x = x;

    this.y = y;

    this.width = width;

    this.height = height;

    this.speedModifier = 0;

    this.hasCollidedWith = function(ball) {

        var paddleLeftWall = this.x;

        var paddleRightWall = this.x + this.width;

        var paddleTopWall = this.y;

        var paddleBottomWall = this.y + this.height;

        if (ball.x > paddleLeftWall &&

            ball.x < paddleRightWall &&

            ball.y > paddleTopWall &&

            ball.y < paddleBottomWall) {

            return true;

        }

        return false;

    };

    this.move = function(keyCode) {

        var nextY = this.y;

        if (keyCode == 40) {

            nextY += 5;

            this.speedModifer = 1.5;

        } else if (keyCode == 38) {

            nextY += -5;

            this.speedModifier = 1.5;

        } else {

            this.speedModifier = 0;

        }

        nextY = nextY < 0 ? 0 : nextY;

        nextY = nextY + this.height > 480 ? 480 - this.height : nextY;

        this.y = nextY;

    };

}

var player = new paddle(5, 200, 25, 100);

var ai = new paddle(610, 200, 25, 100);

var ball = {

    x: 320,

    y: 240,

    radius: 7,

    xSpeed: 2,

    ySpeed: 0,

    playerscore: 0,

    aiscore: 0,

    reverseX: function() {

        this.xSpeed *= -1;

    },

    reverseY: function() {

        this.ySpeed *= -1;

    },

    reset: function() {

        alert('The score is now ' + this.playerscore + ' to ' + this.aiscore);

        this.x = 20;

        this.y = 24;

        this.xSpeed = 2;

        this.ySpeed = 0;

        

    },

    isBouncing: function() {

        return ball.ySpeed != 0;

    },

    modifyXSpeedBy: function(modification) {

        modification = this.xSpeed < 0 ? modification * -1 : modification;

        var nextValue = this.xSpeed + modification;

        nextValue = Math.abs(nextValue) > 9 ? 9 : nextValue;

        this.xSpeed = nextValue;

    },

    modifyYSpeedBy: function(modification) {

        modification = this.ySpeed < 0 ? modification * -1 : modification;

        this.ySpeed += modification;

    }

};


function tick() {

    updateGame();

    draw()

    window.setTimeout("tick()", 1000 / 60);

}


function updateGame() {

    ball.x += ball.xSpeed;

    ball.y += ball.ySpeed;

    if (ball.x < 0) {

        ball.reset();

        ball.aiscore = ball.aiscore + 1;

        

    }

    if (ball.x > 640) {

        ball.reset();

        ball.playerscore = ball.playerscore + 1

        

    }

    if (ball.y <= 0 || ball.y >= 480) {

        ball.reverseY();

    }

    var collidedWithPlayer = player.hasCollidedWith(ball);

    var collidedWithAi = ai.hasCollidedWith(ball);

    if (collidedWithPlayer || collidedWithAi) {

        ball.reverseX();

        ball.modifyXSpeedBy(0.25);

        var speedUpValue = collidedWithPlayer ? player.speedModifier : ai.speedModifier;

        ball.modifyYSpeedBy(speedUpValue);

    }

    for (var keyCode in heldDown) {

        player.move(keyCode);

    }

    var aiMiddle = ai.y + (ai.height / 2);

    if (aiMiddle < ball.y) {

        ai.move(40);

    }

    if (aiMiddle > ball.y) {

        ai.move(38);

    }

    

}


function draw() {

    ctx.fillStyle = "black";

    ctx.fillRect(0, 0, 640, 480);

    renderPaddle(player);

    renderPaddle(ai);

    renderBall(ball);

}


function renderPaddle(paddle) {

    ctx.fillStyle = "blue";

    ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);

}


function renderBall(ball) {

    ctx.beginPath();

    ctx.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI, false);

    ctx.fillStyle = "pink";

    ctx.fill();

}

var heldDown = {};

window.addEventListener("keydown", function(keyInfo) {

    heldDown[event.keyCode] = true;

}, false);

window.addEventListener("keyup", function(keyInfo) {

    delete heldDown[event.keyCode];

}, false);

tick();

<canvas id='my' width='640' height='480'></canvas>


查看完整回答
反對(duì) 回復(fù) 2023-07-20
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)