/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
coins: 0,
ownedItems: [],
equippedItems: [],
isSkeletonTransformed: false
});
/****
* Classes
****/
var CareButton = Container.expand(function (buttonType, color) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(buttonType + 'Button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(buttonType.toUpperCase(), {
size: 72,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
tween(buttonText, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
tween(buttonText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
if (buttonType === 'feed') {
pet.feed();
LK.getSound('feed').play();
} else if (buttonType === 'play') {
pet.play();
LK.getSound('play').play();
// Earn coin for playing
storage.coins += 1;
coinsText.setText('Coins: ' + storage.coins);
} else if (buttonType === 'sleep') {
pet.rest();
LK.getSound('sleep').play();
}
};
return self;
});
var MeterDisplay = Container.expand(function (meterType, color) {
var self = Container.call(this);
var meterBackground = self.attachAsset(meterType + 'Meter', {
anchorX: 0,
anchorY: 0.5,
alpha: 0.3
});
var meterFill = self.attachAsset(meterType + 'Meter', {
anchorX: 0,
anchorY: 0.5
});
self.updateMeter = function (value) {
var percentage = Math.max(0, value / 100);
meterFill.width = 900 * percentage;
// Change color based on level
if (value <= 20) {
meterFill.tint = 0xff0000; // Red
} else if (value <= 50) {
meterFill.tint = 0xff6600; // Orange
} else {
meterFill.tint = color; // Original color
}
};
return self;
});
var Shop = Container.expand(function () {
var self = Container.call(this);
self.visible = false;
var shopBackground = self.attachAsset('shopPanel', {
anchorX: 0.5,
anchorY: 0.5
});
var shopTitle = new Text2('SHOP', {
size: 96,
fill: 0x333333
});
shopTitle.anchor.set(0.5, 0.5);
shopTitle.y = -450;
self.addChild(shopTitle);
var closeButton = new Text2('CLOSE', {
size: 60,
fill: 0xff0000
});
closeButton.anchor.set(0.5, 0.5);
closeButton.y = 450;
self.addChild(closeButton);
closeButton.down = function (x, y, obj) {
self.visible = false;
};
// Create shop items
var hatItem = self.addChild(new ShopItem('hat', 10, 'HAT'));
hatItem.x = -300;
hatItem.y = -100;
var pizzaItem = self.addChild(new ShopItem('pizza', 15, 'PIZZA'));
pizzaItem.x = 0;
pizzaItem.y = -100;
var phoneItem = self.addChild(new ShopItem('phone', 25, 'PHONE'));
phoneItem.x = 300;
phoneItem.y = -100;
// Second shop section title
var transformTitle = new Text2('TRANSFORMATIONS', {
size: 72,
fill: 0x333333
});
transformTitle.anchor.set(0.5, 0.5);
transformTitle.y = 150;
self.addChild(transformTitle);
// Skeleton transformation item
var skeletonItem = self.addChild(new ShopItem('skeleton', 999, 'SKELETON'));
skeletonItem.x = 0;
skeletonItem.y = 250;
return self;
});
var ShopItem = Container.expand(function (itemType, price, name) {
var self = Container.call(this);
var itemBackground = self.attachAsset('itemButton', {
anchorX: 0.5,
anchorY: 0.5
});
var itemIcon = self.attachAsset(itemType, {
anchorX: 0.5,
anchorY: 0.5,
y: -30
});
var itemName = new Text2(name, {
size: 36,
fill: 0x333333
});
itemName.anchor.set(0.5, 0.5);
itemName.y = 60;
self.addChild(itemName);
var priceText = new Text2(price + ' coins', {
size: 30,
fill: 0x666666
});
priceText.anchor.set(0.5, 0.5);
priceText.y = 100;
self.addChild(priceText);
var owned = storage.ownedItems.indexOf(itemType) !== -1;
var equipped = storage.equippedItems.indexOf(itemType) !== -1;
if (owned) {
var ownedText = new Text2('OWNED', {
size: 27,
fill: 0x4caf50
});
ownedText.anchor.set(0.5, 0.5);
ownedText.y = 130;
self.addChild(ownedText);
if (equipped) {
itemBackground.tint = 0x99ff99; // Brighter green for equipped
} else {
itemBackground.tint = 0xccffcc; // Light green for owned but not equipped
}
}
self.itemType = itemType;
self.price = price;
self.owned = owned;
self.down = function (x, y, obj) {
if (!self.owned && storage.coins >= self.price) {
storage.coins -= self.price;
storage.ownedItems.push(self.itemType);
self.owned = true;
var ownedText = new Text2('OWNED', {
size: 27,
fill: 0x4caf50
});
ownedText.anchor.set(0.5, 0.5);
ownedText.y = 130;
self.addChild(ownedText);
itemBackground.tint = 0xccffcc;
coinsText.setText('Coins: ' + storage.coins);
// Handle skeleton transformation
if (self.itemType === 'skeleton') {
storage.isSkeletonTransformed = true;
pet.transformToSkeleton();
}
} else if (self.owned && self.itemType !== 'skeleton') {
// Toggle equipment (only for regular items, not transformation)
var isEquipped = storage.equippedItems.indexOf(self.itemType) !== -1;
if (isEquipped) {
// Unequip item
var index = storage.equippedItems.indexOf(self.itemType);
storage.equippedItems.splice(index, 1);
itemBackground.tint = 0xccffcc; // Light green for owned but not equipped
} else {
// Equip item
storage.equippedItems.push(self.itemType);
itemBackground.tint = 0x99ff99; // Brighter green for equipped
}
}
// Update pet equipment immediately
if (self.itemType !== 'skeleton') {
pet.updateEquipment();
}
};
return self;
});
var Tamagochi = Container.expand(function () {
var self = Container.call(this);
var petGraphics = self.attachAsset('pet', {
anchorX: 0.5,
anchorY: 0.5
});
// Equipment objects
var hatEquipment = null;
var phoneEquipment = null;
var pizzaEquipment = null;
self.hungerLevel = 100;
self.happinessLevel = 100;
self.energyLevel = 100;
self.transformToSkeleton = function () {
// Remove the current pet graphics
self.removeChild(petGraphics);
// Create new skeleton graphics
petGraphics = self.attachAsset('skeleton', {
anchorX: 0.5,
anchorY: 0.5
});
};
// Check if already transformed on initialization
if (storage.isSkeletonTransformed) {
self.removeChild(petGraphics);
petGraphics = self.attachAsset('skeleton', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.updateEquipment = function () {
// Check if hat should be equipped
if (storage.ownedItems.indexOf('hat') !== -1 && storage.equippedItems.indexOf('hat') !== -1) {
if (!hatEquipment) {
hatEquipment = self.attachAsset('hat', {
anchorX: 0.5,
anchorY: 1,
scaleX: 4.0,
scaleY: 4.0
});
hatEquipment.y = -350; // Top of pet's head
hatEquipment.x = 0;
hatEquipment.down = function (x, y, obj) {
// Deselect hat
var index = storage.equippedItems.indexOf('hat');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (hatEquipment) {
self.removeChild(hatEquipment);
hatEquipment = null;
}
// Check if phone should be equipped
if (storage.ownedItems.indexOf('phone') !== -1 && storage.equippedItems.indexOf('phone') !== -1) {
if (!phoneEquipment) {
phoneEquipment = self.attachAsset('phone', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4.0,
scaleY: 4.0
});
phoneEquipment.y = 0; // Center height
phoneEquipment.x = -400; // Left side of pet
phoneEquipment.down = function (x, y, obj) {
// Deselect phone
var index = storage.equippedItems.indexOf('phone');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (phoneEquipment) {
self.removeChild(phoneEquipment);
phoneEquipment = null;
}
// Check if pizza should be equipped
if (storage.ownedItems.indexOf('pizza') !== -1 && storage.equippedItems.indexOf('pizza') !== -1) {
if (!pizzaEquipment) {
pizzaEquipment = self.attachAsset('pizza', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4.0,
scaleY: 4.0
});
pizzaEquipment.y = 0; // Center height
pizzaEquipment.x = 400; // Right side of pet
pizzaEquipment.down = function (x, y, obj) {
// Deselect pizza
var index = storage.equippedItems.indexOf('pizza');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (pizzaEquipment) {
self.removeChild(pizzaEquipment);
pizzaEquipment = null;
}
};
self.update = function () {
// Update equipment display
self.updateEquipment();
// Decrease all meters gradually (about 3 points every 1 second at 60fps)
if (LK.ticks % 60 === 0) {
self.hungerLevel = Math.max(0, self.hungerLevel - 3);
self.happinessLevel = Math.max(0, self.happinessLevel - 3);
self.energyLevel = Math.max(0, self.energyLevel - 3);
}
// Change pet appearance based on lowest need
var lowestLevel = Math.min(self.hungerLevel, self.happinessLevel, self.energyLevel);
if (lowestLevel <= 20) {
petGraphics.tint = 0xff0000; // Red when critical
} else if (lowestLevel <= 50) {
petGraphics.tint = 0xffaa00; // Orange when low
} else {
petGraphics.tint = 0xffffff; // Normal color
}
// Animate pet slightly
if (LK.ticks % 180 === 0) {
tween(petGraphics, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 300,
easing: tween.easeOut
});
}
if (LK.ticks % 180 === 30) {
tween(petGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 300,
easing: tween.easeOut
});
}
};
self.feed = function () {
self.hungerLevel = Math.min(100, self.hungerLevel + 4);
// Happy bounce animation
tween(petGraphics, {
scaleY: 1.3
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
self.play = function () {
self.happinessLevel = Math.min(100, self.happinessLevel + 4);
// Wiggle animation
tween(petGraphics, {
rotation: 0.2
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
rotation: -0.2
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
rotation: 0
}, {
duration: 100,
easing: tween.easeOut
});
}
});
}
});
};
self.rest = function () {
self.energyLevel = Math.min(100, self.energyLevel + 4);
// Gentle pulse animation
tween(petGraphics, {
alpha: 0.7
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
alpha: 1.0
}, {
duration: 400,
easing: tween.easeOut
});
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var survivalTime = 0;
// Create pet
var pet = game.addChild(new Tamagochi());
pet.x = 2048 / 2;
pet.y = 800;
// Create meters
var hungerMeter = game.addChild(new MeterDisplay('hunger', 0x4CAF50));
hungerMeter.x = 724;
hungerMeter.y = 1200;
var happinessMeter = game.addChild(new MeterDisplay('happiness', 0x2196F3));
happinessMeter.x = 724;
happinessMeter.y = 1300;
var energyMeter = game.addChild(new MeterDisplay('energy', 0xFF9800));
energyMeter.x = 724;
energyMeter.y = 1400;
// Create meter labels
var hungerLabel = new Text2('HUNGER', {
size: 72,
fill: 0x333333
});
hungerLabel.anchor.set(1, 0.5);
hungerLabel.x = 700;
hungerLabel.y = 1200;
game.addChild(hungerLabel);
var happinessLabel = new Text2('HAPPY', {
size: 72,
fill: 0x333333
});
happinessLabel.anchor.set(1, 0.5);
happinessLabel.x = 700;
happinessLabel.y = 1300;
game.addChild(happinessLabel);
var energyLabel = new Text2('ENERGY', {
size: 72,
fill: 0x333333
});
energyLabel.anchor.set(1, 0.5);
energyLabel.x = 700;
energyLabel.y = 1400;
game.addChild(energyLabel);
// Create care buttons
var feedButton = game.addChild(new CareButton('feed', 0x4CAF50));
feedButton.x = 600;
feedButton.y = 1600;
var playButton = game.addChild(new CareButton('play', 0x2196F3));
playButton.x = 1024;
playButton.y = 1600;
var sleepButton = game.addChild(new CareButton('sleep', 0xFF9800));
sleepButton.x = 1448;
sleepButton.y = 1600;
// Create shop button
var shopButtonGraphics = game.attachAsset('shopButton', {
anchorX: 0.5,
anchorY: 0.5
});
shopButtonGraphics.x = 1750;
shopButtonGraphics.y = 1600;
var shopButtonText = new Text2('SHOP', {
size: 48,
fill: 0xFFFFFF
});
shopButtonText.anchor.set(0.5, 0.5);
shopButtonText.x = 1750;
shopButtonText.y = 1600;
game.addChild(shopButtonText);
shopButtonGraphics.down = function (x, y, obj) {
shop.visible = true;
};
// Create shop
var shop = game.addChild(new Shop());
shop.x = 2048 / 2;
shop.y = 2732 / 2;
// Create coins display
var coinsText = new Text2('Coins: ' + storage.coins, {
size: 60,
fill: 0xFFFFFF
});
coinsText.anchor.set(1, 0);
coinsText.x = 2048 - 100;
coinsText.y = 100;
game.addChild(coinsText);
// Score display
var scoreText = new Text2('Time: 0s', {
size: 96,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Pet name display
var petNameText = new Text2('MY TAMAGOCHI', {
size: 120,
fill: 0xFFFFFF
});
petNameText.anchor.set(0.5, 0.5);
petNameText.x = 2048 / 2;
petNameText.y = 400;
game.addChild(petNameText);
game.update = function () {
// Update survival time (increment every second)
if (LK.ticks % 60 === 0) {
survivalTime++;
LK.setScore(survivalTime);
scoreText.setText('Time: ' + survivalTime + 's');
}
// Update meter displays
hungerMeter.updateMeter(pet.hungerLevel);
happinessMeter.updateMeter(pet.happinessLevel);
energyMeter.updateMeter(pet.energyLevel);
// Check for game over
if (pet.hungerLevel <= 0 || pet.happinessLevel <= 0 || pet.energyLevel <= 0) {
LK.showGameOver();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
coins: 0,
ownedItems: [],
equippedItems: [],
isSkeletonTransformed: false
});
/****
* Classes
****/
var CareButton = Container.expand(function (buttonType, color) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(buttonType + 'Button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(buttonType.toUpperCase(), {
size: 72,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
tween(buttonText, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(buttonGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
tween(buttonText, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
if (buttonType === 'feed') {
pet.feed();
LK.getSound('feed').play();
} else if (buttonType === 'play') {
pet.play();
LK.getSound('play').play();
// Earn coin for playing
storage.coins += 1;
coinsText.setText('Coins: ' + storage.coins);
} else if (buttonType === 'sleep') {
pet.rest();
LK.getSound('sleep').play();
}
};
return self;
});
var MeterDisplay = Container.expand(function (meterType, color) {
var self = Container.call(this);
var meterBackground = self.attachAsset(meterType + 'Meter', {
anchorX: 0,
anchorY: 0.5,
alpha: 0.3
});
var meterFill = self.attachAsset(meterType + 'Meter', {
anchorX: 0,
anchorY: 0.5
});
self.updateMeter = function (value) {
var percentage = Math.max(0, value / 100);
meterFill.width = 900 * percentage;
// Change color based on level
if (value <= 20) {
meterFill.tint = 0xff0000; // Red
} else if (value <= 50) {
meterFill.tint = 0xff6600; // Orange
} else {
meterFill.tint = color; // Original color
}
};
return self;
});
var Shop = Container.expand(function () {
var self = Container.call(this);
self.visible = false;
var shopBackground = self.attachAsset('shopPanel', {
anchorX: 0.5,
anchorY: 0.5
});
var shopTitle = new Text2('SHOP', {
size: 96,
fill: 0x333333
});
shopTitle.anchor.set(0.5, 0.5);
shopTitle.y = -450;
self.addChild(shopTitle);
var closeButton = new Text2('CLOSE', {
size: 60,
fill: 0xff0000
});
closeButton.anchor.set(0.5, 0.5);
closeButton.y = 450;
self.addChild(closeButton);
closeButton.down = function (x, y, obj) {
self.visible = false;
};
// Create shop items
var hatItem = self.addChild(new ShopItem('hat', 10, 'HAT'));
hatItem.x = -300;
hatItem.y = -100;
var pizzaItem = self.addChild(new ShopItem('pizza', 15, 'PIZZA'));
pizzaItem.x = 0;
pizzaItem.y = -100;
var phoneItem = self.addChild(new ShopItem('phone', 25, 'PHONE'));
phoneItem.x = 300;
phoneItem.y = -100;
// Second shop section title
var transformTitle = new Text2('TRANSFORMATIONS', {
size: 72,
fill: 0x333333
});
transformTitle.anchor.set(0.5, 0.5);
transformTitle.y = 150;
self.addChild(transformTitle);
// Skeleton transformation item
var skeletonItem = self.addChild(new ShopItem('skeleton', 999, 'SKELETON'));
skeletonItem.x = 0;
skeletonItem.y = 250;
return self;
});
var ShopItem = Container.expand(function (itemType, price, name) {
var self = Container.call(this);
var itemBackground = self.attachAsset('itemButton', {
anchorX: 0.5,
anchorY: 0.5
});
var itemIcon = self.attachAsset(itemType, {
anchorX: 0.5,
anchorY: 0.5,
y: -30
});
var itemName = new Text2(name, {
size: 36,
fill: 0x333333
});
itemName.anchor.set(0.5, 0.5);
itemName.y = 60;
self.addChild(itemName);
var priceText = new Text2(price + ' coins', {
size: 30,
fill: 0x666666
});
priceText.anchor.set(0.5, 0.5);
priceText.y = 100;
self.addChild(priceText);
var owned = storage.ownedItems.indexOf(itemType) !== -1;
var equipped = storage.equippedItems.indexOf(itemType) !== -1;
if (owned) {
var ownedText = new Text2('OWNED', {
size: 27,
fill: 0x4caf50
});
ownedText.anchor.set(0.5, 0.5);
ownedText.y = 130;
self.addChild(ownedText);
if (equipped) {
itemBackground.tint = 0x99ff99; // Brighter green for equipped
} else {
itemBackground.tint = 0xccffcc; // Light green for owned but not equipped
}
}
self.itemType = itemType;
self.price = price;
self.owned = owned;
self.down = function (x, y, obj) {
if (!self.owned && storage.coins >= self.price) {
storage.coins -= self.price;
storage.ownedItems.push(self.itemType);
self.owned = true;
var ownedText = new Text2('OWNED', {
size: 27,
fill: 0x4caf50
});
ownedText.anchor.set(0.5, 0.5);
ownedText.y = 130;
self.addChild(ownedText);
itemBackground.tint = 0xccffcc;
coinsText.setText('Coins: ' + storage.coins);
// Handle skeleton transformation
if (self.itemType === 'skeleton') {
storage.isSkeletonTransformed = true;
pet.transformToSkeleton();
}
} else if (self.owned && self.itemType !== 'skeleton') {
// Toggle equipment (only for regular items, not transformation)
var isEquipped = storage.equippedItems.indexOf(self.itemType) !== -1;
if (isEquipped) {
// Unequip item
var index = storage.equippedItems.indexOf(self.itemType);
storage.equippedItems.splice(index, 1);
itemBackground.tint = 0xccffcc; // Light green for owned but not equipped
} else {
// Equip item
storage.equippedItems.push(self.itemType);
itemBackground.tint = 0x99ff99; // Brighter green for equipped
}
}
// Update pet equipment immediately
if (self.itemType !== 'skeleton') {
pet.updateEquipment();
}
};
return self;
});
var Tamagochi = Container.expand(function () {
var self = Container.call(this);
var petGraphics = self.attachAsset('pet', {
anchorX: 0.5,
anchorY: 0.5
});
// Equipment objects
var hatEquipment = null;
var phoneEquipment = null;
var pizzaEquipment = null;
self.hungerLevel = 100;
self.happinessLevel = 100;
self.energyLevel = 100;
self.transformToSkeleton = function () {
// Remove the current pet graphics
self.removeChild(petGraphics);
// Create new skeleton graphics
petGraphics = self.attachAsset('skeleton', {
anchorX: 0.5,
anchorY: 0.5
});
};
// Check if already transformed on initialization
if (storage.isSkeletonTransformed) {
self.removeChild(petGraphics);
petGraphics = self.attachAsset('skeleton', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.updateEquipment = function () {
// Check if hat should be equipped
if (storage.ownedItems.indexOf('hat') !== -1 && storage.equippedItems.indexOf('hat') !== -1) {
if (!hatEquipment) {
hatEquipment = self.attachAsset('hat', {
anchorX: 0.5,
anchorY: 1,
scaleX: 4.0,
scaleY: 4.0
});
hatEquipment.y = -350; // Top of pet's head
hatEquipment.x = 0;
hatEquipment.down = function (x, y, obj) {
// Deselect hat
var index = storage.equippedItems.indexOf('hat');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (hatEquipment) {
self.removeChild(hatEquipment);
hatEquipment = null;
}
// Check if phone should be equipped
if (storage.ownedItems.indexOf('phone') !== -1 && storage.equippedItems.indexOf('phone') !== -1) {
if (!phoneEquipment) {
phoneEquipment = self.attachAsset('phone', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4.0,
scaleY: 4.0
});
phoneEquipment.y = 0; // Center height
phoneEquipment.x = -400; // Left side of pet
phoneEquipment.down = function (x, y, obj) {
// Deselect phone
var index = storage.equippedItems.indexOf('phone');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (phoneEquipment) {
self.removeChild(phoneEquipment);
phoneEquipment = null;
}
// Check if pizza should be equipped
if (storage.ownedItems.indexOf('pizza') !== -1 && storage.equippedItems.indexOf('pizza') !== -1) {
if (!pizzaEquipment) {
pizzaEquipment = self.attachAsset('pizza', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4.0,
scaleY: 4.0
});
pizzaEquipment.y = 0; // Center height
pizzaEquipment.x = 400; // Right side of pet
pizzaEquipment.down = function (x, y, obj) {
// Deselect pizza
var index = storage.equippedItems.indexOf('pizza');
if (index !== -1) {
storage.equippedItems.splice(index, 1);
}
};
}
} else if (pizzaEquipment) {
self.removeChild(pizzaEquipment);
pizzaEquipment = null;
}
};
self.update = function () {
// Update equipment display
self.updateEquipment();
// Decrease all meters gradually (about 3 points every 1 second at 60fps)
if (LK.ticks % 60 === 0) {
self.hungerLevel = Math.max(0, self.hungerLevel - 3);
self.happinessLevel = Math.max(0, self.happinessLevel - 3);
self.energyLevel = Math.max(0, self.energyLevel - 3);
}
// Change pet appearance based on lowest need
var lowestLevel = Math.min(self.hungerLevel, self.happinessLevel, self.energyLevel);
if (lowestLevel <= 20) {
petGraphics.tint = 0xff0000; // Red when critical
} else if (lowestLevel <= 50) {
petGraphics.tint = 0xffaa00; // Orange when low
} else {
petGraphics.tint = 0xffffff; // Normal color
}
// Animate pet slightly
if (LK.ticks % 180 === 0) {
tween(petGraphics, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 300,
easing: tween.easeOut
});
}
if (LK.ticks % 180 === 30) {
tween(petGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 300,
easing: tween.easeOut
});
}
};
self.feed = function () {
self.hungerLevel = Math.min(100, self.hungerLevel + 4);
// Happy bounce animation
tween(petGraphics, {
scaleY: 1.3
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
self.play = function () {
self.happinessLevel = Math.min(100, self.happinessLevel + 4);
// Wiggle animation
tween(petGraphics, {
rotation: 0.2
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
rotation: -0.2
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
rotation: 0
}, {
duration: 100,
easing: tween.easeOut
});
}
});
}
});
};
self.rest = function () {
self.energyLevel = Math.min(100, self.energyLevel + 4);
// Gentle pulse animation
tween(petGraphics, {
alpha: 0.7
}, {
duration: 400,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(petGraphics, {
alpha: 1.0
}, {
duration: 400,
easing: tween.easeOut
});
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var survivalTime = 0;
// Create pet
var pet = game.addChild(new Tamagochi());
pet.x = 2048 / 2;
pet.y = 800;
// Create meters
var hungerMeter = game.addChild(new MeterDisplay('hunger', 0x4CAF50));
hungerMeter.x = 724;
hungerMeter.y = 1200;
var happinessMeter = game.addChild(new MeterDisplay('happiness', 0x2196F3));
happinessMeter.x = 724;
happinessMeter.y = 1300;
var energyMeter = game.addChild(new MeterDisplay('energy', 0xFF9800));
energyMeter.x = 724;
energyMeter.y = 1400;
// Create meter labels
var hungerLabel = new Text2('HUNGER', {
size: 72,
fill: 0x333333
});
hungerLabel.anchor.set(1, 0.5);
hungerLabel.x = 700;
hungerLabel.y = 1200;
game.addChild(hungerLabel);
var happinessLabel = new Text2('HAPPY', {
size: 72,
fill: 0x333333
});
happinessLabel.anchor.set(1, 0.5);
happinessLabel.x = 700;
happinessLabel.y = 1300;
game.addChild(happinessLabel);
var energyLabel = new Text2('ENERGY', {
size: 72,
fill: 0x333333
});
energyLabel.anchor.set(1, 0.5);
energyLabel.x = 700;
energyLabel.y = 1400;
game.addChild(energyLabel);
// Create care buttons
var feedButton = game.addChild(new CareButton('feed', 0x4CAF50));
feedButton.x = 600;
feedButton.y = 1600;
var playButton = game.addChild(new CareButton('play', 0x2196F3));
playButton.x = 1024;
playButton.y = 1600;
var sleepButton = game.addChild(new CareButton('sleep', 0xFF9800));
sleepButton.x = 1448;
sleepButton.y = 1600;
// Create shop button
var shopButtonGraphics = game.attachAsset('shopButton', {
anchorX: 0.5,
anchorY: 0.5
});
shopButtonGraphics.x = 1750;
shopButtonGraphics.y = 1600;
var shopButtonText = new Text2('SHOP', {
size: 48,
fill: 0xFFFFFF
});
shopButtonText.anchor.set(0.5, 0.5);
shopButtonText.x = 1750;
shopButtonText.y = 1600;
game.addChild(shopButtonText);
shopButtonGraphics.down = function (x, y, obj) {
shop.visible = true;
};
// Create shop
var shop = game.addChild(new Shop());
shop.x = 2048 / 2;
shop.y = 2732 / 2;
// Create coins display
var coinsText = new Text2('Coins: ' + storage.coins, {
size: 60,
fill: 0xFFFFFF
});
coinsText.anchor.set(1, 0);
coinsText.x = 2048 - 100;
coinsText.y = 100;
game.addChild(coinsText);
// Score display
var scoreText = new Text2('Time: 0s', {
size: 96,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Pet name display
var petNameText = new Text2('MY TAMAGOCHI', {
size: 120,
fill: 0xFFFFFF
});
petNameText.anchor.set(0.5, 0.5);
petNameText.x = 2048 / 2;
petNameText.y = 400;
game.addChild(petNameText);
game.update = function () {
// Update survival time (increment every second)
if (LK.ticks % 60 === 0) {
survivalTime++;
LK.setScore(survivalTime);
scoreText.setText('Time: ' + survivalTime + 's');
}
// Update meter displays
hungerMeter.updateMeter(pet.hungerLevel);
happinessMeter.updateMeter(pet.happinessLevel);
energyMeter.updateMeter(pet.energyLevel);
// Check for game over
if (pet.hungerLevel <= 0 || pet.happinessLevel <= 0 || pet.energyLevel <= 0) {
LK.showGameOver();
}
};