/**** * Classes ****/ // Bullet class for shooting var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> // Dinosaur class representing the target var Dinosaur = Container.expand(function () { var self = Container.call(this); var dinosaurGraphics = self.attachAsset('dinosaur', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.x += self.speed; if (self.x > 2048) { self.x = 0; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var dinosaurs = []; var bullets = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var highScore = 0; var highScoreTxt = new Text2('High Score: 0', { size: 100, fill: "#ffffff" }); highScoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(highScoreTxt); highScoreTxt.y = 150; // Position below the score text // Function to spawn a dinosaur function spawnDinosaur() { var dinosaur = new Dinosaur(); dinosaur.x = Math.random() * 2048; dinosaur.y = Math.random() * 2732 / 2; dinosaurs.push(dinosaur); game.addChild(dinosaur); } // Function to shoot a bullet function shootBullet(x, y) { var bullet = new Bullet(); bullet.x = x; bullet.y = y; bullets.push(bullet); game.addChild(bullet); } // Handle game updates game.update = function () { for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); if (bullets[i].y < 0) { bullets[i].destroy(); bullets.splice(i, 1); } } for (var j = dinosaurs.length - 1; j >= 0; j--) { dinosaurs[j].update(); if (dinosaurs.length > 10) { LK.showGameOver(); highScoreTxt.setText('High Score: ' + highScore); return; } if (dinosaurs[j].x > 2048) { dinosaurs.splice(j, 1); } for (var k = bullets.length - 1; k >= 0; k--) { if (dinosaurs[j].intersects(bullets[k])) { score++; scoreTxt.setText(score); if (score > highScore) { highScore = score; highScoreTxt.setText('High Score: ' + highScore); } dinosaurs[j].destroy(); bullets[k].destroy(); dinosaurs.splice(j, 1); bullets.splice(k, 1); // Removed lowest score tracking break; } } } if (LK.ticks % 120 == 0) { spawnDinosaur(); } }; // Handle touch events for shooting game.down = function (x, y, obj) { shootBullet(x, y); };
/****
* Classes
****/
// Bullet class for shooting
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
// Dinosaur class representing the target
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinosaurGraphics = self.attachAsset('dinosaur', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.x += self.speed;
if (self.x > 2048) {
self.x = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var dinosaurs = [];
var bullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var highScore = 0;
var highScoreTxt = new Text2('High Score: 0', {
size: 100,
fill: "#ffffff"
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 150; // Position below the score text
// Function to spawn a dinosaur
function spawnDinosaur() {
var dinosaur = new Dinosaur();
dinosaur.x = Math.random() * 2048;
dinosaur.y = Math.random() * 2732 / 2;
dinosaurs.push(dinosaur);
game.addChild(dinosaur);
}
// Function to shoot a bullet
function shootBullet(x, y) {
var bullet = new Bullet();
bullet.x = x;
bullet.y = y;
bullets.push(bullet);
game.addChild(bullet);
}
// Handle game updates
game.update = function () {
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < 0) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
for (var j = dinosaurs.length - 1; j >= 0; j--) {
dinosaurs[j].update();
if (dinosaurs.length > 10) {
LK.showGameOver();
highScoreTxt.setText('High Score: ' + highScore);
return;
}
if (dinosaurs[j].x > 2048) {
dinosaurs.splice(j, 1);
}
for (var k = bullets.length - 1; k >= 0; k--) {
if (dinosaurs[j].intersects(bullets[k])) {
score++;
scoreTxt.setText(score);
if (score > highScore) {
highScore = score;
highScoreTxt.setText('High Score: ' + highScore);
}
dinosaurs[j].destroy();
bullets[k].destroy();
dinosaurs.splice(j, 1);
bullets.splice(k, 1);
// Removed lowest score tracking
break;
}
}
}
if (LK.ticks % 120 == 0) {
spawnDinosaur();
}
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
shootBullet(x, y);
};