/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Broccoli = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('broccoli', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 200;
self.maxHealth = 200;
self.shootCooldown = 0;
self.moveTimer = 0;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
self.moveTimer++;
if (self.moveTimer >= 60) {
self.moveTimer = 0;
self.targetX = steak.x;
self.targetY = steak.y;
}
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
if (self.shootCooldown <= 0 && distance < 300) {
self.shoot();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.health = 0;
}
LK.effects.flashObject(self, 0xFF0000, 300);
};
self.shoot = function () {
var bullet = new BroccoliBullet();
bullet.x = self.x;
bullet.y = self.y;
var dx = steak.x - self.x;
var dy = steak.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
bullet.directionX = dx / distance;
bullet.directionY = dy / distance;
broccoliBullets.push(bullet);
game.addChild(bullet);
self.shootCooldown = 45;
LK.getSound('shoot').play();
};
return self;
});
var BroccoliBullet = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('broccoliBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6;
self.directionX = 0;
self.directionY = 0;
self.update = function () {
self.x += self.directionX * self.speed;
self.y += self.directionY * self.speed;
};
return self;
});
var Steak = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('steak', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.health = 100;
self.maxHealth = 100;
self.shootCooldown = 0;
self.update = function () {
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.health = 0;
}
LK.effects.flashObject(self, 0xFF0000, 300);
};
self.shoot = function (targetX, targetY) {
if (self.shootCooldown <= 0) {
var bullet = new SteakBullet();
bullet.x = self.x;
bullet.y = self.y;
var dx = targetX - self.x;
var dy = targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
bullet.directionX = dx / distance;
bullet.directionY = dy / distance;
steakBullets.push(bullet);
game.addChild(bullet);
self.shootCooldown = 15;
LK.getSound('shoot').play();
}
};
return self;
});
var SteakBullet = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('steakBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.directionX = 0;
self.directionY = 0;
self.update = function () {
self.x += self.directionX * self.speed;
self.y += self.directionY * self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var steak = game.addChild(new Steak());
var broccoli = game.addChild(new Broccoli());
var steakBullets = [];
var broccoliBullets = [];
steak.x = 1024;
steak.y = 1800;
broccoli.x = 1024;
broccoli.y = 600;
var healthBarBg = game.addChild(LK.getAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0
}));
healthBarBg.x = 1024;
healthBarBg.y = 100;
var healthBar = game.addChild(LK.getAsset('healthBar', {
anchorX: 0.5,
anchorY: 0
}));
healthBar.x = 1024;
healthBar.y = 100;
var broccoliHealthBarBg = game.addChild(LK.getAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0
}));
broccoliHealthBarBg.x = 1024;
broccoliHealthBarBg.y = 150;
var broccoliHealthBar = game.addChild(LK.getAsset('healthBar', {
anchorX: 0.5,
anchorY: 0,
color: 0x32CD32
}));
broccoliHealthBar.x = 1024;
broccoliHealthBar.y = 150;
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var movingUp = false;
var movingDown = false;
var movingLeft = false;
var movingRight = false;
game.down = function (x, y, obj) {
steak.shoot(x, y);
};
game.move = function (x, y, obj) {
var dx = x - steak.x;
var dy = y - steak.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 10) {
steak.x += dx / distance * steak.speed;
steak.y += dy / distance * steak.speed;
if (steak.x < 40) steak.x = 40;
if (steak.x > 2008) steak.x = 2008;
if (steak.y < 40) steak.y = 40;
if (steak.y > 2692) steak.y = 2692;
}
};
game.update = function () {
if (broccoli.x < 35) broccoli.x = 35;
if (broccoli.x > 2013) broccoli.x = 2013;
if (broccoli.y < 35) broccoli.y = 35;
if (broccoli.y > 2697) broccoli.y = 2697;
for (var i = steakBullets.length - 1; i >= 0; i--) {
var bullet = steakBullets[i];
if (bullet.x < -50 || bullet.x > 2098 || bullet.y < -50 || bullet.y > 2782) {
bullet.destroy();
steakBullets.splice(i, 1);
continue;
}
if (bullet.intersects(broccoli)) {
broccoli.takeDamage(10);
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('hit').play();
bullet.destroy();
steakBullets.splice(i, 1);
if (broccoli.health <= 0) {
LK.showYouWin();
return;
}
}
}
for (var j = broccoliBullets.length - 1; j >= 0; j--) {
var bBullet = broccoliBullets[j];
if (bBullet.x < -50 || bBullet.x > 2098 || bBullet.y < -50 || bBullet.y > 2782) {
bBullet.destroy();
broccoliBullets.splice(j, 1);
continue;
}
if (bBullet.intersects(steak)) {
steak.takeDamage(15);
LK.getSound('hit').play();
bBullet.destroy();
broccoliBullets.splice(j, 1);
if (steak.health <= 0) {
LK.showGameOver();
return;
}
}
}
healthBar.width = steak.health / steak.maxHealth * 200;
broccoliHealthBar.width = broccoli.health / broccoli.maxHealth * 200;
if (steak.intersects(broccoli)) {
steak.takeDamage(1);
if (steak.health <= 0) {
LK.showGameOver();
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,257 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Broccoli = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('broccoli', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2;
+ self.health = 200;
+ self.maxHealth = 200;
+ self.shootCooldown = 0;
+ self.moveTimer = 0;
+ self.targetX = 0;
+ self.targetY = 0;
+ self.update = function () {
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ self.moveTimer++;
+ if (self.moveTimer >= 60) {
+ self.moveTimer = 0;
+ self.targetX = steak.x;
+ self.targetY = steak.y;
+ }
+ var dx = self.targetX - self.x;
+ var dy = self.targetY - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 5) {
+ self.x += dx / distance * self.speed;
+ self.y += dy / distance * self.speed;
+ }
+ if (self.shootCooldown <= 0 && distance < 300) {
+ self.shoot();
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ if (self.health <= 0) {
+ self.health = 0;
+ }
+ LK.effects.flashObject(self, 0xFF0000, 300);
+ };
+ self.shoot = function () {
+ var bullet = new BroccoliBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ var dx = steak.x - self.x;
+ var dy = steak.y - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ bullet.directionX = dx / distance;
+ bullet.directionY = dy / distance;
+ broccoliBullets.push(bullet);
+ game.addChild(bullet);
+ self.shootCooldown = 45;
+ LK.getSound('shoot').play();
+ };
+ return self;
+});
+var BroccoliBullet = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('broccoliBullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 6;
+ self.directionX = 0;
+ self.directionY = 0;
+ self.update = function () {
+ self.x += self.directionX * self.speed;
+ self.y += self.directionY * self.speed;
+ };
+ return self;
+});
+var Steak = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('steak', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.health = 100;
+ self.maxHealth = 100;
+ self.shootCooldown = 0;
+ self.update = function () {
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ if (self.health <= 0) {
+ self.health = 0;
+ }
+ LK.effects.flashObject(self, 0xFF0000, 300);
+ };
+ self.shoot = function (targetX, targetY) {
+ if (self.shootCooldown <= 0) {
+ var bullet = new SteakBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ var dx = targetX - self.x;
+ var dy = targetY - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ bullet.directionX = dx / distance;
+ bullet.directionY = dy / distance;
+ steakBullets.push(bullet);
+ game.addChild(bullet);
+ self.shootCooldown = 15;
+ LK.getSound('shoot').play();
+ }
+ };
+ return self;
+});
+var SteakBullet = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('steakBullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 8;
+ self.directionX = 0;
+ self.directionY = 0;
+ self.update = function () {
+ self.x += self.directionX * self.speed;
+ self.y += self.directionY * self.speed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var steak = game.addChild(new Steak());
+var broccoli = game.addChild(new Broccoli());
+var steakBullets = [];
+var broccoliBullets = [];
+steak.x = 1024;
+steak.y = 1800;
+broccoli.x = 1024;
+broccoli.y = 600;
+var healthBarBg = game.addChild(LK.getAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0
+}));
+healthBarBg.x = 1024;
+healthBarBg.y = 100;
+var healthBar = game.addChild(LK.getAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0
+}));
+healthBar.x = 1024;
+healthBar.y = 100;
+var broccoliHealthBarBg = game.addChild(LK.getAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0
+}));
+broccoliHealthBarBg.x = 1024;
+broccoliHealthBarBg.y = 150;
+var broccoliHealthBar = game.addChild(LK.getAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0,
+ color: 0x32CD32
+}));
+broccoliHealthBar.x = 1024;
+broccoliHealthBar.y = 150;
+var scoreTxt = new Text2('Score: 0', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+var movingUp = false;
+var movingDown = false;
+var movingLeft = false;
+var movingRight = false;
+game.down = function (x, y, obj) {
+ steak.shoot(x, y);
+};
+game.move = function (x, y, obj) {
+ var dx = x - steak.x;
+ var dy = y - steak.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 10) {
+ steak.x += dx / distance * steak.speed;
+ steak.y += dy / distance * steak.speed;
+ if (steak.x < 40) steak.x = 40;
+ if (steak.x > 2008) steak.x = 2008;
+ if (steak.y < 40) steak.y = 40;
+ if (steak.y > 2692) steak.y = 2692;
+ }
+};
+game.update = function () {
+ if (broccoli.x < 35) broccoli.x = 35;
+ if (broccoli.x > 2013) broccoli.x = 2013;
+ if (broccoli.y < 35) broccoli.y = 35;
+ if (broccoli.y > 2697) broccoli.y = 2697;
+ for (var i = steakBullets.length - 1; i >= 0; i--) {
+ var bullet = steakBullets[i];
+ if (bullet.x < -50 || bullet.x > 2098 || bullet.y < -50 || bullet.y > 2782) {
+ bullet.destroy();
+ steakBullets.splice(i, 1);
+ continue;
+ }
+ if (bullet.intersects(broccoli)) {
+ broccoli.takeDamage(10);
+ LK.setScore(LK.getScore() + 10);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ LK.getSound('hit').play();
+ bullet.destroy();
+ steakBullets.splice(i, 1);
+ if (broccoli.health <= 0) {
+ LK.showYouWin();
+ return;
+ }
+ }
+ }
+ for (var j = broccoliBullets.length - 1; j >= 0; j--) {
+ var bBullet = broccoliBullets[j];
+ if (bBullet.x < -50 || bBullet.x > 2098 || bBullet.y < -50 || bBullet.y > 2782) {
+ bBullet.destroy();
+ broccoliBullets.splice(j, 1);
+ continue;
+ }
+ if (bBullet.intersects(steak)) {
+ steak.takeDamage(15);
+ LK.getSound('hit').play();
+ bBullet.destroy();
+ broccoliBullets.splice(j, 1);
+ if (steak.health <= 0) {
+ LK.showGameOver();
+ return;
+ }
+ }
+ }
+ healthBar.width = steak.health / steak.maxHealth * 200;
+ broccoliHealthBar.width = broccoli.health / broccoli.maxHealth * 200;
+ if (steak.intersects(broccoli)) {
+ steak.takeDamage(1);
+ if (steak.health <= 0) {
+ LK.showGameOver();
+ }
+ }
+};
\ No newline at end of file