User prompt
oyun tahtaları biraz daha büyük olmalı çok küçükl gözüküyor ayrıca gemileri dikene yada yatay olarak yerleştirebilmeliyiz. yerleştirdiğim gemileri oyuna başla butonuna basana kadar tekrar yerleştirebilmeliyim. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
gemiler oyun başlamadan önce kenarda durmalı sürükleyerek ve çevirerek istediğmiiz yere koymalıyız. ayrıca vurduğumuz kısımlar için kutular olmalı soldan aşağı doğru harf harf yukarıdan soldan sağa doğru sayı olmak üzere ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
Battleship Commander
Initial prompt
amiral battı oyunu yapmak istiyorum
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var GridCell = Container.expand(function (x, y, isPlayer) { var self = Container.call(this); self.gridX = x; self.gridY = y; self.isPlayer = isPlayer; self.hasShip = false; self.isHit = false; self.isMiss = false; self.shipId = -1; var cellGraphics = self.attachAsset('water', { anchorX: 0, anchorY: 0 }); self.setShip = function (shipId) { self.hasShip = true; self.shipId = shipId; if (self.isPlayer) { cellGraphics.tint = 0x808080; } }; self.markHit = function () { self.isHit = true; cellGraphics.tint = 0xFF0000; }; self.markMiss = function () { self.isMiss = true; cellGraphics.tint = 0xFFFFFF; }; self.markSunk = function () { cellGraphics.tint = 0x2F2F2F; }; self.down = function (x, y, obj) { if (gameState === 'placement' && self.isPlayer) { selectCellForPlacement(self.gridX, self.gridY); } else if (gameState === 'playing' && !self.isPlayer && !self.isHit && !self.isMiss) { playerShoot(self.gridX, self.gridY); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var Ship = function Ship(length, id) { this.length = length; this.id = id; this.hits = 0; this.cells = []; this.isSunk = false; this.addCell = function (x, y) { this.cells.push({ x: x, y: y }); }; this.hit = function () { this.hits++; if (this.hits >= this.length) { this.isSunk = true; return true; } return false; }; }; var GRID_SIZE = 10; var CELL_SIZE = 80; var GRID_OFFSET_X = 200; var PLAYER_GRID_Y = 1500; var AI_GRID_Y = 300; var playerGrid = []; var aiGrid = []; var playerShips = []; var aiShips = []; var gameState = 'placement'; // 'placement', 'playing', 'gameOver' var currentPlayer = 'player'; // 'player', 'ai' var selectedX = -1; var selectedY = -1; var currentShipToPlace = 0; var isHorizontalPlacement = true; var shipLengths = [5, 4, 3, 3, 2]; // UI Elements var statusText = new Text2('Place your ships', { size: 60, fill: 0x000000 }); statusText.anchor.set(0.5, 0); LK.gui.top.addChild(statusText); var instructionText = new Text2('Tap to place ship horizontally', { size: 40, fill: 0x000000 }); instructionText.anchor.set(0.5, 0); instructionText.y = 100; LK.gui.top.addChild(instructionText); // Initialize grids function initializeGrids() { // Player grid for (var x = 0; x < GRID_SIZE; x++) { playerGrid[x] = []; for (var y = 0; y < GRID_SIZE; y++) { var cell = new GridCell(x, y, true); cell.x = GRID_OFFSET_X + x * CELL_SIZE; cell.y = PLAYER_GRID_Y + y * CELL_SIZE; playerGrid[x][y] = cell; game.addChild(cell); } } // AI grid for (var x = 0; x < GRID_SIZE; x++) { aiGrid[x] = []; for (var y = 0; y < GRID_SIZE; y++) { var cell = new GridCell(x, y, false); cell.x = GRID_OFFSET_X + x * CELL_SIZE; cell.y = AI_GRID_Y + y * CELL_SIZE; aiGrid[x][y] = cell; game.addChild(cell); } } // Add grid labels var playerLabel = new Text2('Your Fleet', { size: 50, fill: 0x000000 }); playerLabel.anchor.set(0.5, 0.5); playerLabel.x = GRID_OFFSET_X + GRID_SIZE * CELL_SIZE / 2; playerLabel.y = PLAYER_GRID_Y - 50; game.addChild(playerLabel); var aiLabel = new Text2('Enemy Waters', { size: 50, fill: 0x000000 }); aiLabel.anchor.set(0.5, 0.5); aiLabel.x = GRID_OFFSET_X + GRID_SIZE * CELL_SIZE / 2; aiLabel.y = AI_GRID_Y - 50; game.addChild(aiLabel); } function selectCellForPlacement(x, y) { selectedX = x; selectedY = y; if (canPlaceShip(x, y, shipLengths[currentShipToPlace], isHorizontalPlacement, true)) { placeShip(x, y, shipLengths[currentShipToPlace], isHorizontalPlacement, true, currentShipToPlace); currentShipToPlace++; if (currentShipToPlace >= shipLengths.length) { gameState = 'playing'; statusText.setText('Fire at enemy positions!'); instructionText.setText('Tap enemy grid to shoot'); placeAIShips(); } else { statusText.setText('Place ship ' + (currentShipToPlace + 1) + ' (length: ' + shipLengths[currentShipToPlace] + ')'); } } } function canPlaceShip(startX, startY, length, horizontal, isPlayer) { var grid = isPlayer ? playerGrid : aiGrid; for (var i = 0; i < length; i++) { var x = horizontal ? startX + i : startX; var y = horizontal ? startY : startY + i; if (x >= GRID_SIZE || y >= GRID_SIZE || x < 0 || y < 0) { return false; } if (grid[x][y].hasShip) { return false; } } return true; } function placeShip(startX, startY, length, horizontal, isPlayer, shipId) { var grid = isPlayer ? playerGrid : aiGrid; var ships = isPlayer ? playerShips : aiShips; var ship = new Ship(length, shipId); for (var i = 0; i < length; i++) { var x = horizontal ? startX + i : startX; var y = horizontal ? startY : startY + i; grid[x][y].setShip(shipId); ship.addCell(x, y); } ships.push(ship); } function placeAIShips() { for (var i = 0; i < shipLengths.length; i++) { var placed = false; var attempts = 0; while (!placed && attempts < 100) { var x = Math.floor(Math.random() * GRID_SIZE); var y = Math.floor(Math.random() * GRID_SIZE); var horizontal = Math.random() < 0.5; if (canPlaceShip(x, y, shipLengths[i], horizontal, false)) { placeShip(x, y, shipLengths[i], horizontal, false, i); placed = true; } attempts++; } } } function playerShoot(x, y) { if (currentPlayer !== 'player') return; var cell = aiGrid[x][y]; var hit = false; if (cell.hasShip) { cell.markHit(); hit = true; LK.getSound('hit').play(); var ship = aiShips[cell.shipId]; var sunk = ship.hit(); if (sunk) { LK.getSound('sunk').play(); markShipAsSunk(ship, false); if (checkVictory(false)) { gameState = 'gameOver'; statusText.setText('Victory! You sank all enemy ships!'); instructionText.setText(''); LK.showYouWin(); return; } } statusText.setText('Hit! Fire again!'); } else { cell.markMiss(); LK.getSound('miss').play(); currentPlayer = 'ai'; statusText.setText('Miss! Enemy turn...'); LK.setTimeout(function () { aiTurn(); }, 1000); } } function aiTurn() { if (currentPlayer !== 'ai') return; var x, y; var attempts = 0; do { x = Math.floor(Math.random() * GRID_SIZE); y = Math.floor(Math.random() * GRID_SIZE); attempts++; } while ((playerGrid[x][y].isHit || playerGrid[x][y].isMiss) && attempts < 100); var cell = playerGrid[x][y]; var hit = false; if (cell.hasShip) { cell.markHit(); hit = true; LK.getSound('hit').play(); var ship = playerShips[cell.shipId]; var sunk = ship.hit(); if (sunk) { LK.getSound('sunk').play(); markShipAsSunk(ship, true); if (checkVictory(true)) { gameState = 'gameOver'; statusText.setText('Defeat! Enemy sank all your ships!'); instructionText.setText(''); LK.showGameOver(); return; } } statusText.setText('Enemy hit your ship!'); LK.setTimeout(function () { aiTurn(); }, 1000); } else { cell.markMiss(); LK.getSound('miss').play(); currentPlayer = 'player'; statusText.setText('Enemy missed! Your turn!'); } } function markShipAsSunk(ship, isPlayer) { var grid = isPlayer ? playerGrid : aiGrid; for (var i = 0; i < ship.cells.length; i++) { var cellPos = ship.cells[i]; grid[cellPos.x][cellPos.y].markSunk(); } } function checkVictory(playerLost) { var ships = playerLost ? playerShips : aiShips; for (var i = 0; i < ships.length; i++) { if (!ships[i].isSunk) { return false; } } return true; } // Double tap to rotate ship during placement var lastTapTime = 0; game.down = function (x, y, obj) { if (gameState === 'placement') { var currentTime = Date.now(); if (currentTime - lastTapTime < 300) { isHorizontalPlacement = !isHorizontalPlacement; instructionText.setText('Tap to place ship ' + (isHorizontalPlacement ? 'horizontally' : 'vertically')); } lastTapTime = currentTime; } }; // Initialize the game initializeGrids();
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,311 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var GridCell = Container.expand(function (x, y, isPlayer) {
+ var self = Container.call(this);
+ self.gridX = x;
+ self.gridY = y;
+ self.isPlayer = isPlayer;
+ self.hasShip = false;
+ self.isHit = false;
+ self.isMiss = false;
+ self.shipId = -1;
+ var cellGraphics = self.attachAsset('water', {
+ anchorX: 0,
+ anchorY: 0
+ });
+ self.setShip = function (shipId) {
+ self.hasShip = true;
+ self.shipId = shipId;
+ if (self.isPlayer) {
+ cellGraphics.tint = 0x808080;
+ }
+ };
+ self.markHit = function () {
+ self.isHit = true;
+ cellGraphics.tint = 0xFF0000;
+ };
+ self.markMiss = function () {
+ self.isMiss = true;
+ cellGraphics.tint = 0xFFFFFF;
+ };
+ self.markSunk = function () {
+ cellGraphics.tint = 0x2F2F2F;
+ };
+ self.down = function (x, y, obj) {
+ if (gameState === 'placement' && self.isPlayer) {
+ selectCellForPlacement(self.gridX, self.gridY);
+ } else if (gameState === 'playing' && !self.isPlayer && !self.isHit && !self.isMiss) {
+ playerShoot(self.gridX, self.gridY);
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var Ship = function Ship(length, id) {
+ this.length = length;
+ this.id = id;
+ this.hits = 0;
+ this.cells = [];
+ this.isSunk = false;
+ this.addCell = function (x, y) {
+ this.cells.push({
+ x: x,
+ y: y
+ });
+ };
+ this.hit = function () {
+ this.hits++;
+ if (this.hits >= this.length) {
+ this.isSunk = true;
+ return true;
+ }
+ return false;
+ };
+};
+var GRID_SIZE = 10;
+var CELL_SIZE = 80;
+var GRID_OFFSET_X = 200;
+var PLAYER_GRID_Y = 1500;
+var AI_GRID_Y = 300;
+var playerGrid = [];
+var aiGrid = [];
+var playerShips = [];
+var aiShips = [];
+var gameState = 'placement'; // 'placement', 'playing', 'gameOver'
+var currentPlayer = 'player'; // 'player', 'ai'
+var selectedX = -1;
+var selectedY = -1;
+var currentShipToPlace = 0;
+var isHorizontalPlacement = true;
+var shipLengths = [5, 4, 3, 3, 2];
+// UI Elements
+var statusText = new Text2('Place your ships', {
+ size: 60,
+ fill: 0x000000
+});
+statusText.anchor.set(0.5, 0);
+LK.gui.top.addChild(statusText);
+var instructionText = new Text2('Tap to place ship horizontally', {
+ size: 40,
+ fill: 0x000000
+});
+instructionText.anchor.set(0.5, 0);
+instructionText.y = 100;
+LK.gui.top.addChild(instructionText);
+// Initialize grids
+function initializeGrids() {
+ // Player grid
+ for (var x = 0; x < GRID_SIZE; x++) {
+ playerGrid[x] = [];
+ for (var y = 0; y < GRID_SIZE; y++) {
+ var cell = new GridCell(x, y, true);
+ cell.x = GRID_OFFSET_X + x * CELL_SIZE;
+ cell.y = PLAYER_GRID_Y + y * CELL_SIZE;
+ playerGrid[x][y] = cell;
+ game.addChild(cell);
+ }
+ }
+ // AI grid
+ for (var x = 0; x < GRID_SIZE; x++) {
+ aiGrid[x] = [];
+ for (var y = 0; y < GRID_SIZE; y++) {
+ var cell = new GridCell(x, y, false);
+ cell.x = GRID_OFFSET_X + x * CELL_SIZE;
+ cell.y = AI_GRID_Y + y * CELL_SIZE;
+ aiGrid[x][y] = cell;
+ game.addChild(cell);
+ }
+ }
+ // Add grid labels
+ var playerLabel = new Text2('Your Fleet', {
+ size: 50,
+ fill: 0x000000
+ });
+ playerLabel.anchor.set(0.5, 0.5);
+ playerLabel.x = GRID_OFFSET_X + GRID_SIZE * CELL_SIZE / 2;
+ playerLabel.y = PLAYER_GRID_Y - 50;
+ game.addChild(playerLabel);
+ var aiLabel = new Text2('Enemy Waters', {
+ size: 50,
+ fill: 0x000000
+ });
+ aiLabel.anchor.set(0.5, 0.5);
+ aiLabel.x = GRID_OFFSET_X + GRID_SIZE * CELL_SIZE / 2;
+ aiLabel.y = AI_GRID_Y - 50;
+ game.addChild(aiLabel);
+}
+function selectCellForPlacement(x, y) {
+ selectedX = x;
+ selectedY = y;
+ if (canPlaceShip(x, y, shipLengths[currentShipToPlace], isHorizontalPlacement, true)) {
+ placeShip(x, y, shipLengths[currentShipToPlace], isHorizontalPlacement, true, currentShipToPlace);
+ currentShipToPlace++;
+ if (currentShipToPlace >= shipLengths.length) {
+ gameState = 'playing';
+ statusText.setText('Fire at enemy positions!');
+ instructionText.setText('Tap enemy grid to shoot');
+ placeAIShips();
+ } else {
+ statusText.setText('Place ship ' + (currentShipToPlace + 1) + ' (length: ' + shipLengths[currentShipToPlace] + ')');
+ }
+ }
+}
+function canPlaceShip(startX, startY, length, horizontal, isPlayer) {
+ var grid = isPlayer ? playerGrid : aiGrid;
+ for (var i = 0; i < length; i++) {
+ var x = horizontal ? startX + i : startX;
+ var y = horizontal ? startY : startY + i;
+ if (x >= GRID_SIZE || y >= GRID_SIZE || x < 0 || y < 0) {
+ return false;
+ }
+ if (grid[x][y].hasShip) {
+ return false;
+ }
+ }
+ return true;
+}
+function placeShip(startX, startY, length, horizontal, isPlayer, shipId) {
+ var grid = isPlayer ? playerGrid : aiGrid;
+ var ships = isPlayer ? playerShips : aiShips;
+ var ship = new Ship(length, shipId);
+ for (var i = 0; i < length; i++) {
+ var x = horizontal ? startX + i : startX;
+ var y = horizontal ? startY : startY + i;
+ grid[x][y].setShip(shipId);
+ ship.addCell(x, y);
+ }
+ ships.push(ship);
+}
+function placeAIShips() {
+ for (var i = 0; i < shipLengths.length; i++) {
+ var placed = false;
+ var attempts = 0;
+ while (!placed && attempts < 100) {
+ var x = Math.floor(Math.random() * GRID_SIZE);
+ var y = Math.floor(Math.random() * GRID_SIZE);
+ var horizontal = Math.random() < 0.5;
+ if (canPlaceShip(x, y, shipLengths[i], horizontal, false)) {
+ placeShip(x, y, shipLengths[i], horizontal, false, i);
+ placed = true;
+ }
+ attempts++;
+ }
+ }
+}
+function playerShoot(x, y) {
+ if (currentPlayer !== 'player') return;
+ var cell = aiGrid[x][y];
+ var hit = false;
+ if (cell.hasShip) {
+ cell.markHit();
+ hit = true;
+ LK.getSound('hit').play();
+ var ship = aiShips[cell.shipId];
+ var sunk = ship.hit();
+ if (sunk) {
+ LK.getSound('sunk').play();
+ markShipAsSunk(ship, false);
+ if (checkVictory(false)) {
+ gameState = 'gameOver';
+ statusText.setText('Victory! You sank all enemy ships!');
+ instructionText.setText('');
+ LK.showYouWin();
+ return;
+ }
+ }
+ statusText.setText('Hit! Fire again!');
+ } else {
+ cell.markMiss();
+ LK.getSound('miss').play();
+ currentPlayer = 'ai';
+ statusText.setText('Miss! Enemy turn...');
+ LK.setTimeout(function () {
+ aiTurn();
+ }, 1000);
+ }
+}
+function aiTurn() {
+ if (currentPlayer !== 'ai') return;
+ var x, y;
+ var attempts = 0;
+ do {
+ x = Math.floor(Math.random() * GRID_SIZE);
+ y = Math.floor(Math.random() * GRID_SIZE);
+ attempts++;
+ } while ((playerGrid[x][y].isHit || playerGrid[x][y].isMiss) && attempts < 100);
+ var cell = playerGrid[x][y];
+ var hit = false;
+ if (cell.hasShip) {
+ cell.markHit();
+ hit = true;
+ LK.getSound('hit').play();
+ var ship = playerShips[cell.shipId];
+ var sunk = ship.hit();
+ if (sunk) {
+ LK.getSound('sunk').play();
+ markShipAsSunk(ship, true);
+ if (checkVictory(true)) {
+ gameState = 'gameOver';
+ statusText.setText('Defeat! Enemy sank all your ships!');
+ instructionText.setText('');
+ LK.showGameOver();
+ return;
+ }
+ }
+ statusText.setText('Enemy hit your ship!');
+ LK.setTimeout(function () {
+ aiTurn();
+ }, 1000);
+ } else {
+ cell.markMiss();
+ LK.getSound('miss').play();
+ currentPlayer = 'player';
+ statusText.setText('Enemy missed! Your turn!');
+ }
+}
+function markShipAsSunk(ship, isPlayer) {
+ var grid = isPlayer ? playerGrid : aiGrid;
+ for (var i = 0; i < ship.cells.length; i++) {
+ var cellPos = ship.cells[i];
+ grid[cellPos.x][cellPos.y].markSunk();
+ }
+}
+function checkVictory(playerLost) {
+ var ships = playerLost ? playerShips : aiShips;
+ for (var i = 0; i < ships.length; i++) {
+ if (!ships[i].isSunk) {
+ return false;
+ }
+ }
+ return true;
+}
+// Double tap to rotate ship during placement
+var lastTapTime = 0;
+game.down = function (x, y, obj) {
+ if (gameState === 'placement') {
+ var currentTime = Date.now();
+ if (currentTime - lastTapTime < 300) {
+ isHorizontalPlacement = !isHorizontalPlacement;
+ instructionText.setText('Tap to place ship ' + (isHorizontalPlacement ? 'horizontally' : 'vertically'));
+ }
+ lastTapTime = currentTime;
+ }
+};
+// Initialize the game
+initializeGrids();
\ No newline at end of file
water look on top. In-Game asset. 2d. High contrast. No shadows
white location icon look on top. In-Game asset. 2d. High contrast. No shadows
explosion look on top. In-Game asset. 2d. High contrast. No shadows
white smoke look on top. In-Game asset. 2d. High contrast. No shadows
red point icon. In-Game asset. 2d. High contrast. No shadows
2d sea mine In-Game asset. 2d. High contrast. No shadows
warship commander head looking right in game asset. In-Game asset. 2d. High contrast. No shadows
warship commander head looking left in game asset. In-Game asset. 2d. High contrast. No shadows
speech bubble 2d. In-Game asset. 2d. High contrast. No shadows
metal border. In-Game asset. 2d. High contrast. No shadows
metal line. In-Game asset. 2d. High contrast. No shadows