User prompt
Make the text appear on the middle of the screen and redistribute button should appear on players side
User prompt
Make the hands face each other. Player 2s hands looking up.
User prompt
Fingers are not distinguishable. Fingers are too close
User prompt
I want the hand sprites to have a right number of fingers up
Code edit (1 edits merged)
Please save this source code
User prompt
Finger Battle: Two-Player Strategy
Initial prompt
I want to make 2 player turn based game. The rules are simple. Both players start with 1 finger up from both hands. Player 1 select which of his hand he wants to play. Then selects one of the hands of the opponent. Then opponent has sum of the total selected hands finger up. If the number is greater than 5 it should take mode 5. If one of the players has 5 fingers up that hand is out of play. If the number of fingers from one hand is even and the other hand is out of play player can chose to share even fingers to both of the hands instead of atacking.
/****
* Classes
****/
var Hand = Container.expand(function (player, isLeft, fingers) {
var self = Container.call(this);
self.player = player;
self.isLeft = isLeft;
self.fingers = fingers || 1;
self.isEliminated = false;
self.isSelected = false;
self.handGraphics = null;
self.selectedOutline = null;
self.eliminatedOverlay = null;
self.fingerDots = [];
self.updateDisplay = function () {
// Remove all existing hand graphics
for (var i = 0; i < self.fingerDots.length; i++) {
self.removeChild(self.fingerDots[i]);
}
self.fingerDots = [];
if (self.handGraphics) {
self.removeChild(self.handGraphics);
}
if (self.selectedOutline) {
self.removeChild(self.selectedOutline);
}
if (self.eliminatedOverlay) {
self.removeChild(self.eliminatedOverlay);
}
// Create hand palm
self.handGraphics = self.attachAsset('handPalm', {
anchorX: 0.5,
anchorY: 0.5
});
// Create fingers (5 total, positioned from left to right with better spacing)
var fingerPositions = [-60, -30, 0, 30, 60];
for (var i = 0; i < 5; i++) {
var isFingerUp = i < self.fingers && !self.isEliminated;
var fingerAsset = isFingerUp ? 'fingerUp' : 'fingerDown';
var fingerY = isFingerUp ? -110 : -70;
var finger = self.attachAsset(fingerAsset, {
anchorX: 0.5,
anchorY: 1.0,
x: fingerPositions[i],
y: fingerY
});
// Make down fingers more visually distinct
if (!isFingerUp) {
finger.alpha = 0.6;
}
self.fingerDots.push(finger);
}
// Add selection outline if selected
if (self.isSelected) {
self.selectedOutline = self.attachAsset('selectedHandOutline', {
anchorX: 0.5,
anchorY: 0.5
});
self.selectedOutline.alpha = 0.3;
}
// Add eliminated overlay if eliminated
if (self.isEliminated) {
self.eliminatedOverlay = self.attachAsset('eliminatedOverlay', {
anchorX: 0.5,
anchorY: 0.5
});
self.eliminatedOverlay.alpha = 0.7;
}
};
self.setFingers = function (count) {
self.fingers = count;
if (self.fingers >= 5) {
if (self.fingers === 5) {
self.isEliminated = true;
LK.getSound('eliminate').play();
} else {
self.fingers = self.fingers % 5;
}
}
self.updateDisplay();
};
self.revive = function (count) {
self.isEliminated = false;
self.fingers = count;
self.updateDisplay();
};
self.setSelected = function (selected) {
self.isSelected = selected;
self.updateDisplay();
};
self.down = function (x, y, obj) {
if (!self.isEliminated) {
if (currentPlayer === self.player && gamePhase === 'selectOwn') {
// Select own hand
selectedHand = self;
gamePhase = 'selectTarget';
clearSelection();
self.setSelected(true);
LK.getSound('select').play();
} else if (currentPlayer !== self.player && gamePhase === 'selectTarget') {
// Attack opponent's hand
var damage = selectedHand.fingers;
self.setFingers(self.fingers + damage);
LK.getSound('attack').play();
clearSelection();
checkWinCondition();
if (!gameEnded) {
switchPlayer();
}
}
}
};
self.updateDisplay();
return self;
});
var RedistributeButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphics = self.attachAsset('redistributeButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.buttonText = new Text2('Redistribute', {
size: 40,
fill: '#ffffff'
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.down = function (x, y, obj) {
if (canRedistribute()) {
redistribute();
LK.getSound('select').play();
switchPlayer();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game state variables
var currentPlayer = 1;
var gamePhase = 'selectOwn'; // 'selectOwn', 'selectTarget'
var selectedHand = null;
var gameEnded = false;
// Player hands
var player1LeftHand, player1RightHand;
var player2LeftHand, player2RightHand;
var redistributeButton;
// UI elements
var turnText, instructionText;
// Initialize hands
player1LeftHand = game.addChild(new Hand(1, true, 1));
player1RightHand = game.addChild(new Hand(1, false, 1));
player2LeftHand = game.addChild(new Hand(2, true, 1));
player2RightHand = game.addChild(new Hand(2, false, 1));
// Position hands
player1LeftHand.x = 2048 / 2 - 150;
player1LeftHand.y = 2732 - 200;
player1RightHand.x = 2048 / 2 + 150;
player1RightHand.y = 2732 - 200;
player2LeftHand.x = 2048 / 2 - 150;
player2LeftHand.y = 200;
player2RightHand.x = 2048 / 2 + 150;
player2RightHand.y = 200;
// Create redistribute button
redistributeButton = game.addChild(new RedistributeButton());
redistributeButton.x = 2048 / 2;
redistributeButton.y = 2732 / 2 + 100;
redistributeButton.visible = false;
// UI Text
turnText = new Text2('Player 1 Turn', {
size: 60,
fill: '#ffffff'
});
turnText.anchor.set(0.5, 0);
LK.gui.top.addChild(turnText);
instructionText = new Text2('Select your hand', {
size: 40,
fill: '#ffffff'
});
instructionText.anchor.set(0.5, 0);
instructionText.y = 80;
LK.gui.top.addChild(instructionText);
function clearSelection() {
player1LeftHand.setSelected(false);
player1RightHand.setSelected(false);
player2LeftHand.setSelected(false);
player2RightHand.setSelected(false);
}
function switchPlayer() {
currentPlayer = currentPlayer === 1 ? 2 : 1;
gamePhase = 'selectOwn';
selectedHand = null;
turnText.setText('Player ' + currentPlayer + ' Turn');
instructionText.setText('Select your hand');
redistributeButton.visible = canRedistribute();
}
function canRedistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedCount = 0;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedCount++;
} else {
activeHand = hands[i];
}
}
return eliminatedCount === 1 && activeHand && activeHand.fingers % 2 === 0 && activeHand.fingers > 0;
}
function redistribute() {
var hands = getCurrentPlayerHands();
var activeHand = null;
var eliminatedHand = null;
for (var i = 0; i < hands.length; i++) {
if (hands[i].isEliminated) {
eliminatedHand = hands[i];
} else {
activeHand = hands[i];
}
}
if (activeHand && eliminatedHand) {
var fingersToRedistribute = activeHand.fingers / 2;
activeHand.setFingers(fingersToRedistribute);
eliminatedHand.revive(fingersToRedistribute);
}
redistributeButton.visible = false;
}
function getCurrentPlayerHands() {
if (currentPlayer === 1) {
return [player1LeftHand, player1RightHand];
} else {
return [player2LeftHand, player2RightHand];
}
}
function checkWinCondition() {
// Check if player 1 lost
if (player1LeftHand.isEliminated && player1RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 2 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
// Check if player 2 lost
else if (player2LeftHand.isEliminated && player2RightHand.isEliminated) {
gameEnded = true;
turnText.setText('Player 1 Wins!');
instructionText.setText('Game Over');
LK.getSound('win').play();
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
}
game.update = function () {
if (gameEnded) return;
// Update instruction text based on game phase
if (gamePhase === 'selectOwn') {
if (canRedistribute()) {
instructionText.setText('Select your hand or redistribute');
} else {
instructionText.setText('Select your hand');
}
} else if (gamePhase === 'selectTarget') {
instructionText.setText('Select opponent hand to attack');
}
// Update redistribute button visibility
redistributeButton.visible = gamePhase === 'selectOwn' && canRedistribute();
}; ===================================================================
--- original.js
+++ change.js
@@ -31,20 +31,24 @@
self.handGraphics = self.attachAsset('handPalm', {
anchorX: 0.5,
anchorY: 0.5
});
- // Create fingers (5 total, positioned from left to right)
- var fingerPositions = [-40, -20, 0, 20, 40];
+ // Create fingers (5 total, positioned from left to right with better spacing)
+ var fingerPositions = [-60, -30, 0, 30, 60];
for (var i = 0; i < 5; i++) {
var isFingerUp = i < self.fingers && !self.isEliminated;
var fingerAsset = isFingerUp ? 'fingerUp' : 'fingerDown';
- var fingerY = isFingerUp ? -100 : -80;
+ var fingerY = isFingerUp ? -110 : -70;
var finger = self.attachAsset(fingerAsset, {
anchorX: 0.5,
anchorY: 1.0,
x: fingerPositions[i],
y: fingerY
});
+ // Make down fingers more visually distinct
+ if (!isFingerUp) {
+ finger.alpha = 0.6;
+ }
self.fingerDots.push(finger);
}
// Add selection outline if selected
if (self.isSelected) {