User prompt
Enlarge the text at the bottom of the game screen
User prompt
Enlarge the text under Fancy Freeze
User prompt
Make sure the player can pet Fancy Freeze ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
To pet Fancy Freeze the player must tap him
User prompt
Make sure the hunger bar doesn't drain too quickly and make sure you can pet Fancy Freeze he should have hearts floating over his head for a short time to represent his affection ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Instead of a number use bars to display Fancy Freeze's happy and hunger level ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Fancy Freeze should be happy when you pet him ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Remove the penguin belly and eye textures
User prompt
Fix code
User prompt
Please fix the bug: 'Uncaught TypeError: penguin.contains is not a function' in or related to this line: 'if (obj === penguin || penguin.contains(obj)) {' Line Number: 233
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));' Line Number: 233
Code edit (1 edits merged)
Please save this source code
User prompt
Fancy Freeze: Penguin Care
Initial prompt
Let's make a cute virtual pet type game where you take care of a penguin Named Fancy Freeze, you will feed him blue Popsicles and pet him
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphics = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
self.lifespan = 1500;
self.startY = self.y;
self.update = function () {
self.lifespan -= 16;
if (self.lifespan <= 0) {
self.destroy();
}
};
return self;
});
var Penguin = Container.expand(function () {
var self = Container.call(this);
var penguinBody = self.attachAsset('penguin', {
anchorX: 0.5,
anchorY: 0.5
});
self.happiness = 100;
self.hunger = 50;
self.lastHappiness = 100;
self.lastHunger = 50;
self.isAnimating = false;
self.feed = function () {
if (!self.isAnimating && self.hunger > 0) {
self.isAnimating = true;
self.hunger = Math.min(100, self.hunger + 25);
LK.getSound('eat').play();
tween(self, {
scaleY: 0.9
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
}
});
}
});
}
};
self.pet = function () {
if (!self.isAnimating) {
self.isAnimating = true;
self.happiness = Math.min(100, self.happiness + 20);
LK.getSound('pet').play();
tween(self, {
rotation: 0.1
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
rotation: -0.1
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
rotation: 0
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
}
});
}
});
}
});
for (var h = 0; h < 3; h++) {
var newHeart = new Heart();
newHeart.x = self.x + (Math.random() - 0.5) * 100;
newHeart.y = self.y - 100;
hearts.push(newHeart);
game.addChild(newHeart);
tween(newHeart, {
y: newHeart.y - 150,
alpha: 0
}, {
duration: 1500,
easing: tween.easeOut
});
}
if (self.happiness >= 95) {
LK.getSound('happy').play();
tween(self, {
scaleX: 1.15,
scaleY: 1.15
}, {
duration: 300,
easing: tween.elasticOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut
});
}
});
}
}
};
self.down = function (x, y, obj) {
self.pet();
}; //{1k_new}
self.update = function () {
self.hunger = Math.max(0, self.hunger - 0.01);
self.happiness = Math.max(0, self.happiness - 0.02);
};
return self;
});
var Popsicle = Container.expand(function () {
var self = Container.call(this);
var popsicleBody = self.attachAsset('popsicle', {
anchorX: 0.5,
anchorY: 0.5
});
var popsicleStick = self.attachAsset('popsicleStick', {
anchorX: 0.5,
anchorY: 0
});
popsicleStick.y = 70;
self.isEaten = false;
self.down = function (x, y, obj) {
if (!self.isEaten) {
self.isEaten = true;
LK.getSound('eat').play();
tween(self, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var penguin = game.addChild(new Penguin());
penguin.x = 1024;
penguin.y = 1200;
var happinessLabel = new Text2('Happiness', {
size: 40,
fill: 0xFFFFFF
});
happinessLabel.anchor.set(0, 0);
happinessLabel.x = 100;
happinessLabel.y = 100;
LK.gui.addChild(happinessLabel);
var happinessBgBar = LK.getAsset('happinessBg', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 150
});
LK.gui.addChild(happinessBgBar);
var happinessBar = LK.getAsset('happinessBar', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 150
});
LK.gui.addChild(happinessBar);
var hungerLabel = new Text2('Hunger', {
size: 40,
fill: 0xFFFFFF
});
hungerLabel.anchor.set(0, 0);
hungerLabel.x = 100;
hungerLabel.y = 300;
LK.gui.addChild(hungerLabel);
var hungerBgBar = LK.getAsset('hungerBg', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 350
});
LK.gui.addChild(hungerBgBar);
var hungerBar = LK.getAsset('hungerBar', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 350
});
LK.gui.addChild(hungerBar);
var hearts = [];
var popsicles = [];
var popsicleSpawnTimer = LK.setInterval(function () {
if (popsicles.length < 3) {
var newPopsicle = game.addChild(new Popsicle());
newPopsicle.x = 300 + Math.random() * 1448;
newPopsicle.y = 400;
popsicles.push(newPopsicle);
}
}, 3000);
var instructionsLabel = new Text2('Tap Fancy Freeze to pet • Tap popsicles to feed', {
size: 48,
fill: 0xFFFFFF
});
instructionsLabel.anchor.set(0.5, 0);
instructionsLabel.x = 1024;
instructionsLabel.y = 2600;
game.addChild(instructionsLabel);
var penguinNameLabel = new Text2('Fancy Freeze', {
size: 120,
fill: 0xFFFFFF
});
penguinNameLabel.anchor.set(0.5, 0);
penguinNameLabel.x = 1024;
penguinNameLabel.y = 1550;
game.addChild(penguinNameLabel);
game.down = function (x, y, obj) {
var clickedPenguin = obj === penguin;
if (!clickedPenguin) {
var current = obj.parent;
while (current) {
if (current === penguin) {
clickedPenguin = true;
break;
}
current = current.parent;
}
}
if (clickedPenguin) {
penguin.pet();
}
};
game.update = function () {
penguin.update();
var targetHappinessWidth = penguin.happiness / 100 * 500;
var targetHungerWidth = penguin.hunger / 100 * 500;
happinessBar.scaleX = targetHappinessWidth / happinessBar.width;
hungerBar.scaleX = targetHungerWidth / hungerBar.width;
for (var h = hearts.length - 1; h >= 0; h--) {
hearts[h].update();
if (hearts[h].lifespan <= 0) {
hearts[h].destroy();
hearts.splice(h, 1);
}
}
for (var i = popsicles.length - 1; i >= 0; i--) {
if (popsicles[i].isEaten) {
penguin.feed();
popsicles.splice(i, 1);
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphics = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
self.lifespan = 1500;
self.startY = self.y;
self.update = function () {
self.lifespan -= 16;
if (self.lifespan <= 0) {
self.destroy();
}
};
return self;
});
var Penguin = Container.expand(function () {
var self = Container.call(this);
var penguinBody = self.attachAsset('penguin', {
anchorX: 0.5,
anchorY: 0.5
});
self.happiness = 100;
self.hunger = 50;
self.lastHappiness = 100;
self.lastHunger = 50;
self.isAnimating = false;
self.feed = function () {
if (!self.isAnimating && self.hunger > 0) {
self.isAnimating = true;
self.hunger = Math.min(100, self.hunger + 25);
LK.getSound('eat').play();
tween(self, {
scaleY: 0.9
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
}
});
}
});
}
};
self.pet = function () {
if (!self.isAnimating) {
self.isAnimating = true;
self.happiness = Math.min(100, self.happiness + 20);
LK.getSound('pet').play();
tween(self, {
rotation: 0.1
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
rotation: -0.1
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
rotation: 0
}, {
duration: 100,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
}
});
}
});
}
});
for (var h = 0; h < 3; h++) {
var newHeart = new Heart();
newHeart.x = self.x + (Math.random() - 0.5) * 100;
newHeart.y = self.y - 100;
hearts.push(newHeart);
game.addChild(newHeart);
tween(newHeart, {
y: newHeart.y - 150,
alpha: 0
}, {
duration: 1500,
easing: tween.easeOut
});
}
if (self.happiness >= 95) {
LK.getSound('happy').play();
tween(self, {
scaleX: 1.15,
scaleY: 1.15
}, {
duration: 300,
easing: tween.elasticOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut
});
}
});
}
}
};
self.down = function (x, y, obj) {
self.pet();
}; //{1k_new}
self.update = function () {
self.hunger = Math.max(0, self.hunger - 0.01);
self.happiness = Math.max(0, self.happiness - 0.02);
};
return self;
});
var Popsicle = Container.expand(function () {
var self = Container.call(this);
var popsicleBody = self.attachAsset('popsicle', {
anchorX: 0.5,
anchorY: 0.5
});
var popsicleStick = self.attachAsset('popsicleStick', {
anchorX: 0.5,
anchorY: 0
});
popsicleStick.y = 70;
self.isEaten = false;
self.down = function (x, y, obj) {
if (!self.isEaten) {
self.isEaten = true;
LK.getSound('eat').play();
tween(self, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var penguin = game.addChild(new Penguin());
penguin.x = 1024;
penguin.y = 1200;
var happinessLabel = new Text2('Happiness', {
size: 40,
fill: 0xFFFFFF
});
happinessLabel.anchor.set(0, 0);
happinessLabel.x = 100;
happinessLabel.y = 100;
LK.gui.addChild(happinessLabel);
var happinessBgBar = LK.getAsset('happinessBg', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 150
});
LK.gui.addChild(happinessBgBar);
var happinessBar = LK.getAsset('happinessBar', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 150
});
LK.gui.addChild(happinessBar);
var hungerLabel = new Text2('Hunger', {
size: 40,
fill: 0xFFFFFF
});
hungerLabel.anchor.set(0, 0);
hungerLabel.x = 100;
hungerLabel.y = 300;
LK.gui.addChild(hungerLabel);
var hungerBgBar = LK.getAsset('hungerBg', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 350
});
LK.gui.addChild(hungerBgBar);
var hungerBar = LK.getAsset('hungerBar', {
anchorX: 0,
anchorY: 0,
x: 100,
y: 350
});
LK.gui.addChild(hungerBar);
var hearts = [];
var popsicles = [];
var popsicleSpawnTimer = LK.setInterval(function () {
if (popsicles.length < 3) {
var newPopsicle = game.addChild(new Popsicle());
newPopsicle.x = 300 + Math.random() * 1448;
newPopsicle.y = 400;
popsicles.push(newPopsicle);
}
}, 3000);
var instructionsLabel = new Text2('Tap Fancy Freeze to pet • Tap popsicles to feed', {
size: 48,
fill: 0xFFFFFF
});
instructionsLabel.anchor.set(0.5, 0);
instructionsLabel.x = 1024;
instructionsLabel.y = 2600;
game.addChild(instructionsLabel);
var penguinNameLabel = new Text2('Fancy Freeze', {
size: 120,
fill: 0xFFFFFF
});
penguinNameLabel.anchor.set(0.5, 0);
penguinNameLabel.x = 1024;
penguinNameLabel.y = 1550;
game.addChild(penguinNameLabel);
game.down = function (x, y, obj) {
var clickedPenguin = obj === penguin;
if (!clickedPenguin) {
var current = obj.parent;
while (current) {
if (current === penguin) {
clickedPenguin = true;
break;
}
current = current.parent;
}
}
if (clickedPenguin) {
penguin.pet();
}
};
game.update = function () {
penguin.update();
var targetHappinessWidth = penguin.happiness / 100 * 500;
var targetHungerWidth = penguin.hunger / 100 * 500;
happinessBar.scaleX = targetHappinessWidth / happinessBar.width;
hungerBar.scaleX = targetHungerWidth / hungerBar.width;
for (var h = hearts.length - 1; h >= 0; h--) {
hearts[h].update();
if (hearts[h].lifespan <= 0) {
hearts[h].destroy();
hearts.splice(h, 1);
}
}
for (var i = popsicles.length - 1; i >= 0; i--) {
if (popsicles[i].isEaten) {
penguin.feed();
popsicles.splice(i, 1);
}
}
};