如何以“数据字典”方式编写此代码?

时间:2018-10-22 19:04:27

标签: javascript dictionary

因此,我得到了一个类似子手的游戏,该游戏将输出一些画布绘图,但看起来我的代码非常长。同时,有人告诉我制作一个具有所有功能的对象和字典,但我真的不知道怎么做。这是代码

const failDraw = function() {
    let drawMe = lives;
    drawArray[drawMe]();
  };

  const canvas = function() {
    myStickman = document.getElementById("stickman");
    context = myStickman.getContext("2d");
    context.beginPath();
  };

  const head = function() {
    myStickman = document.getElementById("stickman");
    context = myStickman.getContext("2d");
    context.beginPath();
    context.arc(60, 25, 10, 0, Math.PI * 2, true);
    context.stroke();
  };

  const draw = function(x, y, x2, y2) {
    context.moveTo(x, y);
    context.lineTo(x2, y2);
    context.stroke();
  };

  const frame1 = function() {
    draw(0, 150, 150, 150);
  };

  const frame2 = function() {
    draw(10, 0, 10, 600);
  };

  const frame3 = function() {
    draw(0, 5, 70, 5);
  };

  const frame4 = function() {
    draw(60, 5, 60, 15);
  };

  const torso = function() {
    draw(60, 36, 60, 70);
  };

  const rightArm = function() {
    draw(60, 46, 100, 50);
  };

  const leftArm = function() {
    draw(60, 46, 20, 50);
  };

  const rightLeg = function() {
    draw(60, 70, 100, 100);
  };

  const leftLeg = function() {
    draw(60, 70, 20, 100);
  };

  const drawArray = [
    rightLeg,
    leftLeg,
    rightArm,
    leftArm,
    torso,
    head,
    frame4,
    frame3,
    frame2,
    frame1
  ];

faildraw在代码的最后一个函数数组中进行下一步。

谢谢!

0 个答案:

没有答案