/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var CubeFace = Container.expand(function (color) { var self = Container.call(this); var face = self.attachAsset('cubeFace', { anchorX: 0.5, anchorY: 0.5 }); self.currentColor = color || 0xffffff; face.tint = self.currentColor; self.setColor = function (newColor) { self.currentColor = newColor; face.tint = newColor; }; self.getColor = function () { return self.currentColor; }; return self; }); var GameButton = Container.expand(function (text, color) { var self = Container.call(this); var button = self.attachAsset('scrambleButton', { anchorX: 0.5, anchorY: 0.5 }); button.tint = color || 0x3498db; var buttonText = new Text2(text, { size: 32, fill: 0xFFFFFF }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.down = function (x, y, obj) { tween(self, { scaleX: 0.9, scaleY: 0.9 }, { duration: 100 }); }; self.up = function (x, y, obj) { tween(self, { scaleX: 1, scaleY: 1 }, { duration: 100 }); if (self.onPress) { self.onPress(); } }; return self; }); var RubiksCube = Container.expand(function () { var self = Container.call(this); // Rubik's cube colors self.colors = { white: 0xffffff, red: 0xff0000, blue: 0x0000ff, orange: 0xff8c00, green: 0x00ff00, yellow: 0xffff00 }; self.colorArray = [self.colors.white, self.colors.red, self.colors.blue, self.colors.orange, self.colors.green, self.colors.yellow]; // Create a 3x3 grid of faces self.faces = []; self.targetPattern = []; function initializeCube() { for (var i = 0; i < 9; i++) { var face = new CubeFace(self.colors.white); var row = Math.floor(i / 3); var col = i % 3; face.x = (col - 1) * 190; face.y = (row - 1) * 190; self.faces.push(face); self.addChild(face); } } self.scramble = function () { for (var i = 0; i < self.faces.length; i++) { var randomColorIndex = Math.floor(Math.random() * self.colorArray.length); self.faces[i].setColor(self.colorArray[randomColorIndex]); } }; self.generateTarget = function () { self.targetPattern = []; for (var i = 0; i < 9; i++) { var randomColorIndex = Math.floor(Math.random() * self.colorArray.length); self.targetPattern.push(self.colorArray[randomColorIndex]); } }; self.checkSolution = function () { for (var i = 0; i < self.faces.length; i++) { if (self.faces[i].getColor() !== self.targetPattern[i]) { return false; } } return true; }; self.setToTarget = function () { for (var i = 0; i < self.faces.length; i++) { self.faces[i].setColor(self.targetPattern[i]); } }; self.rotateFace = function (faceIndex) { if (faceIndex >= 0 && faceIndex < self.faces.length) { var currentColorIndex = self.colorArray.indexOf(self.faces[faceIndex].getColor()); var nextColorIndex = (currentColorIndex + 1) % self.colorArray.length; self.faces[faceIndex].setColor(self.colorArray[nextColorIndex]); // Animate rotation tween(self.faces[faceIndex], { rotation: self.faces[faceIndex].rotation + Math.PI * 0.5 }, { duration: 200, easing: tween.easeOut }); LK.getSound('move').play(); } }; initializeCube(); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x34495e }); /**** * Game Code ****/ // Game state variables var currentLevel = storage.currentLevel || 1; var maxLevel = 200; var timeLimit = 180; // 3 minutes in seconds var timeRemaining = timeLimit; var gameActive = false; var hintsAvailable = Math.max(0, 10 - Math.floor(currentLevel / 20)); var movesCount = 0; var gameTimer = null; // UI Elements var levelText = new Text2('Level: 1', { size: 48, fill: 0xFFFFFF }); levelText.anchor.set(0.5, 0); LK.gui.top.addChild(levelText); var timerText = new Text2('3:00', { size: 40, fill: 0xFFFFFF }); timerText.anchor.set(1, 0); timerText.x = -50; timerText.y = 20; LK.gui.topRight.addChild(timerText); var movesText = new Text2('Moves: 0', { size: 36, fill: 0xFFFFFF }); movesText.anchor.set(0, 0); movesText.x = 120; movesText.y = 20; LK.gui.topLeft.addChild(movesText); var hintsText = new Text2('Hints: ' + hintsAvailable, { size: 32, fill: 0xF39C12 }); hintsText.anchor.set(0.5, 1); hintsText.y = -20; LK.gui.bottom.addChild(hintsText); // Game objects var cube = game.addChild(new RubiksCube()); cube.x = 1024; cube.y = 1200; var targetCube = game.addChild(new RubiksCube()); targetCube.x = 1024; targetCube.y = 600; targetCube.alpha = 0.7; // Buttons var scrambleButton = game.addChild(new GameButton('Scramble', 0x3498db)); scrambleButton.x = 400; scrambleButton.y = 2200; var solveButton = game.addChild(new GameButton('Solve', 0x27ae60)); solveButton.x = 800; solveButton.y = 2200; var hintButton = game.addChild(new GameButton('Hint', 0xf39c12)); hintButton.x = 1200; hintButton.y = 2200; var nextLevelButton = game.addChild(new GameButton('Next Level', 0xe74c3c)); nextLevelButton.x = 1600; nextLevelButton.y = 2200; // Target pattern label var targetLabel = new Text2('Target Pattern:', { size: 36, fill: 0xFFFFFF }); targetLabel.anchor.set(0.5, 0.5); targetLabel.x = 1024; targetLabel.y = 450; game.addChild(targetLabel); // Functions function startLevel() { timeLimit = Math.min(300, 180 + Math.floor(currentLevel / 10) * 30); // Progressive time increase timeRemaining = timeLimit; gameActive = true; movesCount = 0; hintsAvailable = Math.max(0, 10 - Math.floor(currentLevel / 20)); levelText.setText('Level: ' + currentLevel); movesText.setText('Moves: 0'); hintsText.setText('Hints: ' + hintsAvailable); cube.generateTarget(); targetCube.targetPattern = cube.targetPattern.slice(); targetCube.setToTarget(); cube.scramble(); // Start timer if (gameTimer) { LK.clearInterval(gameTimer); } gameTimer = LK.setInterval(updateTimer, 1000); } function updateTimer() { if (!gameActive) return; timeRemaining--; var minutes = Math.floor(timeRemaining / 60); var seconds = timeRemaining % 60; var timeString = minutes + ':' + (seconds < 10 ? '0' : '') + seconds; timerText.setText(timeString); if (timeRemaining <= 0) { gameOver(); } } function gameOver() { gameActive = false; if (gameTimer) { LK.clearInterval(gameTimer); gameTimer = null; } LK.showGameOver(); } function levelComplete() { gameActive = false; if (gameTimer) { LK.clearInterval(gameTimer); gameTimer = null; } LK.getSound('levelComplete').play(); LK.effects.flashScreen(0x27ae60, 1000); currentLevel++; storage.currentLevel = currentLevel; if (currentLevel > maxLevel) { LK.showYouWin(); } else { LK.setTimeout(function () { startLevel(); }, 2000); } } function giveHint() { if (hintsAvailable <= 0 || !gameActive) return; hintsAvailable--; hintsText.setText('Hints: ' + hintsAvailable); // Find first incorrect face and highlight it for (var i = 0; i < cube.faces.length; i++) { if (cube.faces[i].getColor() !== cube.targetPattern[i]) { LK.effects.flashObject(cube.faces[i], 0xffff00, 1000); break; } } } // Button handlers scrambleButton.onPress = function () { if (gameActive) { cube.scramble(); movesCount++; movesText.setText('Moves: ' + movesCount); } }; solveButton.onPress = function () { if (gameActive) { cube.setToTarget(); levelComplete(); } }; hintButton.onPress = function () { giveHint(); }; nextLevelButton.onPress = function () { if (!gameActive) { startLevel(); } }; // Cube face interaction with improved touch feedback game.down = function (x, y, obj) { if (!gameActive) return; var gamePos = game.toLocal({ x: x, y: y }); for (var i = 0; i < cube.faces.length; i++) { var face = cube.faces[i]; var faceGlobalX = cube.x + face.x; var faceGlobalY = cube.y + face.y; var distanceX = Math.abs(gamePos.x - faceGlobalX); var distanceY = Math.abs(gamePos.y - faceGlobalY); if (distanceX < 80 && distanceY < 80) { // Add visual feedback tween(face, { scaleX: 1.1, scaleY: 1.1 }, { duration: 100, yoyo: true, repeat: 1 }); cube.rotateFace(i); movesCount++; movesText.setText('Moves: ' + movesCount); // Check if level is complete if (cube.checkSolution()) { levelComplete(); } break; } } }; // Add drag support for easier control var dragStartPos = null; var dragThreshold = 50; game.move = function (x, y, obj) { if (!gameActive || !dragStartPos) return; var gamePos = game.toLocal({ x: x, y: y }); var deltaX = gamePos.x - dragStartPos.x; var deltaY = gamePos.y - dragStartPos.y; var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); if (distance > dragThreshold) { // Find which face we're dragging from for (var i = 0; i < cube.faces.length; i++) { var face = cube.faces[i]; var faceGlobalX = cube.x + face.x; var faceGlobalY = cube.y + face.y; var startDistanceX = Math.abs(dragStartPos.x - faceGlobalX); var startDistanceY = Math.abs(dragStartPos.y - faceGlobalY); if (startDistanceX < 80 && startDistanceY < 80) { cube.rotateFace(i); movesCount++; movesText.setText('Moves: ' + movesCount); if (cube.checkSolution()) { levelComplete(); } dragStartPos = null; // Reset drag break; } } } }; // Track drag start position var originalDown = game.down; game.down = function (x, y, obj) { dragStartPos = game.toLocal({ x: x, y: y }); if (originalDown) originalDown(x, y, obj); }; game.up = function (x, y, obj) { dragStartPos = null; }; // Start first level startLevel(); // Play background music LK.playMusic('bgmusic'); ;
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var CubeFace = Container.expand(function (color) {
var self = Container.call(this);
var face = self.attachAsset('cubeFace', {
anchorX: 0.5,
anchorY: 0.5
});
self.currentColor = color || 0xffffff;
face.tint = self.currentColor;
self.setColor = function (newColor) {
self.currentColor = newColor;
face.tint = newColor;
};
self.getColor = function () {
return self.currentColor;
};
return self;
});
var GameButton = Container.expand(function (text, color) {
var self = Container.call(this);
var button = self.attachAsset('scrambleButton', {
anchorX: 0.5,
anchorY: 0.5
});
button.tint = color || 0x3498db;
var buttonText = new Text2(text, {
size: 32,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
var RubiksCube = Container.expand(function () {
var self = Container.call(this);
// Rubik's cube colors
self.colors = {
white: 0xffffff,
red: 0xff0000,
blue: 0x0000ff,
orange: 0xff8c00,
green: 0x00ff00,
yellow: 0xffff00
};
self.colorArray = [self.colors.white, self.colors.red, self.colors.blue, self.colors.orange, self.colors.green, self.colors.yellow];
// Create a 3x3 grid of faces
self.faces = [];
self.targetPattern = [];
function initializeCube() {
for (var i = 0; i < 9; i++) {
var face = new CubeFace(self.colors.white);
var row = Math.floor(i / 3);
var col = i % 3;
face.x = (col - 1) * 190;
face.y = (row - 1) * 190;
self.faces.push(face);
self.addChild(face);
}
}
self.scramble = function () {
for (var i = 0; i < self.faces.length; i++) {
var randomColorIndex = Math.floor(Math.random() * self.colorArray.length);
self.faces[i].setColor(self.colorArray[randomColorIndex]);
}
};
self.generateTarget = function () {
self.targetPattern = [];
for (var i = 0; i < 9; i++) {
var randomColorIndex = Math.floor(Math.random() * self.colorArray.length);
self.targetPattern.push(self.colorArray[randomColorIndex]);
}
};
self.checkSolution = function () {
for (var i = 0; i < self.faces.length; i++) {
if (self.faces[i].getColor() !== self.targetPattern[i]) {
return false;
}
}
return true;
};
self.setToTarget = function () {
for (var i = 0; i < self.faces.length; i++) {
self.faces[i].setColor(self.targetPattern[i]);
}
};
self.rotateFace = function (faceIndex) {
if (faceIndex >= 0 && faceIndex < self.faces.length) {
var currentColorIndex = self.colorArray.indexOf(self.faces[faceIndex].getColor());
var nextColorIndex = (currentColorIndex + 1) % self.colorArray.length;
self.faces[faceIndex].setColor(self.colorArray[nextColorIndex]);
// Animate rotation
tween(self.faces[faceIndex], {
rotation: self.faces[faceIndex].rotation + Math.PI * 0.5
}, {
duration: 200,
easing: tween.easeOut
});
LK.getSound('move').play();
}
};
initializeCube();
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Game state variables
var currentLevel = storage.currentLevel || 1;
var maxLevel = 200;
var timeLimit = 180; // 3 minutes in seconds
var timeRemaining = timeLimit;
var gameActive = false;
var hintsAvailable = Math.max(0, 10 - Math.floor(currentLevel / 20));
var movesCount = 0;
var gameTimer = null;
// UI Elements
var levelText = new Text2('Level: 1', {
size: 48,
fill: 0xFFFFFF
});
levelText.anchor.set(0.5, 0);
LK.gui.top.addChild(levelText);
var timerText = new Text2('3:00', {
size: 40,
fill: 0xFFFFFF
});
timerText.anchor.set(1, 0);
timerText.x = -50;
timerText.y = 20;
LK.gui.topRight.addChild(timerText);
var movesText = new Text2('Moves: 0', {
size: 36,
fill: 0xFFFFFF
});
movesText.anchor.set(0, 0);
movesText.x = 120;
movesText.y = 20;
LK.gui.topLeft.addChild(movesText);
var hintsText = new Text2('Hints: ' + hintsAvailable, {
size: 32,
fill: 0xF39C12
});
hintsText.anchor.set(0.5, 1);
hintsText.y = -20;
LK.gui.bottom.addChild(hintsText);
// Game objects
var cube = game.addChild(new RubiksCube());
cube.x = 1024;
cube.y = 1200;
var targetCube = game.addChild(new RubiksCube());
targetCube.x = 1024;
targetCube.y = 600;
targetCube.alpha = 0.7;
// Buttons
var scrambleButton = game.addChild(new GameButton('Scramble', 0x3498db));
scrambleButton.x = 400;
scrambleButton.y = 2200;
var solveButton = game.addChild(new GameButton('Solve', 0x27ae60));
solveButton.x = 800;
solveButton.y = 2200;
var hintButton = game.addChild(new GameButton('Hint', 0xf39c12));
hintButton.x = 1200;
hintButton.y = 2200;
var nextLevelButton = game.addChild(new GameButton('Next Level', 0xe74c3c));
nextLevelButton.x = 1600;
nextLevelButton.y = 2200;
// Target pattern label
var targetLabel = new Text2('Target Pattern:', {
size: 36,
fill: 0xFFFFFF
});
targetLabel.anchor.set(0.5, 0.5);
targetLabel.x = 1024;
targetLabel.y = 450;
game.addChild(targetLabel);
// Functions
function startLevel() {
timeLimit = Math.min(300, 180 + Math.floor(currentLevel / 10) * 30); // Progressive time increase
timeRemaining = timeLimit;
gameActive = true;
movesCount = 0;
hintsAvailable = Math.max(0, 10 - Math.floor(currentLevel / 20));
levelText.setText('Level: ' + currentLevel);
movesText.setText('Moves: 0');
hintsText.setText('Hints: ' + hintsAvailable);
cube.generateTarget();
targetCube.targetPattern = cube.targetPattern.slice();
targetCube.setToTarget();
cube.scramble();
// Start timer
if (gameTimer) {
LK.clearInterval(gameTimer);
}
gameTimer = LK.setInterval(updateTimer, 1000);
}
function updateTimer() {
if (!gameActive) return;
timeRemaining--;
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
var timeString = minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
timerText.setText(timeString);
if (timeRemaining <= 0) {
gameOver();
}
}
function gameOver() {
gameActive = false;
if (gameTimer) {
LK.clearInterval(gameTimer);
gameTimer = null;
}
LK.showGameOver();
}
function levelComplete() {
gameActive = false;
if (gameTimer) {
LK.clearInterval(gameTimer);
gameTimer = null;
}
LK.getSound('levelComplete').play();
LK.effects.flashScreen(0x27ae60, 1000);
currentLevel++;
storage.currentLevel = currentLevel;
if (currentLevel > maxLevel) {
LK.showYouWin();
} else {
LK.setTimeout(function () {
startLevel();
}, 2000);
}
}
function giveHint() {
if (hintsAvailable <= 0 || !gameActive) return;
hintsAvailable--;
hintsText.setText('Hints: ' + hintsAvailable);
// Find first incorrect face and highlight it
for (var i = 0; i < cube.faces.length; i++) {
if (cube.faces[i].getColor() !== cube.targetPattern[i]) {
LK.effects.flashObject(cube.faces[i], 0xffff00, 1000);
break;
}
}
}
// Button handlers
scrambleButton.onPress = function () {
if (gameActive) {
cube.scramble();
movesCount++;
movesText.setText('Moves: ' + movesCount);
}
};
solveButton.onPress = function () {
if (gameActive) {
cube.setToTarget();
levelComplete();
}
};
hintButton.onPress = function () {
giveHint();
};
nextLevelButton.onPress = function () {
if (!gameActive) {
startLevel();
}
};
// Cube face interaction with improved touch feedback
game.down = function (x, y, obj) {
if (!gameActive) return;
var gamePos = game.toLocal({
x: x,
y: y
});
for (var i = 0; i < cube.faces.length; i++) {
var face = cube.faces[i];
var faceGlobalX = cube.x + face.x;
var faceGlobalY = cube.y + face.y;
var distanceX = Math.abs(gamePos.x - faceGlobalX);
var distanceY = Math.abs(gamePos.y - faceGlobalY);
if (distanceX < 80 && distanceY < 80) {
// Add visual feedback
tween(face, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 100,
yoyo: true,
repeat: 1
});
cube.rotateFace(i);
movesCount++;
movesText.setText('Moves: ' + movesCount);
// Check if level is complete
if (cube.checkSolution()) {
levelComplete();
}
break;
}
}
};
// Add drag support for easier control
var dragStartPos = null;
var dragThreshold = 50;
game.move = function (x, y, obj) {
if (!gameActive || !dragStartPos) return;
var gamePos = game.toLocal({
x: x,
y: y
});
var deltaX = gamePos.x - dragStartPos.x;
var deltaY = gamePos.y - dragStartPos.y;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > dragThreshold) {
// Find which face we're dragging from
for (var i = 0; i < cube.faces.length; i++) {
var face = cube.faces[i];
var faceGlobalX = cube.x + face.x;
var faceGlobalY = cube.y + face.y;
var startDistanceX = Math.abs(dragStartPos.x - faceGlobalX);
var startDistanceY = Math.abs(dragStartPos.y - faceGlobalY);
if (startDistanceX < 80 && startDistanceY < 80) {
cube.rotateFace(i);
movesCount++;
movesText.setText('Moves: ' + movesCount);
if (cube.checkSolution()) {
levelComplete();
}
dragStartPos = null; // Reset drag
break;
}
}
}
};
// Track drag start position
var originalDown = game.down;
game.down = function (x, y, obj) {
dragStartPos = game.toLocal({
x: x,
y: y
});
if (originalDown) originalDown(x, y, obj);
};
game.up = function (x, y, obj) {
dragStartPos = null;
};
// Start first level
startLevel();
// Play background music
LK.playMusic('bgmusic');
;