/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var DeadPerson = Container.expand(function () {
var self = Container.call(this);
var deadPersonGraphics = self.attachAsset('deadPerson', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Doll = Container.expand(function () {
var self = Container.call(this);
var dollBody = self.attachAsset('dollBody', {
anchorX: 0.5,
anchorY: 1.0
});
self.isWatching = false;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isMoving = false;
self.moveSpeed = 3;
self.lastPosition = 0;
self.update = function () {
if (self.isMoving && currentPhase === 'green') {
self.y -= self.moveSpeed;
}
};
return self;
});
var Soldier = Container.expand(function () {
var self = Container.call(this);
var soldierGraphics = self.attachAsset('soldier', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xF4A460
});
/****
* Game Code
****/
// Add background ground pattern
var backgroundGround = game.addChild(LK.getAsset('backgroundGround', {
anchorX: 0,
anchorY: 0,
scaleX: 136.5,
scaleY: 182.1,
x: 0,
y: 0
}));
// Game state variables
var currentPhase = 'green'; // 'green' or 'red'
var phaseTimer = 0;
var nextPhaseChange = 3000; // Initial green phase duration
var gameStarted = false;
var gameEnded = false;
var isPlayerMoving = false;
var playerLastY = 0;
// Create sky background behind finish line
var skyBackground = game.addChild(LK.getAsset('backgroundSky', {
anchorX: 0.5,
anchorY: 1.0,
scaleX: 20.48,
scaleY: 3.0,
x: 2048 / 2,
y: 300
}));
// Create finish line
var finishLine = game.addChild(LK.getAsset('finishLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 300
}));
// Create start line
var startLine = game.addChild(LK.getAsset('startLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2400
}));
// Create player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2400;
playerLastY = player.y;
// Create doll
var doll = game.addChild(new Doll());
doll.x = 2048 / 2;
doll.y = 600;
// Create left soldier
var leftSoldier = game.addChild(new Soldier());
leftSoldier.x = 2048 / 2 - 200;
leftSoldier.y = 600;
// Create right soldier
var rightSoldier = game.addChild(new Soldier());
rightSoldier.x = 2048 / 2 + 200;
rightSoldier.y = 600;
// Create first dead person
var deadPerson1 = game.addChild(new DeadPerson());
deadPerson1.x = 2048 / 2 - 400;
deadPerson1.y = 1500;
// Create second dead person
var deadPerson2 = game.addChild(new DeadPerson());
deadPerson2.x = 2048 / 2 + 400;
deadPerson2.y = 1800;
// Create third dead person
var deadPerson3 = game.addChild(new DeadPerson());
deadPerson3.x = 2048 / 2 - 600;
deadPerson3.y = 2100;
// Create fourth dead person
var deadPerson4 = game.addChild(new DeadPerson());
deadPerson4.x = 2048 / 2 + 600;
deadPerson4.y = 1200;
// Create fifth dead person
var deadPerson5 = game.addChild(new DeadPerson());
deadPerson5.x = 2048 / 2 - 300;
deadPerson5.y = 1000;
// Create sixth dead person
var deadPerson6 = game.addChild(new DeadPerson());
deadPerson6.x = 2048 / 2 + 300;
deadPerson6.y = 2200;
// Create phase indicator text
var phaseText = new Text2('GREEN LIGHT', {
size: 100,
fill: '#00FF00'
});
phaseText.anchor.set(0.5, 0);
LK.gui.top.addChild(phaseText);
// Create instruction text
var instructionText = new Text2('Hold to move, release to stop', {
size: 60,
fill: '#FFFFFF'
});
instructionText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(instructionText);
// Timer for random phase changes
var phaseChangeTimer = LK.setInterval(function () {
if (gameEnded) return;
changePhase();
}, nextPhaseChange);
function changePhase() {
if (currentPhase === 'green') {
// Change to red light
currentPhase = 'red';
phaseText.setText('RED LIGHT');
phaseText.fill = '#FF0000';
LK.getSound('redLight').play();
// Red light duration: 1-3 seconds
nextPhaseChange = 1000 + Math.random() * 2000;
} else {
// Change to green light
currentPhase = 'green';
phaseText.setText('GREEN LIGHT');
phaseText.fill = '#00FF00';
LK.getSound('greenLight').play();
// Green light duration: 2-4 seconds, gets shorter over time
var baseDuration = Math.max(1000, 4000 - LK.getScore() * 100);
nextPhaseChange = baseDuration + Math.random() * 2000;
}
// Clear old timer and set new one
LK.clearInterval(phaseChangeTimer);
phaseChangeTimer = LK.setInterval(changePhase, nextPhaseChange);
}
function checkMovement() {
var currentY = player.y;
var hasMovedUp = currentY < playerLastY - 2; // Small threshold to account for floating point precision
if (currentPhase === 'red' && hasMovedUp) {
// Player caught moving during red light
eliminatePlayer();
}
playerLastY = currentY;
}
function eliminatePlayer() {
gameEnded = true;
LK.clearInterval(phaseChangeTimer);
LK.effects.flashScreen(0xFF0000, 1000);
LK.getSound('elimination').play();
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
function checkVictory() {
if (player.y <= finishLine.y + 50) {
gameEnded = true;
LK.clearInterval(phaseChangeTimer);
LK.getSound('victory').play();
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 500);
}
}
// Touch controls
game.down = function (x, y, obj) {
if (gameEnded) return;
player.isMoving = true;
isPlayerMoving = true;
};
game.up = function (x, y, obj) {
if (gameEnded) return;
player.isMoving = false;
isPlayerMoving = false;
};
// Main game loop
game.update = function () {
if (gameEnded) return;
// Check for movement violations
checkMovement();
// Check for victory condition
checkVictory();
// Update score based on progress
var progress = Math.max(0, startLine.y - player.y);
var maxProgress = startLine.y - finishLine.y;
var scoreProgress = Math.floor(progress / maxProgress * 50);
LK.setScore(scoreProgress);
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var DeadPerson = Container.expand(function () {
var self = Container.call(this);
var deadPersonGraphics = self.attachAsset('deadPerson', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Doll = Container.expand(function () {
var self = Container.call(this);
var dollBody = self.attachAsset('dollBody', {
anchorX: 0.5,
anchorY: 1.0
});
self.isWatching = false;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isMoving = false;
self.moveSpeed = 3;
self.lastPosition = 0;
self.update = function () {
if (self.isMoving && currentPhase === 'green') {
self.y -= self.moveSpeed;
}
};
return self;
});
var Soldier = Container.expand(function () {
var self = Container.call(this);
var soldierGraphics = self.attachAsset('soldier', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xF4A460
});
/****
* Game Code
****/
// Add background ground pattern
var backgroundGround = game.addChild(LK.getAsset('backgroundGround', {
anchorX: 0,
anchorY: 0,
scaleX: 136.5,
scaleY: 182.1,
x: 0,
y: 0
}));
// Game state variables
var currentPhase = 'green'; // 'green' or 'red'
var phaseTimer = 0;
var nextPhaseChange = 3000; // Initial green phase duration
var gameStarted = false;
var gameEnded = false;
var isPlayerMoving = false;
var playerLastY = 0;
// Create sky background behind finish line
var skyBackground = game.addChild(LK.getAsset('backgroundSky', {
anchorX: 0.5,
anchorY: 1.0,
scaleX: 20.48,
scaleY: 3.0,
x: 2048 / 2,
y: 300
}));
// Create finish line
var finishLine = game.addChild(LK.getAsset('finishLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 300
}));
// Create start line
var startLine = game.addChild(LK.getAsset('startLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2400
}));
// Create player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2400;
playerLastY = player.y;
// Create doll
var doll = game.addChild(new Doll());
doll.x = 2048 / 2;
doll.y = 600;
// Create left soldier
var leftSoldier = game.addChild(new Soldier());
leftSoldier.x = 2048 / 2 - 200;
leftSoldier.y = 600;
// Create right soldier
var rightSoldier = game.addChild(new Soldier());
rightSoldier.x = 2048 / 2 + 200;
rightSoldier.y = 600;
// Create first dead person
var deadPerson1 = game.addChild(new DeadPerson());
deadPerson1.x = 2048 / 2 - 400;
deadPerson1.y = 1500;
// Create second dead person
var deadPerson2 = game.addChild(new DeadPerson());
deadPerson2.x = 2048 / 2 + 400;
deadPerson2.y = 1800;
// Create third dead person
var deadPerson3 = game.addChild(new DeadPerson());
deadPerson3.x = 2048 / 2 - 600;
deadPerson3.y = 2100;
// Create fourth dead person
var deadPerson4 = game.addChild(new DeadPerson());
deadPerson4.x = 2048 / 2 + 600;
deadPerson4.y = 1200;
// Create fifth dead person
var deadPerson5 = game.addChild(new DeadPerson());
deadPerson5.x = 2048 / 2 - 300;
deadPerson5.y = 1000;
// Create sixth dead person
var deadPerson6 = game.addChild(new DeadPerson());
deadPerson6.x = 2048 / 2 + 300;
deadPerson6.y = 2200;
// Create phase indicator text
var phaseText = new Text2('GREEN LIGHT', {
size: 100,
fill: '#00FF00'
});
phaseText.anchor.set(0.5, 0);
LK.gui.top.addChild(phaseText);
// Create instruction text
var instructionText = new Text2('Hold to move, release to stop', {
size: 60,
fill: '#FFFFFF'
});
instructionText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(instructionText);
// Timer for random phase changes
var phaseChangeTimer = LK.setInterval(function () {
if (gameEnded) return;
changePhase();
}, nextPhaseChange);
function changePhase() {
if (currentPhase === 'green') {
// Change to red light
currentPhase = 'red';
phaseText.setText('RED LIGHT');
phaseText.fill = '#FF0000';
LK.getSound('redLight').play();
// Red light duration: 1-3 seconds
nextPhaseChange = 1000 + Math.random() * 2000;
} else {
// Change to green light
currentPhase = 'green';
phaseText.setText('GREEN LIGHT');
phaseText.fill = '#00FF00';
LK.getSound('greenLight').play();
// Green light duration: 2-4 seconds, gets shorter over time
var baseDuration = Math.max(1000, 4000 - LK.getScore() * 100);
nextPhaseChange = baseDuration + Math.random() * 2000;
}
// Clear old timer and set new one
LK.clearInterval(phaseChangeTimer);
phaseChangeTimer = LK.setInterval(changePhase, nextPhaseChange);
}
function checkMovement() {
var currentY = player.y;
var hasMovedUp = currentY < playerLastY - 2; // Small threshold to account for floating point precision
if (currentPhase === 'red' && hasMovedUp) {
// Player caught moving during red light
eliminatePlayer();
}
playerLastY = currentY;
}
function eliminatePlayer() {
gameEnded = true;
LK.clearInterval(phaseChangeTimer);
LK.effects.flashScreen(0xFF0000, 1000);
LK.getSound('elimination').play();
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
function checkVictory() {
if (player.y <= finishLine.y + 50) {
gameEnded = true;
LK.clearInterval(phaseChangeTimer);
LK.getSound('victory').play();
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 500);
}
}
// Touch controls
game.down = function (x, y, obj) {
if (gameEnded) return;
player.isMoving = true;
isPlayerMoving = true;
};
game.up = function (x, y, obj) {
if (gameEnded) return;
player.isMoving = false;
isPlayerMoving = false;
};
// Main game loop
game.update = function () {
if (gameEnded) return;
// Check for movement violations
checkMovement();
// Check for victory condition
checkVictory();
// Update score based on progress
var progress = Math.max(0, startLine.y - player.y);
var maxProgress = startLine.y - finishLine.y;
var scoreProgress = Math.floor(progress / maxProgress * 50);
LK.setScore(scoreProgress);
};