/****
* Classes
****/
// Class for Vengeful Animals
var Animal = Container.expand(function () {
var self = Container.call(this);
var animalGraphics = self.attachAsset('animal', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
});
//<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 = 10;
self.health = 3; // Mario can withstand three touches with the animals
self.immune = true; // Mario starts with immunity
self.immuneTime = 180; // Immunity lasts for 3 seconds (180 ticks)
self.update = function () {
// Update logic for Mario
if (self.immune) {
self.immuneTime--;
if (self.immuneTime <= 0) {
self.immune = false;
}
}
};
});
// Class for Meat
var Meat = Container.expand(function () {
var self = Container.call(this);
var meatGraphics = self.attachAsset('meat', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Init game with sky blue background
});
/****
* Game Code
****/
// Add a grass background
var background = game.attachAsset('background', {
anchorX: 0,
anchorY: 0
}); // Attach the grass background to the game
// Initialize Mario
var mario = game.addChild(new Mario());
mario.x = 1024;
mario.y = 2400;
// Initialize Meat
var meats = [];
for (var i = 0; i < 5; i++) {
var meat = new Meat();
meat.x = Math.random() * 2048;
meat.y = 0;
meats.push(meat);
game.addChild(meat);
}
// Initialize Animals
var animals = [];
for (var j = 0; j < 3; j++) {
var animal = new Animal();
animal.x = Math.random() * 2048;
animal.y = Math.random() * 2000;
animals.push(animal);
game.addChild(animal);
}
// Handle game move events
game.move = function (x, y, obj) {
mario.x = x;
mario.y = y;
};
// Update game logic
game.update = function () {
// Check for collisions between Mario and Meat
for (var i = meats.length - 1; i >= 0; i--) {
if (mario.intersects(meats[i])) {
meats[i].destroy();
meats.splice(i, 1);
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
LK.getSound('eat').play(); // Play the 'eat' sound
if (LK.getScore() >= 100) {
mario.attachAsset('mario_grill', {
anchorX: 0.5,
anchorY: 0.5
});
LK.getSound('shape').play(); // Play the 'shape' sound
}
}
}
// Check for collisions between Mario and Animals
for (var j = animals.length - 1; j >= 0; j--) {
if (mario.intersects(animals[j]) && !mario.immune) {
mario.health--;
LK.getSound('ram').play(); // Play the 'ram' sound
if (mario.health <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
// Check if Mario touches any side of the screen
if (mario.x <= 0 || mario.x >= 2048 || mario.y <= 0 || mario.y >= 2732 && !mario.immune) {
mario.health--;
LK.getSound('ram').play(); // Play the 'ram' sound
if (mario.health <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update all animals
animals.forEach(function (animal) {
animal.update();
});
// Generate meat every 60 ticks
if (LK.ticks % 60 == 0) {
var meat = new Meat();
meat.x = Math.random() * 2048;
meat.y = 0;
meats.push(meat);
game.addChild(meat);
}
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);