HTML图形-HTML游戏:合谋控制(破坏和得分)

时间:2019-01-25 10:27:53

标签: javascript html5 html5-canvas

我是第一次实践HTML游戏。 仅以https://www.w3schools.com/graphics/game_intro.asp作为我的唯一指南,我设法完成了自己的游戏。 游戏非常简单:随机物体(绿色方块)从上方掉落。播放器(红色方块)左右移动。游戏中的主要目标是让玩家收集/碰撞掉掉的物体。每次与1个对象发生碰撞,玩家将获得1分。 现在,我该如何消灭与玩家碰撞的物体(绿色方块)并一次设置1点?

这是代码:

<!DOCTYPE html>
<html>
<head>
  <title>Food Game v1</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
    border:1px solid #d3d3d3;
    background-image: linear-gradient(#FFFDF0, white);
}

  /* buttons align in center*/
  .center {
  margin: auto;
  width: 90%;
  padding: 10px;
}
</style>
</head>
<body onload="startGame()">
<script>

var myGamePiece;
var myFallenObj_ = []; //array of fallen objects
var myScore;

function startGame() {
  myGamePiece = new component(30, 30, "red", 130, 300);
  myScore = new component("10px", "Consolas", "black", 100, 345,"text");
  myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 300;
        this.canvas.height = 350;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.frameNo = 0; //if we use an array
        this.interval = setInterval(updateGameArea, 20);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    },
    stop: function(){
        clearInterval(this.interval);
    }
}

function component(width, height, color, x, y, type) {
    this.type = type;
    this.width = width;
    this.height = height;
    this.score = 0;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
        ctx = myGameArea.context;
        if(this.type == "text"){ //check on text object
          ctx.font = this.width + " " + this.height;
          ctx.fillStyle = color;
          ctx.fillText(this.text, this.x, this.y);
        }else {
          ctx.fillStyle = color;
          ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
    this.crashWith = function(otherobj){
        var myleft = this.x;
        var myright = this.x + (this.width);
        var mytop = this.y;
        var mybottom = this.y + (this.height);
        var otherleft = otherobj.x;
        var otherright = otherobj.x + (otherobj.width);
        var othertop = otherobj.y;
        var otherbottom = otherobj.y + (otherobj.height);
        var crash = true;
        if((myright < otherleft) || (myleft > otherright) || (mybottom < othertop) || (mytop > otherbottom)) {
            crash = false;
        }
        return crash;
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
    var width_, minWidth_, maxWidth_;
    var x,y;
    
    for (i=0;i<myFallenObj_.length; i++){
      if(myGamePiece.crashWith(myFallenObj_[i])){ //if collision happens
        myScore.score ++;
        //destroy(myFallenObj_[i]);
      }
    }

    myScore.text = "SCORE: " + myScore.score;
    //check myGamePiece not go over the left border
    if(myGamePiece.x <= 0)
      myGamePiece.x = 1;
    //check myGamePiece not go over the right border
    if(myGamePiece.x > 265)
      myGamePiece.x = 260;

    myScore.update();

    myGameArea.frameNo +=1;
    if (myGameArea.frameNo == 1 || everyinterval(150)){
      //x = 10;
      minWidth_=0;
      maxWidth_=300;
      width_ = Math.floor(Math.random()*(maxWidth_-minWidth_+1)+minWidth_);
      y = - 50;
      myFallenObj_.push(new component (30,30,"green", width_, y));
    }
    for (i=0; i<myFallenObj_.length; i++){
      myFallenObj_[i].x +=0;
      myFallenObj_[i].y +=1;
      myFallenObj_[i].update();
    }
   
}

function everyinterval(n){
  if((myGameArea.frameNo /n) % 1 == 0)
    return true;
  return false;
}

function moveleft() {
    myGamePiece.speedX = - 2;

}

function moveright() {
    myGamePiece.speedX = 2;

}

function clearmove() {
    myGamePiece.speedX = 0;
    myGamePiece.speedY = 0;
}
</script>
<div class="center">

  <button onmousedown="moveleft()" onmouseup="clearmove()" ontouchstart="moveleft()">LEFT</button>
  <button onmousedown="moveright()" onmouseup="clearmove()" ontouchstart="moveright()">RIGHT</button><br><br>

</div>

</body>
</html>

到目前为止,我所做的就是应用该碰撞并计算分数,但是只要红色和绿色不断重复触摸自己,分数就很重要(这是错误的)。

任何建议都值得欢迎,谢谢!

2 个答案:

答案 0 :(得分:0)

scorable属性添加到您的component

function component(...) {
...
this.scorable = true;
...
}

然后使用该属性标记它是否仍为scorable(如果不跳过的话):

for (i=0;i<myFallenObj_.length; i++){
  if(myGamePiece.crashWith(myFallenObj_[i]) && myFallenObj_[i].scorable){ //if collision happens
    myScore.score ++;

    myFallenObj_[i].scorable = false;
    //destroy(myFallenObj_[i]);
  } 
}

答案 1 :(得分:0)

由于@ACD,我解决了我有关积分的问题的一半。 我的答案的另一半是如何处理我的(绿色)组件:当绿色组件崩溃到红色组件然后消失时,请理解这一点。 好吧,我无法正确处理对象的destroy属性(在本例中为我的组件)。我想Unity有太多JS,我想:p。我不知道HTML Canvas的哲学是否有这样的属性。为了以某种方式“应用” destroy属性来解决我的问题(以使我的绿色组件从画布上消失),我必须首先创建一个“幻想”。基本上,使用组件在画布上的定位。 主要是,我必须设置一个变量var garbagePOS = 2000; //画布尺寸之外的任何可能的度量。 然后,我将崩溃的绿色组件的位置x设置为新的位置等于2000(从视觉上看,不属于我的内容)myFallenObj_[i].x = garbagePOS;就像这样,它会产生错觉。可能不是理想的解决方案。但是目前对我来说很好。还有其他建议吗?