/**** * Classes ****/ // Cactus class var Cactus = Container.expand(function () { var self = Container.call(this); var cactusGraphics = self.attachAsset('cactus', { anchorX: 0.5, anchorY: 1.0 }); self.speed = -5; self.move = function () { self.x += self.speed; }; }); // Assets will be automatically created based on usage in the code. // Dinosaur class var Dinosaur = Container.expand(function () { var self = Container.call(this); var dinosaurGraphics = self.attachAsset('dinosaur', { anchorX: 0.5, anchorY: 1.0 }); self.isJumping = false; self.jumpSpeed = 0; self.gravity = 0.5; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.jumpSpeed = -20; } }; self.update = function () { if (self.isJumping) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.y > game.floorLevel) { self.y = game.floorLevel; self.isJumping = false; } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xFFFFFF // Init game with white background }); /**** * Game Code ****/ // Global variables game.floorLevel = 2732 - 200; // Assuming floor is 200px from the bottom game.dinosaur = game.addChild(new Dinosaur()); game.dinosaur.x = 400; game.dinosaur.y = game.floorLevel; game.cactuses = []; game.score = 0; game.isGameOver = false; // Touch event to make the dinosaur jump game.on('down', function () { if (!game.isGameOver) { game.dinosaur.jump(); } }); // Game tick event LK.on('tick', function () { if (!game.isGameOver) { game.dinosaur.update(); // Move cactuses and check for collisions for (var i = game.cactuses.length - 1; i >= 0; i--) { var cactus = game.cactuses[i]; cactus.move(); // Check if cactus is off screen if (cactus.x < -100) { // Assuming cactus width is less than 100px cactus.destroy(); game.cactuses.splice(i, 1); game.score++; } // Check for collision if (game.dinosaur.intersects(cactus)) { game.isGameOver = true; LK.showGameOver(); } } // Spawn cactus if (LK.ticks % 120 == 0) { // Spawn a cactus every 2 seconds var newCactus = new Cactus(); newCactus.x = 2048; // Start from the right edge newCactus.y = game.floorLevel; game.cactuses.push(newCactus); game.addChild(newCactus); } } }); // Display score var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Update score display LK.on('tick', function () { if (!game.isGameOver) { scoreTxt.setText("Score: " + game.score); } });
/****
* Classes
****/
// Cactus class
var Cactus = Container.expand(function () {
var self = Container.call(this);
var cactusGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = -5;
self.move = function () {
self.x += self.speed;
};
});
// Assets will be automatically created based on usage in the code.
// Dinosaur class
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinosaurGraphics = self.attachAsset('dinosaur', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.jumpSpeed = 0;
self.gravity = 0.5;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = -20;
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
if (self.y > game.floorLevel) {
self.y = game.floorLevel;
self.isJumping = false;
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // Init game with white background
});
/****
* Game Code
****/
// Global variables
game.floorLevel = 2732 - 200; // Assuming floor is 200px from the bottom
game.dinosaur = game.addChild(new Dinosaur());
game.dinosaur.x = 400;
game.dinosaur.y = game.floorLevel;
game.cactuses = [];
game.score = 0;
game.isGameOver = false;
// Touch event to make the dinosaur jump
game.on('down', function () {
if (!game.isGameOver) {
game.dinosaur.jump();
}
});
// Game tick event
LK.on('tick', function () {
if (!game.isGameOver) {
game.dinosaur.update();
// Move cactuses and check for collisions
for (var i = game.cactuses.length - 1; i >= 0; i--) {
var cactus = game.cactuses[i];
cactus.move();
// Check if cactus is off screen
if (cactus.x < -100) {
// Assuming cactus width is less than 100px
cactus.destroy();
game.cactuses.splice(i, 1);
game.score++;
}
// Check for collision
if (game.dinosaur.intersects(cactus)) {
game.isGameOver = true;
LK.showGameOver();
}
}
// Spawn cactus
if (LK.ticks % 120 == 0) {
// Spawn a cactus every 2 seconds
var newCactus = new Cactus();
newCactus.x = 2048; // Start from the right edge
newCactus.y = game.floorLevel;
game.cactuses.push(newCactus);
game.addChild(newCactus);
}
}
});
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update score display
LK.on('tick', function () {
if (!game.isGameOver) {
scoreTxt.setText("Score: " + game.score);
}
});