User prompt
kolaylaştıralım
Code edit (1 edits merged)
Please save this source code
User prompt
Kilitli Kapı: Anahtar Seçimi
Initial prompt
5. Kilitli Kapı Amaç: 5 kapıdan doğru anahtarı seç. Nasıl oynanır: Her kapının önünde 1 anahtar var Yanlış seçersen oyun biter Doğru 5 kapıdan geçersen kazanırsın Zorluk: Basit ama şansına bağlı
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Door class: represents a door var Door = Container.expand(function () { var self = Container.call(this); // Attach door asset (rectangle shape) var doorAsset = self.attachAsset('doorShape', { anchorX: 0.5, anchorY: 0.5 }); // Door index (0-4) self.doorIndex = 0; // Animate door opening self.open = function () { // Animate scaleX to 0 (door "opens") tween(doorAsset, { scaleX: 0 }, { duration: 400, easing: tween.cubicIn }); }; // Animate door closing (reset) self.close = function () { doorAsset.scaleX = 1; }; return self; }); // Key class: represents a key in front of a door var Key = Container.expand(function () { var self = Container.call(this); // Attach key asset (ellipse shape) var keyAsset = self.attachAsset('keyShape', { anchorX: 0.5, anchorY: 0.5 }); // Store which door this key belongs to self.doorIndex = 0; // Is this the correct key for the door? self.isCorrect = false; // Visual feedback for selection self.flash = function (color, duration) { tween(keyAsset, { tint: color }, { duration: duration / 2, onFinish: function onFinish() { tween(keyAsset, { tint: 0xf7d560 }, { duration: duration / 2 }); } }); }; // Handle touch/click self.down = function (x, y, obj) { if (game.state !== 'waitingForKey') return; game.handleKeySelection(self); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2d2d2d }); /**** * Game Code ****/ // --- Game Variables --- // --- Asset Initialization (shapes) --- var NUM_DOORS = 5; var doors = []; var keys = []; var currentDoor = 0; // 0-based index var correctKeyIndexes = []; // For each door, which key is correct (0-4) var stateText = null; var infoText = null; // --- GUI: Progress/Info --- stateText = new Text2('', { size: 100, fill: 0xFFFFFF }); stateText.anchor.set(0.5, 0); LK.gui.top.addChild(stateText); infoText = new Text2('', { size: 70, fill: 0xFFE9A0 }); infoText.anchor.set(0.5, 0); LK.gui.bottom.addChild(infoText); // --- Layout Constants --- var gameWidth = 2048; var gameHeight = 2732; var doorsY = 700; var keysY = 1300; var spacing = 340; // horizontal space between doors/keys // --- Helper: Shuffle array in-place --- function shuffle(arr) { for (var i = arr.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } // --- Helper: Set state text --- function setStateText(txt) { stateText.setText(txt); } // --- Helper: Set info text --- function setInfoText(txt) { infoText.setText(txt); } // --- Setup doors and keys --- function setupDoorsAndKeys() { // Remove old doors/keys if any for (var i = 0; i < doors.length; i++) { doors[i].destroy(); } for (var i = 0; i < keys.length; i++) { keys[i].destroy(); } doors = []; keys = []; // For each door, create a Door instance and a Key instance for (var i = 0; i < NUM_DOORS; i++) { // Door var door = new Door(); door.doorIndex = i; // Center doors horizontally door.x = gameWidth / 2 - (NUM_DOORS - 1) * spacing / 2 + i * spacing; door.y = doorsY; game.addChild(door); doors.push(door); // Key var key = new Key(); key.doorIndex = i; // Center keys horizontally, align with doors key.x = door.x; key.y = keysY; game.addChild(key); keys.push(key); } } // --- Setup correct keys for each door (randomly assign one correct key per door) --- function setupCorrectKeys() { correctKeyIndexes = []; // For each door, randomly pick a key index (0-4) as the correct key for (var i = 0; i < NUM_DOORS; i++) { correctKeyIndexes.push(i); } // Shuffle so that correct keys are randomly distributed shuffle(correctKeyIndexes); // Mark keys as correct/incorrect for each door for (var i = 0; i < NUM_DOORS; i++) { for (var j = 0; j < NUM_DOORS; j++) { keys[j].isCorrect = j === correctKeyIndexes[i]; } } } // --- Show only the current door and keys, hide others --- function updateVisibility() { for (var i = 0; i < NUM_DOORS; i++) { doors[i].visible = i === currentDoor; keys[i].visible = true; // All keys visible for selection } } // --- Animate all keys to new positions (shuffle keys for each door) --- function shuffleKeys() { // For each door, shuffle the keys' x positions var positions = []; for (var i = 0; i < NUM_DOORS; i++) { positions.push(gameWidth / 2 - (NUM_DOORS - 1) * spacing / 2 + i * spacing); } shuffle(positions); for (var i = 0; i < NUM_DOORS; i++) { // Animate to new x tween(keys[i], { x: positions[i] }, { duration: 350, easing: tween.cubicInOut }); } } // --- Start a new game --- function startGame() { currentDoor = 0; setStateText("Kapı 1 / 5"); setInfoText("Doğru anahtarı seç!"); setupDoorsAndKeys(); setupCorrectKeys(); updateVisibility(); shuffleKeys(); game.state = 'waitingForKey'; } // --- Handle key selection --- game.handleKeySelection = function (keyObj) { if (game.state !== 'waitingForKey') return; game.state = 'animating'; // Is this the correct key for the current door? var correctKeyIdx = correctKeyIndexes[currentDoor]; var isCorrect = keyObj.doorIndex === correctKeyIdx; if (isCorrect) { // Flash green keyObj.flash(0x6bff6b, 300); // Open the door doors[currentDoor].open(); // Next step after animation LK.setTimeout(function () { if (currentDoor === NUM_DOORS - 1) { // Win! setStateText("Tebrikler!"); setInfoText("5'te 5 yaptın!"); LK.showYouWin(); } else { currentDoor++; setStateText("Kapı " + (currentDoor + 1) + " / 5"); setInfoText("Doğru anahtarı seç!"); // Reset all doors to closed (except opened ones) for (var i = 0; i < NUM_DOORS; i++) { if (i > currentDoor) doors[i].close(); } // Shuffle keys for next door setupCorrectKeys(); shuffleKeys(); updateVisibility(); game.state = 'waitingForKey'; } }, 500); } else { // Flash red keyObj.flash(0xff3b3b, 400); // Shake the door var door = doors[currentDoor]; var origX = door.x; tween(door, { x: origX - 30 }, { duration: 80, onFinish: function onFinish() { tween(door, { x: origX + 30 }, { duration: 80, onFinish: function onFinish() { tween(door, { x: origX }, { duration: 80 }); } }); } }); setInfoText("Yanlış anahtar! Oyun bitti."); LK.setTimeout(function () { LK.showGameOver(); }, 600); } }; // --- Start the game on load --- startGame(); // --- Game update loop (not used, but required for future expansion) --- game.update = function () { // No per-frame logic needed for this game };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,278 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+// Door class: represents a door
+var Door = Container.expand(function () {
+ var self = Container.call(this);
+ // Attach door asset (rectangle shape)
+ var doorAsset = self.attachAsset('doorShape', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Door index (0-4)
+ self.doorIndex = 0;
+ // Animate door opening
+ self.open = function () {
+ // Animate scaleX to 0 (door "opens")
+ tween(doorAsset, {
+ scaleX: 0
+ }, {
+ duration: 400,
+ easing: tween.cubicIn
+ });
+ };
+ // Animate door closing (reset)
+ self.close = function () {
+ doorAsset.scaleX = 1;
+ };
+ return self;
+});
+// Key class: represents a key in front of a door
+var Key = Container.expand(function () {
+ var self = Container.call(this);
+ // Attach key asset (ellipse shape)
+ var keyAsset = self.attachAsset('keyShape', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Store which door this key belongs to
+ self.doorIndex = 0;
+ // Is this the correct key for the door?
+ self.isCorrect = false;
+ // Visual feedback for selection
+ self.flash = function (color, duration) {
+ tween(keyAsset, {
+ tint: color
+ }, {
+ duration: duration / 2,
+ onFinish: function onFinish() {
+ tween(keyAsset, {
+ tint: 0xf7d560
+ }, {
+ duration: duration / 2
+ });
+ }
+ });
+ };
+ // Handle touch/click
+ self.down = function (x, y, obj) {
+ if (game.state !== 'waitingForKey') return;
+ game.handleKeySelection(self);
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x2d2d2d
+});
+
+/****
+* Game Code
+****/
+// --- Game Variables ---
+// --- Asset Initialization (shapes) ---
+var NUM_DOORS = 5;
+var doors = [];
+var keys = [];
+var currentDoor = 0; // 0-based index
+var correctKeyIndexes = []; // For each door, which key is correct (0-4)
+var stateText = null;
+var infoText = null;
+// --- GUI: Progress/Info ---
+stateText = new Text2('', {
+ size: 100,
+ fill: 0xFFFFFF
+});
+stateText.anchor.set(0.5, 0);
+LK.gui.top.addChild(stateText);
+infoText = new Text2('', {
+ size: 70,
+ fill: 0xFFE9A0
+});
+infoText.anchor.set(0.5, 0);
+LK.gui.bottom.addChild(infoText);
+// --- Layout Constants ---
+var gameWidth = 2048;
+var gameHeight = 2732;
+var doorsY = 700;
+var keysY = 1300;
+var spacing = 340; // horizontal space between doors/keys
+// --- Helper: Shuffle array in-place ---
+function shuffle(arr) {
+ for (var i = arr.length - 1; i > 0; i--) {
+ var j = Math.floor(Math.random() * (i + 1));
+ var tmp = arr[i];
+ arr[i] = arr[j];
+ arr[j] = tmp;
+ }
+}
+// --- Helper: Set state text ---
+function setStateText(txt) {
+ stateText.setText(txt);
+}
+// --- Helper: Set info text ---
+function setInfoText(txt) {
+ infoText.setText(txt);
+}
+// --- Setup doors and keys ---
+function setupDoorsAndKeys() {
+ // Remove old doors/keys if any
+ for (var i = 0; i < doors.length; i++) {
+ doors[i].destroy();
+ }
+ for (var i = 0; i < keys.length; i++) {
+ keys[i].destroy();
+ }
+ doors = [];
+ keys = [];
+ // For each door, create a Door instance and a Key instance
+ for (var i = 0; i < NUM_DOORS; i++) {
+ // Door
+ var door = new Door();
+ door.doorIndex = i;
+ // Center doors horizontally
+ door.x = gameWidth / 2 - (NUM_DOORS - 1) * spacing / 2 + i * spacing;
+ door.y = doorsY;
+ game.addChild(door);
+ doors.push(door);
+ // Key
+ var key = new Key();
+ key.doorIndex = i;
+ // Center keys horizontally, align with doors
+ key.x = door.x;
+ key.y = keysY;
+ game.addChild(key);
+ keys.push(key);
+ }
+}
+// --- Setup correct keys for each door (randomly assign one correct key per door) ---
+function setupCorrectKeys() {
+ correctKeyIndexes = [];
+ // For each door, randomly pick a key index (0-4) as the correct key
+ for (var i = 0; i < NUM_DOORS; i++) {
+ correctKeyIndexes.push(i);
+ }
+ // Shuffle so that correct keys are randomly distributed
+ shuffle(correctKeyIndexes);
+ // Mark keys as correct/incorrect for each door
+ for (var i = 0; i < NUM_DOORS; i++) {
+ for (var j = 0; j < NUM_DOORS; j++) {
+ keys[j].isCorrect = j === correctKeyIndexes[i];
+ }
+ }
+}
+// --- Show only the current door and keys, hide others ---
+function updateVisibility() {
+ for (var i = 0; i < NUM_DOORS; i++) {
+ doors[i].visible = i === currentDoor;
+ keys[i].visible = true; // All keys visible for selection
+ }
+}
+// --- Animate all keys to new positions (shuffle keys for each door) ---
+function shuffleKeys() {
+ // For each door, shuffle the keys' x positions
+ var positions = [];
+ for (var i = 0; i < NUM_DOORS; i++) {
+ positions.push(gameWidth / 2 - (NUM_DOORS - 1) * spacing / 2 + i * spacing);
+ }
+ shuffle(positions);
+ for (var i = 0; i < NUM_DOORS; i++) {
+ // Animate to new x
+ tween(keys[i], {
+ x: positions[i]
+ }, {
+ duration: 350,
+ easing: tween.cubicInOut
+ });
+ }
+}
+// --- Start a new game ---
+function startGame() {
+ currentDoor = 0;
+ setStateText("Kapı 1 / 5");
+ setInfoText("Doğru anahtarı seç!");
+ setupDoorsAndKeys();
+ setupCorrectKeys();
+ updateVisibility();
+ shuffleKeys();
+ game.state = 'waitingForKey';
+}
+// --- Handle key selection ---
+game.handleKeySelection = function (keyObj) {
+ if (game.state !== 'waitingForKey') return;
+ game.state = 'animating';
+ // Is this the correct key for the current door?
+ var correctKeyIdx = correctKeyIndexes[currentDoor];
+ var isCorrect = keyObj.doorIndex === correctKeyIdx;
+ if (isCorrect) {
+ // Flash green
+ keyObj.flash(0x6bff6b, 300);
+ // Open the door
+ doors[currentDoor].open();
+ // Next step after animation
+ LK.setTimeout(function () {
+ if (currentDoor === NUM_DOORS - 1) {
+ // Win!
+ setStateText("Tebrikler!");
+ setInfoText("5'te 5 yaptın!");
+ LK.showYouWin();
+ } else {
+ currentDoor++;
+ setStateText("Kapı " + (currentDoor + 1) + " / 5");
+ setInfoText("Doğru anahtarı seç!");
+ // Reset all doors to closed (except opened ones)
+ for (var i = 0; i < NUM_DOORS; i++) {
+ if (i > currentDoor) doors[i].close();
+ }
+ // Shuffle keys for next door
+ setupCorrectKeys();
+ shuffleKeys();
+ updateVisibility();
+ game.state = 'waitingForKey';
+ }
+ }, 500);
+ } else {
+ // Flash red
+ keyObj.flash(0xff3b3b, 400);
+ // Shake the door
+ var door = doors[currentDoor];
+ var origX = door.x;
+ tween(door, {
+ x: origX - 30
+ }, {
+ duration: 80,
+ onFinish: function onFinish() {
+ tween(door, {
+ x: origX + 30
+ }, {
+ duration: 80,
+ onFinish: function onFinish() {
+ tween(door, {
+ x: origX
+ }, {
+ duration: 80
+ });
+ }
+ });
+ }
+ });
+ setInfoText("Yanlış anahtar! Oyun bitti.");
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 600);
+ }
+};
+// --- Start the game on load ---
+startGame();
+// --- Game update loop (not used, but required for future expansion) ---
+game.update = function () {
+ // No per-frame logic needed for this game
+};
\ No newline at end of file