/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Car class representing a car object var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.price = 1000; // Initial price of the car self.upgradeLevel = 0; // Initial upgrade level self.sellPrice = function () { return self.price * (1 + self.upgradeLevel * 0.2); }; self.upgrade = function () { self.upgradeLevel++; self.price += 500; // Increase price with each upgrade }; }); // Player class representing the player var Player = Container.expand(function () { var self = Container.call(this); self.money = 5000; // Initial money self.cars = []; // Array to hold cars self.buyCar = function (car) { if (self.money >= car.price) { self.money -= car.price; self.cars.push(car); } }; self.sellCar = function (car) { var index = self.cars.indexOf(car); if (index > -1) { self.money += car.sellPrice(); self.cars.splice(index, 1); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = new Player(); game.addChild(player); // Create a car and add it to the game var car = new Car(); car.x = 1024; // Center horizontally car.y = 1366; // Center vertically game.addChild(car); // Display player's money var moneyTxt = new Text2('Money: $' + player.money, { size: 50, fill: "#ffffff" }); moneyTxt.anchor.set(0.5, 0); LK.gui.top.addChild(moneyTxt); // Update money display function updateMoneyDisplay() { moneyTxt.setText('Money: $' + player.money); } // Event listener for buying a car car.down = function (x, y, obj) { player.buyCar(car); updateMoneyDisplay(); }; // Event listener for selling a car car.up = function (x, y, obj) { player.sellCar(car); updateMoneyDisplay(); }; // Update function called every game tick game.update = function () { // Game logic to be executed every tick }; // Add a button to upgrade the car var upgradeBtn = new Text2('Upgrade', { size: 50, fill: "#ffffff" }); upgradeBtn.anchor.set(0.5, 0); upgradeBtn.x = 1024; upgradeBtn.y = 1500; LK.gui.top.addChild(upgradeBtn); // Event listener for upgrading the car upgradeBtn.down = function (x, y, obj) { car.upgrade(); updateMoneyDisplay(); }; // Add a button to sell the car var sellBtn = new Text2('Sell', { size: 50, fill: "#ffffff" }); sellBtn.anchor.set(0.5, 0); sellBtn.x = 1024; sellBtn.y = 1600; LK.gui.top.addChild(sellBtn); // Event listener for selling the car sellBtn.down = function (x, y, obj) { player.sellCar(car); updateMoneyDisplay(); };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class representing a car object
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.price = 1000; // Initial price of the car
self.upgradeLevel = 0; // Initial upgrade level
self.sellPrice = function () {
return self.price * (1 + self.upgradeLevel * 0.2);
};
self.upgrade = function () {
self.upgradeLevel++;
self.price += 500; // Increase price with each upgrade
};
});
// Player class representing the player
var Player = Container.expand(function () {
var self = Container.call(this);
self.money = 5000; // Initial money
self.cars = []; // Array to hold cars
self.buyCar = function (car) {
if (self.money >= car.price) {
self.money -= car.price;
self.cars.push(car);
}
};
self.sellCar = function (car) {
var index = self.cars.indexOf(car);
if (index > -1) {
self.money += car.sellPrice();
self.cars.splice(index, 1);
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
game.addChild(player);
// Create a car and add it to the game
var car = new Car();
car.x = 1024; // Center horizontally
car.y = 1366; // Center vertically
game.addChild(car);
// Display player's money
var moneyTxt = new Text2('Money: $' + player.money, {
size: 50,
fill: "#ffffff"
});
moneyTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(moneyTxt);
// Update money display
function updateMoneyDisplay() {
moneyTxt.setText('Money: $' + player.money);
}
// Event listener for buying a car
car.down = function (x, y, obj) {
player.buyCar(car);
updateMoneyDisplay();
};
// Event listener for selling a car
car.up = function (x, y, obj) {
player.sellCar(car);
updateMoneyDisplay();
};
// Update function called every game tick
game.update = function () {
// Game logic to be executed every tick
};
// Add a button to upgrade the car
var upgradeBtn = new Text2('Upgrade', {
size: 50,
fill: "#ffffff"
});
upgradeBtn.anchor.set(0.5, 0);
upgradeBtn.x = 1024;
upgradeBtn.y = 1500;
LK.gui.top.addChild(upgradeBtn);
// Event listener for upgrading the car
upgradeBtn.down = function (x, y, obj) {
car.upgrade();
updateMoneyDisplay();
};
// Add a button to sell the car
var sellBtn = new Text2('Sell', {
size: 50,
fill: "#ffffff"
});
sellBtn.anchor.set(0.5, 0);
sellBtn.x = 1024;
sellBtn.y = 1600;
LK.gui.top.addChild(sellBtn);
// Event listener for selling the car
sellBtn.down = function (x, y, obj) {
player.sellCar(car);
updateMoneyDisplay();
};