/****
* Classes
****/
// Class for Coins
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.x -= 3;
if (self.x < -coinGraphics.width / 2) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for Mario character
var Mario = Container.expand(function () {
var self = Container.call(this);
var marioGraphics = self.attachAsset('mario', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.jumpHeight = 150;
self.isJumping = false;
self.update = function () {
if (self.isJumping) {
self.y -= self.speed;
if (self.y <= self.jumpHeight) {
self.isJumping = false;
}
} else if (self.y < 2732 - marioGraphics.height / 2) {
self.y += self.speed;
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
}
};
});
// Class for Obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.x -= 5;
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue sky background
});
/****
* Game Code
****/
// Initialize Mario
var mario = game.addChild(new Mario());
mario.x = 200;
mario.y = 2732 - 100; // Start near the bottom of the screen
// Arrays to keep track of coins and obstacles
var coins = [];
var obstacles = [];
// Score display
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game update function
game.update = function () {
mario.update();
// Update coins and check for collection
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
coin.update();
if (mario.intersects(coin)) {
score += 10;
scoreTxt.setText('Score: ' + score);
coin.destroy();
coins.splice(i, 1);
}
}
// Update obstacles and check for collision
for (var j = obstacles.length - 1; j >= 0; j--) {
var obstacle = obstacles[j];
obstacle.update();
if (mario.intersects(obstacle)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new coins and obstacles
if (LK.ticks % 60 == 0) {
var newCoin = new Coin();
newCoin.x = 2048 + 50;
newCoin.y = Math.random() * (2732 - 200) + 100;
coins.push(newCoin);
game.addChild(newCoin);
}
if (LK.ticks % 120 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = 2048 + 50;
newObstacle.y = 2732 - 100;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
mario.jump();
};