حرکت بازی

کورس کی سفارش:


物体 کس طرح چل سکتا ہے؟

component 构造函数中添加一个 speed 属性,该属性代表组件当前的速度。

还要对 newPos() 方法进行一些更改,以根据 speedangle 计算组件的位置。

默认情况下,组件面朝上,通过将 speed ویژگی کو 1 رکھیں، عناصر جاری طور پر آگے چلنا شروع ہوگا。

مثال

function component(width, height, color, x, y) {
  this.gamearea = gamearea;
  this.width = width;
  this.height = height;
  this.angle = 0;
  this.speed = 1;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.angle);
    ctx.fillStyle = color;
    ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
    ctx.restore();
  }
  this.newPos = function() {
    this.x += this.speed * Math.sin(this.angle);
    this.y -= this.speed * Math.cos(this.angle);
  }
}

به طور مستقیم امتحان کنید

چرخنا

مجھم چپ اور درست چرخنا چاہیئے۔ ایک نا نام چرخنا بنائیں moveAngle کوئی نئی ویژگی، جو موجودہ حرکت کی قیمتیں یا گردش کی قیمتیں مظاہر کرتی ہے، میں newPos() میں moveAngle ویژگی حساب angle:

مثال

پرسش: moveangle کی ویژگی کو 1 رکھیں، دیکھیئے کیا ہوگا:

function component(width, height, color, x, y) {
  this.width = width;
  this.height = height;
  this.angle = 0;
  this.moveAngle = 1;
  this.speed = 1;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.angle);
    ctx.fillStyle = color;
    ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
    ctx.restore();
  }
  this.newPos = function() {
    this.angle += this.moveAngle * Math.PI / 180;
    this.x += this.speed * Math.sin(this.angle);
    this.y -= this.speed * Math.cos(this.angle);
  }
}

به طور مستقیم امتحان کنید

آپ کیبورد استفادہ کرتے ہیں

آپ کیبورد استفادہ کرتے ہوئے چٹا مربع کس طرح چل سکتا ہے؟ جب آپ 'بالائی' شانی استفادہ کرتے ہیں تو چٹا مربع بالائی اور نیچل نہیں چل سکتا، بلکہ ایک ساتھ ساتھ سمت تبدیل کرکے آگے چل سکتا ہے اور جب آپ چپ اور درست شانی استفادہ کرتے ہیں تو چپ اور درست جان سکتا ہے۔

مثال

به طور مستقیم امتحان کنید

مطمئن شوید که منطقه بازی تمرکز دارد، سپس از کلیدهای اژدها برای حرکت مربع قرمز استفاده کنید.