गेम उछाल

यह लाल वर्ग जब जमीन पर आता है तो उठता है:


रिफ्लैक्शन

हमें जोड़ना है एक और कार्यक्षमता है bounce प्रकृति。

bounce प्रकृति इंगित करती है कि गुरुत्वाकर्षण के कारण जब सामग्री जमीन पर आती है तो सामग्री को रिफ्लैक्शन होगा या नहीं।

bounce प्रकृति मान को अंक होना चाहिए।0 कोई भी रिफ्लैक्शन नहीं करता, 1 यह सामग्री को वापस प्रारंभिक गिरने की स्थिति में रिफ्लैक्शन करेगा।

उदाहरण

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.1;
  this.gravitySpeed = 0;
  this.bounce = 0.6;
  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;
    this.hitBottom();
  }
  this.hitBottom = function() {
    var राकबोटम = this.gamearea.canvas.height - this.height;
    if (this.y > राकबोटम) {
      this.y = राकबोटम;
      this.gravitySpeed = -(this.gravitySpeed * this.bounce);
    }
  }
}

स्वयं प्रयास कीजिए