/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Doll = Container.expand(function () {
var self = Container.call(this);
var dollBody = self.attachAsset('dollBody', {
anchorX: 0.5,
anchorY: 1.0
});
var dollHead = self.attachAsset('dollHead', {
anchorX: 0.5,
anchorY: 0.5,
y: -250
});
self.isWatching = false;
self.turnAround = function () {
self.isWatching = !self.isWatching;
var targetRotation = self.isWatching ? Math.PI : 0;
tween(self, {
rotation: targetRotation
}, {
duration: 500,
easing: tween.easeInOut
});
};
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;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// 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 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 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.style.fill = '#FF0000';
doll.turnAround();
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.style.fill = '#00FF00';
doll.turnAround();
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);
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,187 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Doll = Container.expand(function () {
+ var self = Container.call(this);
+ var dollBody = self.attachAsset('dollBody', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ var dollHead = self.attachAsset('dollHead', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ y: -250
+ });
+ self.isWatching = false;
+ self.turnAround = function () {
+ self.isWatching = !self.isWatching;
+ var targetRotation = self.isWatching ? Math.PI : 0;
+ tween(self, {
+ rotation: targetRotation
+ }, {
+ duration: 500,
+ easing: tween.easeInOut
+ });
+ };
+ 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;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// 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 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 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.style.fill = '#FF0000';
+ doll.turnAround();
+ 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.style.fill = '#00FF00';
+ doll.turnAround();
+ 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);
+};
\ No newline at end of file