制作JS游戏,将其他对象添加到代码中

圣经

我有一个游戏概念,并且使用w3学校,我已经在画布的底部构建了一个用于移动游戏的基本代码。我想开发一款与W3游戏计划不同的游戏。我并不是在实际编写代码时寻求帮助,而是在代码中添加其他变量/对象的地方寻求帮助。这是我当前的代码。

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <style>
    canvas {
      border: 5px solid #d3d3d3;
      background-color: #f1f1f1;
    }
  </style>
</head>

<body onload="startGame()">
  <script>
    var myGamePiece;

    function startGame() {
      myGamePiece = new component(30, 30, "black", 350, 445);
      myGameArea.start();
    }

    var myGameArea = {
      canvas: document.createElement("canvas"),
      start: function() {
        this.canvas.width = 700;
        this.canvas.height = 480;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function(e) {
          myGameArea.key = e.keyCode;
        }) 
        window.addEventListener('keyup', function(e) {
          myGameArea.key = false;
        })
      },
      clear: function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
      }
    }

    function component(width, height, color, x, y) {
      this.width = width;
      this.height = height;
      this.speedX = 0;
      this.speedY = 0;
      this.x = x;
      this.y = y;
      this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
      }
      this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
      }
    }

    function updateGameArea() {
      myGameArea.clear();
      myGamePiece.speedX = 0;
      myGamePiece.speedY = 0;
      if (myGameArea.key && myGameArea.key == 37) {
        myGamePiece.speedX = -1;
      }
      if (myGameArea.key && myGameArea.key == 39) {
        myGamePiece.speedX = 1;
      }
      myGamePiece.newPos();
      myGamePiece.update();
    }
  </script>
</body>

</html>

任何帮助将不胜感激,谢谢。

对于有兴趣的人,我的计划是让不同的物体从顶部掉落,当您抓住它们时,每个物体都会对您的播放器或背景/音乐产生不同的影响。

清除棕褐色

在该代码上添加其他对象,您将需要在两个位置插入代码:

  • 函数startGame
    这是初始化所有对象变量的地方
  • 函数updateGameArea
    这是将对象实际绘制到画布中的位置

因此,让我们跳到一个例子:

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <style>
    canvas {
      border: 5px solid #d3d3d3;
      background-color: #f1f1f1;
    }
  </style>
</head>

<body onload="startGame()">
  <script>
    var myGamePiece;

    function startGame() {
      dots = []
      for (i = 0; i < 45; i++) {
        a = i*8 * Math.PI / 180
        dots.push(new component(2, 2, "red", Math.sin(a)*45 + 80, Math.cos(a)*35 + 60)) 
      }
      myGamePiece = new component(30, 30, "black", 350, 125);
      myGameArea.start();
    }

    var myGameArea = {
      canvas: document.createElement("canvas"),
      start: function() {
        this.canvas.width = 600;
        this.canvas.height = 160;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function(e) {
          myGameArea.key = e.keyCode;
        });        
        window.addEventListener('keyup', function(e) {
          myGameArea.key = false;
        });
      },
      clear: function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
      }
    }

    function component(width, height, color, x, y) {
      this.width = width;
      this.height = height;
      this.speedX = 0;
      this.speedY = 0;
      this.x = x;
      this.y = y;
      this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
      }
      this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
      }
    }

    function updateGameArea() {
      myGameArea.clear();
      myGamePiece.speedX = 0;
      myGamePiece.speedY = 0;
      if (myGameArea.key && myGameArea.key == 37) {
        myGamePiece.speedX = -1;
      }
      if (myGameArea.key && myGameArea.key == 39) {
        myGamePiece.speedX = 1;
      }
      myGamePiece.newPos();
      myGamePiece.update();
      dots.forEach(x => x.update());
    }
  </script>
</body>

</html>

  • 函数startGame
    dots = []...在椭圆形图案上仅添加了一些“点”
  • 函数updateGameArea
    在这里,我们只调用对象的更新:
    dots.forEach(x => x.update());

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将其他文件夹中的功能添加到此文件夹中的对象

来自分类Dev

如何将其他数据传递给将事物添加到对象的函数?

来自分类Dev

将其他联接添加到SQL中已联接的表中

来自分类Dev

将其他行内容添加到.txt

来自分类Dev

将其他数据添加到图谱点

来自分类Dev

JMeter-将其他SSLCipher添加到jmeter

来自分类Dev

如何将其他值添加到DateEntry?

来自分类Dev

UpdateExpression:将其他属性的值添加到列表

来自分类Dev

将其他信息添加到“ visNetwork”

来自分类Dev

将其他行内容添加到.txt

来自分类Dev

将其他链接添加到响应式导航

来自分类Dev

使用画布将其他组件添加到JFrame

来自分类Dev

如何将其他参数添加到我的Ajax回调中?

来自分类Dev

搜索arraylist,从arraylist中删除项目,将其添加到其他位置

来自分类Dev

将其他类中的列表添加到C#时触发事件

来自分类Dev

使用片段将其他元素(不是ListView)添加到导航抽屉中

来自分类Dev

将其他数据添加到$ form.serialize()Ajax发布中?

来自分类Dev

在JSSOR中,如何将其他幻灯片添加到全宽滑块?

来自分类Dev

WCF将其他HTTP标头添加到HTTP响应中以传输SOAP消息

来自分类Dev

如何将其他代理添加到中层DC / OS集群中?

来自分类Dev

将其他值添加到定义为数组的Typescript JSON密钥中

来自分类Dev

猫鼬将其他集合添加到两个聚合集合中

来自分类Dev

在JSSOR中,如何将其他幻灯片添加到全宽滑块?

来自分类Dev

如何将其他参数添加到我的Ajax回调中?

来自分类Dev

将其他文本添加到SQL中的现有字符串?

来自分类Dev

如何通过Sass中的嵌套将其他信息添加到属性选择器?

来自分类Dev

遍历数组并将其他信息添加到PHP中的项

来自分类Dev

将对象添加到数组中并更改该数组中其他对象的属性

来自分类Dev

可以在coffeescript / javascript中将其他对象属性动态添加到现有对象吗?

Related 相关文章

  1. 1

    将其他文件夹中的功能添加到此文件夹中的对象

  2. 2

    如何将其他数据传递给将事物添加到对象的函数?

  3. 3

    将其他联接添加到SQL中已联接的表中

  4. 4

    将其他行内容添加到.txt

  5. 5

    将其他数据添加到图谱点

  6. 6

    JMeter-将其他SSLCipher添加到jmeter

  7. 7

    如何将其他值添加到DateEntry?

  8. 8

    UpdateExpression:将其他属性的值添加到列表

  9. 9

    将其他信息添加到“ visNetwork”

  10. 10

    将其他行内容添加到.txt

  11. 11

    将其他链接添加到响应式导航

  12. 12

    使用画布将其他组件添加到JFrame

  13. 13

    如何将其他参数添加到我的Ajax回调中?

  14. 14

    搜索arraylist,从arraylist中删除项目,将其添加到其他位置

  15. 15

    将其他类中的列表添加到C#时触发事件

  16. 16

    使用片段将其他元素(不是ListView)添加到导航抽屉中

  17. 17

    将其他数据添加到$ form.serialize()Ajax发布中?

  18. 18

    在JSSOR中,如何将其他幻灯片添加到全宽滑块?

  19. 19

    WCF将其他HTTP标头添加到HTTP响应中以传输SOAP消息

  20. 20

    如何将其他代理添加到中层DC / OS集群中?

  21. 21

    将其他值添加到定义为数组的Typescript JSON密钥中

  22. 22

    猫鼬将其他集合添加到两个聚合集合中

  23. 23

    在JSSOR中,如何将其他幻灯片添加到全宽滑块?

  24. 24

    如何将其他参数添加到我的Ajax回调中?

  25. 25

    将其他文本添加到SQL中的现有字符串?

  26. 26

    如何通过Sass中的嵌套将其他信息添加到属性选择器?

  27. 27

    遍历数组并将其他信息添加到PHP中的项

  28. 28

    将对象添加到数组中并更改该数组中其他对象的属性

  29. 29

    可以在coffeescript / javascript中将其他对象属性动态添加到现有对象吗?

热门标签

归档