Javascript - 为什么我的 while 循环不执行?(Pong)

问题描述 投票:0回答:1

我是业余的 :) 对不起,如果这看起来像一个愚蠢的问题,但为什么我的while循环没有执行?它应该画一个粉红色的矩形和两个黑色的矩形作为桨。P.S.是的,我知道i<10没有意义,但它是以后的一个占位符,应该不会影响代码的运行。总之,这是我的代码------------------------------------------------。

    //set up the javascript canvas
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");

var i = 1;

//Game loop
while (i < 10) {
  //register pressed keys
  //var x = event.key;

  if (window.event) {
    var keynum = event.keyCode;
  } else if (event.which) {
    var keynum = event.which;
  }

  //draw the pink background
  ctx.beginPath();
  ctx.rect(0, 0, 315, 100);
  ctx.fillStyle = "#FFC0CB";

  ctx.fill();
  ctx.closePath();

  //Draw the player's paddle
  ctx.beginPath();
  var playerY = 35;
  ctx.rect(15, playerY, 10, 25);
  ctx.fillStyle = "#000000";
  ctx.fill();
  ctx.closePath();

  //Draw the bot's paddle
  ctx.beginPath();
  var botY = 35;
  ctx.rect(280, botY, 10, 25);
  ctx.fill();
  ctx.closePath();
  i++;
}
javascript loops while-loop pong
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.