/**** * Classes ****/ // Artifact class var Artifact = Container.expand(function () { var self = Container.call(this); var artifactGraphics = self.attachAsset('artifact', { anchorX: 0.5, anchorY: 0.5 }); }); // Assets will be automatically generated based on usage in the code. // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 1.0 }); self.speedX = 0; self.speedY = 0; self.onGround = false; self.update = function () { // Update hero's position and handle gravity if (!self.onGround) { self.speedY += 0.5; // gravity } self.x += self.speedX; self.y += self.speedY; // Collision with ground if (self.y > game.height - heroGraphics.height) { self.y = game.height - heroGraphics.height; self.onGround = true; self.speedY = 0; } }; self.jump = function () { if (self.onGround) { self.speedY = -15; self.onGround = false; } }; }); // ItemDescription class var ItemDescription = Container.expand(function (itemName, itemDescription) { var self = Container.call(this); var descriptionGraphics = self.attachAsset('playButton', { anchorX: 0.5, anchorY: 0.5, color: 0x000000, scaleX: 3, scaleY: 2, lineWidth: 10, lineColor: 0x00FF00 }); var descriptionText = new Text2(itemName + '\n' + itemDescription, { size: 50, fill: "#ffffff" }); descriptionText.anchor.set(0.5, 0.5); descriptionText.x = descriptionGraphics.x; descriptionText.y = descriptionGraphics.y - 50; self.addChild(descriptionText); self.updateDescription = function (itemName, itemDescription) { descriptionText.setText(itemName + '\n' + itemDescription); }; }); // Main Menu class var MainMenu = Container.expand(function () { var self = Container.call(this); var title = self.attachAsset('title', { anchorX: 0.5, anchorY: 0.5 }); title.x = game.width / 2; title.y = game.height / 2 - 100; var playButton = self.attachAsset('playButton', { anchorX: 0.5, anchorY: 0.5, color: 0x000000, // Change color to black scaleX: 6, scaleY: 4, lineWidth: 10, lineColor: 0x00FF00 // Change border color to green }); playButton.x = game.width / 4; playButton.y = game.height * 3 / 4; playButton.on('down', function () { self.destroy(); hero.visible = true; platforms.forEach(function (platform) { platform.visible = true; }); artifacts.forEach(function (artifact) { artifact.visible = true; }); }); playButton.on('over', function () { playButton.scaleX = 5.5; playButton.scaleY = 3.5; }); playButton.on('out', function () { playButton.scaleX = 5; playButton.scaleY = 3; }); // Add 'Play' text to the play button var playText = new Text2('Play', { size: 200, // 2x bigger fill: "#ffffff" }); playText.anchor.set(0.5, 0.5); playText.x = playButton.x; playText.y = playButton.y; self.addChild(playText); // Add a shop button same size as the play button var shopButton = self.attachAsset('playButton', { anchorX: 0.5, anchorY: 0.5, color: 0x000000, scaleX: 6, scaleY: 4, lineWidth: 10, lineColor: 0x00FF00 // Change border color to green }); shopButton.x = game.width * 3 / 4; shopButton.y = game.height * 3 / 4; shopButton.on('down', function () { // Show the shop when the shop button is pressed var shop = game.addChild(new Shop()); // Hide the main menu self.visible = false; }); shopButton.on('over', function () { shopButton.scaleX = 5.5; shopButton.scaleY = 3.5; }); shopButton.on('out', function () { shopButton.scaleX = 5; shopButton.scaleY = 3; }); // Add 'Shop' text to the shop button var shopText = new Text2('Shop', { size: 200, fill: "#ffffff" }); shopText.anchor.set(0.5, 0.5); shopText.x = shopButton.x; shopText.y = shopButton.y; self.addChild(shopText); }); // Platform class var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 1.0 }); }); // Shop class var Shop = Container.expand(function () { var self = Container.call(this); // Create a scrollable container for the shop items var shopContainer = new Container(); self.addChild(shopContainer); // Create an array to store the shop items var shopItems = []; // Populate the shop with items in a grid layout var itemsPerRow = 4; // Number of items per row var itemSize = 250; // Size of each item var gap = 50; // Increase the gap between items for (var i = 0; i < 100; i++) { var row = Math.floor(i / itemsPerRow); var col = i % itemsPerRow; var itemName = 'Item ' + (i + 1); var isPack = i % 5 === 0; // Mark every 5th item as a pack var shopItem = new ShopItem(itemName, isPack); shopItem.x = col * (itemSize + gap) + gap; shopItem.y = row * (itemSize + gap) + gap; shopContainer.addChild(shopItem); shopItems.push(shopItem); } // Add a points display at the top of the shop var pointsDisplay = new Text2('Points: ' + LK.getScore(), { size: 100, fill: "#ffffff" }); pointsDisplay.anchor.set(0.5, 0); pointsDisplay.x = game.width / 2; pointsDisplay.y = 20; self.addChild(pointsDisplay); // Add an item description display at the top right corner of the shop var itemDescription = new ItemDescription('', ''); itemDescription.x = game.width * 3 / 4; itemDescription.y = 20; self.addChild(itemDescription); // Update the points display every tick LK.on('tick', function () { pointsDisplay.setText('Points: ' + LK.getScore()); }); // Make the shop scrollable self.on('move', function (obj) { var event = obj.event; var pos = event.getLocalPosition(self); var deltaY = event.data.originalEvent.deltaY; if (deltaY > 0) { // Scroll down shopContainer.y = Math.min(shopContainer.y + 100, 0); } else if (deltaY < 0) { // Scroll up shopContainer.y = Math.max(shopContainer.y - 100, -shopContainer.height + game.height); } // Add a back button at the bottom of the shop var backButton = self.attachAsset('playButton', { anchorX: 0.5, anchorY: 0.5, color: 0x000000, scaleX: 6, scaleY: 4, lineWidth: 10, lineColor: 0x00FF00 }); backButton.x = game.width / 2; backButton.y = game.height - 100; backButton.on('down', function () { // Hide the shop and show the main menu when the back button is pressed self.destroy(); mainMenu.visible = true; }); // Add 'Back' text to the back button var backText = new Text2('Back', { size: 200, fill: "#ffffff" }); backText.anchor.set(0.5, 0.5); backText.x = backButton.x; backText.y = backButton.y; self.addChild(backText); }); }); // ShopItem class var ShopItem = Container.expand(function (itemName, isPack) { var self = Container.call(this); var itemGraphics = self.attachAsset('playButton', { anchorX: 0.5, anchorY: 0.5, color: 0x000000, scaleX: 3, scaleY: 2, lineWidth: 10, lineColor: 0x00FF00 }); var itemText = new Text2(itemName + (isPack ? ' (PACK)' : ''), { size: 50, fill: "#ffffff" }); itemText.anchor.set(0.5, 0.5); itemText.x = itemGraphics.x; itemText.y = itemGraphics.y - 50; self.addChild(itemText); // Add a description for the item var itemDescription = itemName + ' does something cool!'; self.on('down', function () { itemDescription.setText(itemName + '\n' + itemDescription); }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to represent the sky }); /**** * Game Code ****/ var mainMenu = game.addChild(new MainMenu()); function startGame() { hero.visible = false; platforms.forEach(function (platform) { platform.visible = false; }); artifacts.forEach(function (artifact) { artifact.visible = false; }); } var hero = game.addChild(new Hero()); hero.x = game.width / 2; hero.y = game.height - 150; var platforms = []; var artifacts = []; // Create platforms and artifacts function createLevel() { // Example platform and artifact creation var platform = game.addChild(new Platform()); platform.x = game.width / 2; platform.y = game.height - 100; platforms.push(platform); var artifact = game.addChild(new Artifact()); artifact.x = platform.x; artifact.y = platform.y - 50; artifacts.push(artifact); } createLevel(); // Touch event to make the hero jump game.on('down', function (obj) { hero.jump(); }); game.on('up', function () { hero.visible = false; platforms.forEach(function (platform) { platform.visible = false; }); artifacts.forEach(function (artifact) { artifact.visible = false; }); }); // Game tick event LK.on('tick', function () { hero.update(); // Check for artifact collection artifacts.forEach(function (artifact, index) { if (hero.intersects(artifact)) { artifact.destroy(); // Collect the artifact artifacts.splice(index, 1); // Increase score or trigger some effect } }); });
/****
* Classes
****/
// Artifact class
var Artifact = Container.expand(function () {
var self = Container.call(this);
var artifactGraphics = self.attachAsset('artifact', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Assets will be automatically generated based on usage in the code.
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 1.0
});
self.speedX = 0;
self.speedY = 0;
self.onGround = false;
self.update = function () {
// Update hero's position and handle gravity
if (!self.onGround) {
self.speedY += 0.5; // gravity
}
self.x += self.speedX;
self.y += self.speedY;
// Collision with ground
if (self.y > game.height - heroGraphics.height) {
self.y = game.height - heroGraphics.height;
self.onGround = true;
self.speedY = 0;
}
};
self.jump = function () {
if (self.onGround) {
self.speedY = -15;
self.onGround = false;
}
};
});
// ItemDescription class
var ItemDescription = Container.expand(function (itemName, itemDescription) {
var self = Container.call(this);
var descriptionGraphics = self.attachAsset('playButton', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x000000,
scaleX: 3,
scaleY: 2,
lineWidth: 10,
lineColor: 0x00FF00
});
var descriptionText = new Text2(itemName + '\n' + itemDescription, {
size: 50,
fill: "#ffffff"
});
descriptionText.anchor.set(0.5, 0.5);
descriptionText.x = descriptionGraphics.x;
descriptionText.y = descriptionGraphics.y - 50;
self.addChild(descriptionText);
self.updateDescription = function (itemName, itemDescription) {
descriptionText.setText(itemName + '\n' + itemDescription);
};
});
// Main Menu class
var MainMenu = Container.expand(function () {
var self = Container.call(this);
var title = self.attachAsset('title', {
anchorX: 0.5,
anchorY: 0.5
});
title.x = game.width / 2;
title.y = game.height / 2 - 100;
var playButton = self.attachAsset('playButton', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x000000,
// Change color to black
scaleX: 6,
scaleY: 4,
lineWidth: 10,
lineColor: 0x00FF00 // Change border color to green
});
playButton.x = game.width / 4;
playButton.y = game.height * 3 / 4;
playButton.on('down', function () {
self.destroy();
hero.visible = true;
platforms.forEach(function (platform) {
platform.visible = true;
});
artifacts.forEach(function (artifact) {
artifact.visible = true;
});
});
playButton.on('over', function () {
playButton.scaleX = 5.5;
playButton.scaleY = 3.5;
});
playButton.on('out', function () {
playButton.scaleX = 5;
playButton.scaleY = 3;
});
// Add 'Play' text to the play button
var playText = new Text2('Play', {
size: 200,
// 2x bigger
fill: "#ffffff"
});
playText.anchor.set(0.5, 0.5);
playText.x = playButton.x;
playText.y = playButton.y;
self.addChild(playText);
// Add a shop button same size as the play button
var shopButton = self.attachAsset('playButton', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x000000,
scaleX: 6,
scaleY: 4,
lineWidth: 10,
lineColor: 0x00FF00 // Change border color to green
});
shopButton.x = game.width * 3 / 4;
shopButton.y = game.height * 3 / 4;
shopButton.on('down', function () {
// Show the shop when the shop button is pressed
var shop = game.addChild(new Shop());
// Hide the main menu
self.visible = false;
});
shopButton.on('over', function () {
shopButton.scaleX = 5.5;
shopButton.scaleY = 3.5;
});
shopButton.on('out', function () {
shopButton.scaleX = 5;
shopButton.scaleY = 3;
});
// Add 'Shop' text to the shop button
var shopText = new Text2('Shop', {
size: 200,
fill: "#ffffff"
});
shopText.anchor.set(0.5, 0.5);
shopText.x = shopButton.x;
shopText.y = shopButton.y;
self.addChild(shopText);
});
// Platform class
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 1.0
});
});
// Shop class
var Shop = Container.expand(function () {
var self = Container.call(this);
// Create a scrollable container for the shop items
var shopContainer = new Container();
self.addChild(shopContainer);
// Create an array to store the shop items
var shopItems = [];
// Populate the shop with items in a grid layout
var itemsPerRow = 4; // Number of items per row
var itemSize = 250; // Size of each item
var gap = 50; // Increase the gap between items
for (var i = 0; i < 100; i++) {
var row = Math.floor(i / itemsPerRow);
var col = i % itemsPerRow;
var itemName = 'Item ' + (i + 1);
var isPack = i % 5 === 0; // Mark every 5th item as a pack
var shopItem = new ShopItem(itemName, isPack);
shopItem.x = col * (itemSize + gap) + gap;
shopItem.y = row * (itemSize + gap) + gap;
shopContainer.addChild(shopItem);
shopItems.push(shopItem);
}
// Add a points display at the top of the shop
var pointsDisplay = new Text2('Points: ' + LK.getScore(), {
size: 100,
fill: "#ffffff"
});
pointsDisplay.anchor.set(0.5, 0);
pointsDisplay.x = game.width / 2;
pointsDisplay.y = 20;
self.addChild(pointsDisplay);
// Add an item description display at the top right corner of the shop
var itemDescription = new ItemDescription('', '');
itemDescription.x = game.width * 3 / 4;
itemDescription.y = 20;
self.addChild(itemDescription);
// Update the points display every tick
LK.on('tick', function () {
pointsDisplay.setText('Points: ' + LK.getScore());
});
// Make the shop scrollable
self.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
var deltaY = event.data.originalEvent.deltaY;
if (deltaY > 0) {
// Scroll down
shopContainer.y = Math.min(shopContainer.y + 100, 0);
} else if (deltaY < 0) {
// Scroll up
shopContainer.y = Math.max(shopContainer.y - 100, -shopContainer.height + game.height);
}
// Add a back button at the bottom of the shop
var backButton = self.attachAsset('playButton', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x000000,
scaleX: 6,
scaleY: 4,
lineWidth: 10,
lineColor: 0x00FF00
});
backButton.x = game.width / 2;
backButton.y = game.height - 100;
backButton.on('down', function () {
// Hide the shop and show the main menu when the back button is pressed
self.destroy();
mainMenu.visible = true;
});
// Add 'Back' text to the back button
var backText = new Text2('Back', {
size: 200,
fill: "#ffffff"
});
backText.anchor.set(0.5, 0.5);
backText.x = backButton.x;
backText.y = backButton.y;
self.addChild(backText);
});
});
// ShopItem class
var ShopItem = Container.expand(function (itemName, isPack) {
var self = Container.call(this);
var itemGraphics = self.attachAsset('playButton', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x000000,
scaleX: 3,
scaleY: 2,
lineWidth: 10,
lineColor: 0x00FF00
});
var itemText = new Text2(itemName + (isPack ? ' (PACK)' : ''), {
size: 50,
fill: "#ffffff"
});
itemText.anchor.set(0.5, 0.5);
itemText.x = itemGraphics.x;
itemText.y = itemGraphics.y - 50;
self.addChild(itemText);
// Add a description for the item
var itemDescription = itemName + ' does something cool!';
self.on('down', function () {
itemDescription.setText(itemName + '\n' + itemDescription);
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to represent the sky
});
/****
* Game Code
****/
var mainMenu = game.addChild(new MainMenu());
function startGame() {
hero.visible = false;
platforms.forEach(function (platform) {
platform.visible = false;
});
artifacts.forEach(function (artifact) {
artifact.visible = false;
});
}
var hero = game.addChild(new Hero());
hero.x = game.width / 2;
hero.y = game.height - 150;
var platforms = [];
var artifacts = [];
// Create platforms and artifacts
function createLevel() {
// Example platform and artifact creation
var platform = game.addChild(new Platform());
platform.x = game.width / 2;
platform.y = game.height - 100;
platforms.push(platform);
var artifact = game.addChild(new Artifact());
artifact.x = platform.x;
artifact.y = platform.y - 50;
artifacts.push(artifact);
}
createLevel();
// Touch event to make the hero jump
game.on('down', function (obj) {
hero.jump();
});
game.on('up', function () {
hero.visible = false;
platforms.forEach(function (platform) {
platform.visible = false;
});
artifacts.forEach(function (artifact) {
artifact.visible = false;
});
});
// Game tick event
LK.on('tick', function () {
hero.update();
// Check for artifact collection
artifacts.forEach(function (artifact, index) {
if (hero.intersects(artifact)) {
artifact.destroy(); // Collect the artifact
artifacts.splice(index, 1);
// Increase score or trigger some effect
}
});
});