/****
* 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);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 3;
self.isMoving = false;
self.isEliminated = false;
self.animationFrame = 0;
self.startMoving = function () {
if (!self.isEliminated && gameState === 'greenLight') {
self.isMoving = true;
LK.getSound('footsteps').play();
}
};
self.stopMoving = function () {
self.isMoving = false;
LK.getSound('stopSound').play();
};
self.eliminate = function () {
self.isEliminated = true;
self.isMoving = false;
// Elimination animation
tween(playerGraphics, {
rotation: Math.PI,
alpha: 0.3,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
LK.getSound('eliminationSound').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
});
};
self.update = function () {
if (self.isMoving && !self.isEliminated) {
self.y -= self.speed;
// Simple walking animation
self.animationFrame++;
if (self.animationFrame % 10 === 0) {
playerGraphics.rotation = Math.sin(self.animationFrame * 0.3) * 0.1;
}
// Play footsteps sound continuously while moving
if (self.animationFrame % 20 === 0) {
LK.getSound('footsteps').play();
}
} else {
playerGraphics.rotation = 0;
}
};
self.reachedFinish = function () {
// Remove finish line functionality - game continues indefinitely
};
return self;
});
var TrafficLight = Container.expand(function () {
var self = Container.call(this);
var redLight = self.attachAsset('redLight', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
});
var greenLight = self.attachAsset('greenLight', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3,
y: 180
});
self.showRed = function () {
redLight.alpha = 1.0;
greenLight.alpha = 0.3;
tween(redLight, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(redLight, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
self.showGreen = function () {
redLight.alpha = 0.3;
greenLight.alpha = 1.0;
tween(greenLight, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(greenLight, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var gameState = 'waiting'; // 'waiting', 'greenLight', 'redLight', 'gameOver'
var player;
var trafficLight;
var ground;
var startTime;
var gameTimer;
var nextStateTimer;
var level = 1;
var minGreenTime = 2000;
var maxGreenTime = 4000;
var minRedTime = 1500;
var maxRedTime = 3000;
// Create UI elements
var statusText = new Text2('Get Ready!', {
size: 80,
fill: '#ffffff'
});
statusText.anchor.set(0.5, 0.5);
LK.gui.top.addChild(statusText);
var timeText = new Text2('Time: 0s', {
size: 50,
fill: '#ffffff'
});
timeText.anchor.set(1, 0);
LK.gui.topRight.addChild(timeText);
var levelText = new Text2('Level 1', {
size: 50,
fill: '#ffffff'
});
levelText.anchor.set(0, 0);
levelText.x = 120;
LK.gui.topLeft.addChild(levelText);
// Create minimal environment with flat ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 2732 - 300
}));
ground.tint = 0xd4a574; // Light brown sandy ground
player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
trafficLight = game.addChild(new TrafficLight());
trafficLight.x = 2048 / 2;
trafficLight.y = 150;
// Game state management
function startGreenLight() {
gameState = 'greenLight';
statusText.setText('GREEN LIGHT - GO!');
statusText.tint = 0x2ecc71;
trafficLight.showGreen();
LK.getSound('greenLightSound').play();
var duration = Math.random() * (maxGreenTime - minGreenTime) + minGreenTime;
nextStateTimer = LK.setTimeout(startRedLight, duration);
}
function startRedLight() {
gameState = 'redLight';
statusText.setText('RED LIGHT - STOP!');
statusText.tint = 0xe74c3c;
trafficLight.showRed();
LK.getSound('redLightSound').play();
// Check if player was moving during red light
if (player.isMoving) {
player.eliminate();
return;
}
var duration = Math.random() * (maxRedTime - minRedTime) + minRedTime;
nextStateTimer = LK.setTimeout(startGreenLight, duration);
}
function resetGame() {
gameState = 'waiting';
if (nextStateTimer) {
LK.clearTimeout(nextStateTimer);
}
statusText.setText('Get Ready!');
statusText.tint = 0xffffff;
startTime = Date.now();
LK.setTimeout(startGreenLight, 2000);
}
// Touch controls
var isHolding = false;
game.down = function (x, y, obj) {
if (gameState === 'greenLight' && !player.isEliminated) {
isHolding = true;
player.startMoving();
}
};
game.up = function (x, y, obj) {
isHolding = false;
player.stopMoving();
};
game.update = function () {
// Update timer display
if (gameState !== 'waiting' && !player.isEliminated) {
var currentTime = Math.round((Date.now() - startTime) / 1000);
timeText.setText('Time: ' + currentTime + 's');
}
// Check for elimination during red light
if (gameState === 'redLight' && player.isMoving && !player.isEliminated) {
player.eliminate();
}
// Ensure player stops moving when touch is released
if (!isHolding) {
player.stopMoving();
}
};
// Start the game
LK.playMusic('backgroundMusic');
resetGame(); /****
* 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);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 3;
self.isMoving = false;
self.isEliminated = false;
self.animationFrame = 0;
self.startMoving = function () {
if (!self.isEliminated && gameState === 'greenLight') {
self.isMoving = true;
LK.getSound('footsteps').play();
}
};
self.stopMoving = function () {
self.isMoving = false;
LK.getSound('stopSound').play();
};
self.eliminate = function () {
self.isEliminated = true;
self.isMoving = false;
// Elimination animation
tween(playerGraphics, {
rotation: Math.PI,
alpha: 0.3,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
LK.getSound('eliminationSound').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
});
};
self.update = function () {
if (self.isMoving && !self.isEliminated) {
self.y -= self.speed;
// Simple walking animation
self.animationFrame++;
if (self.animationFrame % 10 === 0) {
playerGraphics.rotation = Math.sin(self.animationFrame * 0.3) * 0.1;
}
// Play footsteps sound continuously while moving
if (self.animationFrame % 20 === 0) {
LK.getSound('footsteps').play();
}
} else {
playerGraphics.rotation = 0;
}
};
self.reachedFinish = function () {
// Remove finish line functionality - game continues indefinitely
};
return self;
});
var TrafficLight = Container.expand(function () {
var self = Container.call(this);
var redLight = self.attachAsset('redLight', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
});
var greenLight = self.attachAsset('greenLight', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3,
y: 180
});
self.showRed = function () {
redLight.alpha = 1.0;
greenLight.alpha = 0.3;
tween(redLight, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(redLight, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
self.showGreen = function () {
redLight.alpha = 0.3;
greenLight.alpha = 1.0;
tween(greenLight, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(greenLight, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeOut
});
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var gameState = 'waiting'; // 'waiting', 'greenLight', 'redLight', 'gameOver'
var player;
var trafficLight;
var ground;
var startTime;
var gameTimer;
var nextStateTimer;
var level = 1;
var minGreenTime = 2000;
var maxGreenTime = 4000;
var minRedTime = 1500;
var maxRedTime = 3000;
// Create UI elements
var statusText = new Text2('Get Ready!', {
size: 80,
fill: '#ffffff'
});
statusText.anchor.set(0.5, 0.5);
LK.gui.top.addChild(statusText);
var timeText = new Text2('Time: 0s', {
size: 50,
fill: '#ffffff'
});
timeText.anchor.set(1, 0);
LK.gui.topRight.addChild(timeText);
var levelText = new Text2('Level 1', {
size: 50,
fill: '#ffffff'
});
levelText.anchor.set(0, 0);
levelText.x = 120;
LK.gui.topLeft.addChild(levelText);
// Create minimal environment with flat ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 2732 - 300
}));
ground.tint = 0xd4a574; // Light brown sandy ground
player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
trafficLight = game.addChild(new TrafficLight());
trafficLight.x = 2048 / 2;
trafficLight.y = 150;
// Game state management
function startGreenLight() {
gameState = 'greenLight';
statusText.setText('GREEN LIGHT - GO!');
statusText.tint = 0x2ecc71;
trafficLight.showGreen();
LK.getSound('greenLightSound').play();
var duration = Math.random() * (maxGreenTime - minGreenTime) + minGreenTime;
nextStateTimer = LK.setTimeout(startRedLight, duration);
}
function startRedLight() {
gameState = 'redLight';
statusText.setText('RED LIGHT - STOP!');
statusText.tint = 0xe74c3c;
trafficLight.showRed();
LK.getSound('redLightSound').play();
// Check if player was moving during red light
if (player.isMoving) {
player.eliminate();
return;
}
var duration = Math.random() * (maxRedTime - minRedTime) + minRedTime;
nextStateTimer = LK.setTimeout(startGreenLight, duration);
}
function resetGame() {
gameState = 'waiting';
if (nextStateTimer) {
LK.clearTimeout(nextStateTimer);
}
statusText.setText('Get Ready!');
statusText.tint = 0xffffff;
startTime = Date.now();
LK.setTimeout(startGreenLight, 2000);
}
// Touch controls
var isHolding = false;
game.down = function (x, y, obj) {
if (gameState === 'greenLight' && !player.isEliminated) {
isHolding = true;
player.startMoving();
}
};
game.up = function (x, y, obj) {
isHolding = false;
player.stopMoving();
};
game.update = function () {
// Update timer display
if (gameState !== 'waiting' && !player.isEliminated) {
var currentTime = Math.round((Date.now() - startTime) / 1000);
timeText.setText('Time: ' + currentTime + 's');
}
// Check for elimination during red light
if (gameState === 'redLight' && player.isMoving && !player.isEliminated) {
player.eliminate();
}
// Ensure player stops moving when touch is released
if (!isHolding) {
player.stopMoving();
}
};
// Start the game
LK.playMusic('backgroundMusic');
resetGame();