Gravity ng Game

Mayroong ilang mga laro na may kapangyarihan na ilihis ang mga komponen ng laro sa isang direksyon, halimbawa, ang grabiti ay ilihis ang mga bagay papunta sa lupa.


重力

Kung gusto mong idagdag ang katangian sa aming constructor ng komponen, magdagdag muna ng isang gravity Atributo, ang pagtatakda ng kasalukuyang grabiti. Pagkatapos, magdagdag ng isang gravitySpeed Mga katangian, kapag mayroon palagiang pag-update ng frame, ito ay papataas:

Examples

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

Try it yourself

Tapusin sa ibaba

Upang maiwasan na palaging mababa ang pula na bato, dapat natin ihinto ang pagbaba nito kapag umabot ito sa ibaba ng larong laro:

Examples

  this.newPos = function() {
    this.gravitySpeed += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY + this.gravitySpeed;
    this.hitBottom();
  }
  this.hitBottom = function() {
    var rockbottom = myGameArea.canvas.height - this.height;
    if (this.y > rockbottom) {
      this.y = rockbottom;
    }
  }

Try it yourself

Pabilisin

Sa laro, kapag may puwersa na hinahagis pababa ang bato, dapat mong idisenyo ng isang paraan upang mapabilisin ang komponente.

Kapag may isang button na naka-click, mag-activate ng function upang palipad ang pula na bato sa himpapawid:

Examples

<script>
function accelerate(n) {
  myGamePiece.gravity = n;
}
</script>
<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.1)">Pabilisin</button>

Try it yourself

Isang laro

Create a game based on what we have learned so far:

Examples

Try it yourself

Click the boost button to start the game.

How long can you live? Use the boost button to stay in the air.