/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Player = Container.expand(function () { var self = Container.call(this); self.standing = self.attachAsset('player', { anchorX: 0.5, anchorY: 1.0, visible: true }); self.ducking = self.attachAsset('playerDucking', { anchorX: 0.5, anchorY: 1.0, visible: false }); self.isDucking = false; self.duckDuration = 30; // Increase duck duration self.lives = 3; // Initialize player with 3 lives self.isHit = false; self.hitCooldown = 0; self.duck = function () { if (!self.isDucking && !self.isHit) { self.isDucking = true; self.standing.visible = false; self.ducking.visible = true; self.duckDuration = 40; // Reset duck duration LK.getSound('duck').play(); } }; self.stand = function () { if (self.isDucking && !self.isHit) { self.isDucking = false; self.standing.visible = true; self.ducking.visible = false; } }; self.hit = function () { if (!self.isHit && !self.isDucking) { self.isHit = true; self.hitCooldown = 30; // Half a second at 60fps LK.getSound('hit').play(); LK.effects.flashObject(self, 0xff0000, 500); self.lives--; if (self.lives <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } }; self.update = function () { if (self.isDucking) { self.duckDuration--; if (self.duckDuration <= 0) { self.stand(); } } if (self.isHit) { self.hitCooldown--; if (self.hitCooldown <= 0) { self.isHit = false; } } }; return self; }); var SnowMound = Container.expand(function () { var self = Container.call(this); var moundGraphics = self.attachAsset('snowMound', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Snowball = Container.expand(function () { var self = Container.call(this); var snowballGraphics = self.attachAsset('snowball', { anchorX: 0.5, anchorY: 0.5 }); self.speedX = 0; self.speedY = 0; self.gravity = 0.2; self.active = true; self.reset = function (startX, startY, targetX, targetY) { self.x = startX; self.y = startY; // Calculate angle to target var dx = targetX - startX; var dy = targetY - startY; var distance = Math.sqrt(dx * dx + dy * dy); // Normalize speed based on distance var speed = 10 + Math.random() * 5; self.speedX = dx / distance * speed; // Launch with upward arc var arcHeight = -10 - Math.random() * 5; self.speedY = dy / distance * speed + arcHeight; self.active = true; self.alpha = 1; }; self.update = function () { if (self.active) { self.x += self.speedX; self.y += self.speedY; self.speedY += self.gravity; // Check if out of bounds if (self.y > 2732 || self.x < -200 || self.x > 2248) { self.active = false; } } }; return self; }); var Tree = Container.expand(function () { var self = Container.call(this); var trunk = self.attachAsset('treeTrunk', { anchorX: 0.5, anchorY: 1.0, y: 0 }); var foliage = self.attachAsset('tree', { anchorX: 0.5, anchorY: 1.0, y: -trunk.height, scaleX: 1.5, // Increase scale for more detailed foliage scaleY: 1.5, // Increase scale for more detailed foliage tint: 0x228B22 // Change color for a more vibrant green }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ // Create background elements var background = game.addChild(LK.getAsset('background', { anchorX: 0, anchorY: 0, x: 0, y: 0 })); var ground = game.addChild(LK.getAsset('ground', { anchorX: 0.5, anchorY: 0, x: 1024, y: 2332 })); // Add additional background details var clouds = []; for (var i = 0; i < 3; i++) { var cloud = LK.getAsset('cloud', { anchorX: 0.5, anchorY: 0.5, x: Math.random() * 2048, y: Math.random() * 500 }); clouds.push(cloud); game.addChild(cloud); } var mountains = []; for (var i = 0; i < 2; i++) { var mountain = LK.getAsset('mountain', { anchorX: 0.5, anchorY: 1.0, x: 512 + i * 1024, y: 2332 }); mountains.push(mountain); game.addChild(mountain); } // Add trees in the background var trees = []; for (var i = 0; i < 5; i++) { var tree = new Tree(); tree.x = 200 + i * 400 + Math.random() * 100; tree.y = 2332; trees.push(tree); game.addChild(tree); } // Add snow mounds for cover var mounds = []; for (var i = 0; i < 3; i++) { var mound = new SnowMound(); mound.x = 500 + i * 500; mound.y = 2232; mounds.push(mound); game.addChild(mound); } // Create player var player = new Player(); player.x = 1024; player.y = 2332; game.addChild(player); // Create pool of snowballs var snowballs = []; var maxSnowballs = 20; for (var i = 0; i < maxSnowballs; i++) { var snowball = new Snowball(); snowball.active = false; snowball.visible = false; snowballs.push(snowball); game.addChild(snowball); } // Create score text var scoreTxt = new Text2('Score: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(1, 0); LK.gui.topRight.addChild(scoreTxt); // Create survival time text var timeTxt = new Text2('Time: 0s', { size: 80, fill: 0xFFFFFF }); timeTxt.anchor.set(1, 0); // Position above the score timeTxt.y = -scoreTxt.height - 20; LK.gui.topRight.addChild(timeTxt); // Game state variables var gameStarted = false; var gameTime = 0; var nextSnowballTime = 0; var snowballFrequency = 60; // Frames between snowballs var difficultyIncreaseTime = 600; // 10 seconds at 60fps // Touch input handling game.down = function (x, y, obj) { if (!gameStarted) { gameStarted = true; LK.playMusic('winterMusic'); } if (!player.isDucking) { player.duck(); } }; game.up = function (x, y, obj) { player.stand(); }; // Spawn a snowball function spawnSnowball() { // Find an inactive snowball for (var i = 0; i < snowballs.length; i++) { if (!snowballs[i].active) { // Spawn from the left or right side var side = Math.random() < 0.5 ? -100 : 2148; var startX = side; var startY = 1500 + Math.random() * 500; // Target the player with some random offset var targetX = player.x + (Math.random() * 200 - 100); var targetY = player.y - player.standing.height / 2; snowballs[i].reset(startX, startY, targetX, targetY); snowballs[i].visible = true; LK.getSound('throw').play(); return; } } } // Check if player is protected by a snow mound function isPlayerProtected() { if (!player.isDucking) { return false; } for (var i = 0; i < mounds.length; i++) { var mound = mounds[i]; var distance = Math.abs(player.x - mound.x); if (distance < 250) { return true; } } return false; } // Update game game.update = function () { if (!gameStarted) { return; } // Update game time gameTime++; if (gameTime % 60 === 0) { // Update time display every second var seconds = Math.floor(gameTime / 60); timeTxt.setText('Time: ' + seconds + 's'); // Add points for surviving LK.setScore(LK.getScore() + 1); scoreTxt.setText('Score: ' + LK.getScore()); } // Increase difficulty over time if (gameTime % difficultyIncreaseTime === 0 && snowballFrequency > 15) { snowballFrequency = Math.max(15, snowballFrequency - 5); } // Spawn snowballs if (gameTime >= nextSnowballTime) { spawnSnowball(); nextSnowballTime = gameTime + snowballFrequency - Math.random() * 10; } // Update player player.update(); // Update snowballs and check collisions for (var i = 0; i < snowballs.length; i++) { var snowball = snowballs[i]; if (snowball.active) { snowball.update(); // Check collision with player if (snowball.intersects(player) && !player.isHit) { player.hit(); snowball.active = false; snowball.visible = false; } // Make snowball invisible if not active if (!snowball.active) { snowball.visible = false; } } } }; // Start game with welcome message var welcomeTxt = new Text2('Tap and hold to hide\nbehind snow pillars\nand avoid snowballs!', { size: 100, fill: 0xFFFFFF, align: 'center' }); welcomeTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(welcomeTxt); // Remove welcome message after first touch game.down = function (x, y, obj) { if (!gameStarted) { gameStarted = true; LK.gui.center.removeChild(welcomeTxt); LK.playMusic('winterMusic'); } player.duck(); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Player = Container.expand(function () {
var self = Container.call(this);
self.standing = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0,
visible: true
});
self.ducking = self.attachAsset('playerDucking', {
anchorX: 0.5,
anchorY: 1.0,
visible: false
});
self.isDucking = false;
self.duckDuration = 30; // Increase duck duration
self.lives = 3; // Initialize player with 3 lives
self.isHit = false;
self.hitCooldown = 0;
self.duck = function () {
if (!self.isDucking && !self.isHit) {
self.isDucking = true;
self.standing.visible = false;
self.ducking.visible = true;
self.duckDuration = 40; // Reset duck duration
LK.getSound('duck').play();
}
};
self.stand = function () {
if (self.isDucking && !self.isHit) {
self.isDucking = false;
self.standing.visible = true;
self.ducking.visible = false;
}
};
self.hit = function () {
if (!self.isHit && !self.isDucking) {
self.isHit = true;
self.hitCooldown = 30; // Half a second at 60fps
LK.getSound('hit').play();
LK.effects.flashObject(self, 0xff0000, 500);
self.lives--;
if (self.lives <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
self.update = function () {
if (self.isDucking) {
self.duckDuration--;
if (self.duckDuration <= 0) {
self.stand();
}
}
if (self.isHit) {
self.hitCooldown--;
if (self.hitCooldown <= 0) {
self.isHit = false;
}
}
};
return self;
});
var SnowMound = Container.expand(function () {
var self = Container.call(this);
var moundGraphics = self.attachAsset('snowMound', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Snowball = Container.expand(function () {
var self = Container.call(this);
var snowballGraphics = self.attachAsset('snowball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.gravity = 0.2;
self.active = true;
self.reset = function (startX, startY, targetX, targetY) {
self.x = startX;
self.y = startY;
// Calculate angle to target
var dx = targetX - startX;
var dy = targetY - startY;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize speed based on distance
var speed = 10 + Math.random() * 5;
self.speedX = dx / distance * speed;
// Launch with upward arc
var arcHeight = -10 - Math.random() * 5;
self.speedY = dy / distance * speed + arcHeight;
self.active = true;
self.alpha = 1;
};
self.update = function () {
if (self.active) {
self.x += self.speedX;
self.y += self.speedY;
self.speedY += self.gravity;
// Check if out of bounds
if (self.y > 2732 || self.x < -200 || self.x > 2248) {
self.active = false;
}
}
};
return self;
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var trunk = self.attachAsset('treeTrunk', {
anchorX: 0.5,
anchorY: 1.0,
y: 0
});
var foliage = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 1.0,
y: -trunk.height,
scaleX: 1.5,
// Increase scale for more detailed foliage
scaleY: 1.5,
// Increase scale for more detailed foliage
tint: 0x228B22 // Change color for a more vibrant green
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Create background elements
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
}));
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0.5,
anchorY: 0,
x: 1024,
y: 2332
}));
// Add additional background details
var clouds = [];
for (var i = 0; i < 3; i++) {
var cloud = LK.getAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5,
x: Math.random() * 2048,
y: Math.random() * 500
});
clouds.push(cloud);
game.addChild(cloud);
}
var mountains = [];
for (var i = 0; i < 2; i++) {
var mountain = LK.getAsset('mountain', {
anchorX: 0.5,
anchorY: 1.0,
x: 512 + i * 1024,
y: 2332
});
mountains.push(mountain);
game.addChild(mountain);
}
// Add trees in the background
var trees = [];
for (var i = 0; i < 5; i++) {
var tree = new Tree();
tree.x = 200 + i * 400 + Math.random() * 100;
tree.y = 2332;
trees.push(tree);
game.addChild(tree);
}
// Add snow mounds for cover
var mounds = [];
for (var i = 0; i < 3; i++) {
var mound = new SnowMound();
mound.x = 500 + i * 500;
mound.y = 2232;
mounds.push(mound);
game.addChild(mound);
}
// Create player
var player = new Player();
player.x = 1024;
player.y = 2332;
game.addChild(player);
// Create pool of snowballs
var snowballs = [];
var maxSnowballs = 20;
for (var i = 0; i < maxSnowballs; i++) {
var snowball = new Snowball();
snowball.active = false;
snowball.visible = false;
snowballs.push(snowball);
game.addChild(snowball);
}
// Create score text
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
// Create survival time text
var timeTxt = new Text2('Time: 0s', {
size: 80,
fill: 0xFFFFFF
});
timeTxt.anchor.set(1, 0);
// Position above the score
timeTxt.y = -scoreTxt.height - 20;
LK.gui.topRight.addChild(timeTxt);
// Game state variables
var gameStarted = false;
var gameTime = 0;
var nextSnowballTime = 0;
var snowballFrequency = 60; // Frames between snowballs
var difficultyIncreaseTime = 600; // 10 seconds at 60fps
// Touch input handling
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
LK.playMusic('winterMusic');
}
if (!player.isDucking) {
player.duck();
}
};
game.up = function (x, y, obj) {
player.stand();
};
// Spawn a snowball
function spawnSnowball() {
// Find an inactive snowball
for (var i = 0; i < snowballs.length; i++) {
if (!snowballs[i].active) {
// Spawn from the left or right side
var side = Math.random() < 0.5 ? -100 : 2148;
var startX = side;
var startY = 1500 + Math.random() * 500;
// Target the player with some random offset
var targetX = player.x + (Math.random() * 200 - 100);
var targetY = player.y - player.standing.height / 2;
snowballs[i].reset(startX, startY, targetX, targetY);
snowballs[i].visible = true;
LK.getSound('throw').play();
return;
}
}
}
// Check if player is protected by a snow mound
function isPlayerProtected() {
if (!player.isDucking) {
return false;
}
for (var i = 0; i < mounds.length; i++) {
var mound = mounds[i];
var distance = Math.abs(player.x - mound.x);
if (distance < 250) {
return true;
}
}
return false;
}
// Update game
game.update = function () {
if (!gameStarted) {
return;
}
// Update game time
gameTime++;
if (gameTime % 60 === 0) {
// Update time display every second
var seconds = Math.floor(gameTime / 60);
timeTxt.setText('Time: ' + seconds + 's');
// Add points for surviving
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
}
// Increase difficulty over time
if (gameTime % difficultyIncreaseTime === 0 && snowballFrequency > 15) {
snowballFrequency = Math.max(15, snowballFrequency - 5);
}
// Spawn snowballs
if (gameTime >= nextSnowballTime) {
spawnSnowball();
nextSnowballTime = gameTime + snowballFrequency - Math.random() * 10;
}
// Update player
player.update();
// Update snowballs and check collisions
for (var i = 0; i < snowballs.length; i++) {
var snowball = snowballs[i];
if (snowball.active) {
snowball.update();
// Check collision with player
if (snowball.intersects(player) && !player.isHit) {
player.hit();
snowball.active = false;
snowball.visible = false;
}
// Make snowball invisible if not active
if (!snowball.active) {
snowball.visible = false;
}
}
}
};
// Start game with welcome message
var welcomeTxt = new Text2('Tap and hold to hide\nbehind snow pillars\nand avoid snowballs!', {
size: 100,
fill: 0xFFFFFF,
align: 'center'
});
welcomeTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(welcomeTxt);
// Remove welcome message after first touch
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
LK.gui.center.removeChild(welcomeTxt);
LK.playMusic('winterMusic');
}
player.duck();
};