/****
* Classes
****/
// Define the Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
});
// Define the Monster class
var Monster = Container.expand(function () {
var self = Container.call(this);
var monsterGraphics = self.attachAsset('monster', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Monster update logic
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
// Define the Score class
var Score = Container.expand(function () {
var self = Container.call(this);
var scoreText = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
self.addChild(scoreText);
self.update = function () {
scoreText.setText(score);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 2400; // Near the bottom
var monster = game.addChild(new Monster());
monster.x = 1024; // Center horizontally
monster.y = 200; // Near the top
var bullets = [];
var score = 0;
var scoreDisplay = game.addChild(new Score());
scoreDisplay.x = 1024; // Center horizontally
scoreDisplay.y = 50; // Near the top
// Function to handle shooting
function shootBullet() {
var newBullet = new Bullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
// Function to handle hero movement
function moveHero(x, y) {
hero.x = x;
hero.y = y;
}
// Game update loop
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Check for collisions
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].intersects(monster)) {
score += 10;
bullets[j].destroy();
bullets.splice(j, 1);
// Flash monster to indicate hit
LK.effects.flashObject(monster, 0xff0000, 500);
}
}
// Update monster
monster.update();
if (hero.intersects(monster)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
// Handle touch events for hero movement
game.down = function (x, y, obj) {
moveHero(x, y);
};
game.move = function (x, y, obj) {
moveHero(x, y);
};
game.up = function (x, y, obj) {
// Optionally handle touch release
};
// Set interval for shooting bullets
var shootInterval = LK.setInterval(shootBullet, 500); ===================================================================
--- original.js
+++ change.js
@@ -42,8 +42,20 @@
self.y = 0;
}
};
});
+// Define the Score class
+var Score = Container.expand(function () {
+ var self = Container.call(this);
+ var scoreText = new Text2('0', {
+ size: 150,
+ fill: 0xFFFFFF
+ });
+ self.addChild(scoreText);
+ self.update = function () {
+ scoreText.setText(score);
+ };
+});
/****
* Initialize Game
****/
@@ -62,8 +74,11 @@
monster.x = 1024; // Center horizontally
monster.y = 200; // Near the top
var bullets = [];
var score = 0;
+var scoreDisplay = game.addChild(new Score());
+scoreDisplay.x = 1024; // Center horizontally
+scoreDisplay.y = 50; // Near the top
// Function to handle shooting
function shootBullet() {
var newBullet = new Bullet();
newBullet.x = hero.x;