/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
currentLevel: 1
});
/****
* Classes
****/
var BulletIndicator = Container.expand(function () {
var self = Container.call(this);
var bulletShape = self.attachAsset('bulletIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
self.setActive = function (active) {
bulletShape.alpha = active ? 1 : 0.3;
};
return self;
});
var Chamber = Container.expand(function () {
var self = Container.call(this);
// Chamber properties
self.loaded = false;
self.selected = false;
self.angle = 0;
// Chamber visuals
var chamberBg = self.attachAsset('chamber', {
anchorX: 0.5,
anchorY: 0.5
});
var chamberHole = self.attachAsset('chamberHole', {
anchorX: 0.5,
anchorY: 0.5
});
self.setLoaded = function (isLoaded) {
self.loaded = isLoaded;
// Only show the red bullet indicator if revealed
if (self.revealed && isLoaded) {
chamberHole.destroy();
chamberHole = self.attachAsset('chamberLoaded', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.setRevealed = function (isRevealed) {
self.revealed = isRevealed;
if (isRevealed && self.loaded) {
chamberHole.destroy();
chamberHole = self.attachAsset('chamberLoaded', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.setSelected = function (isSelected) {
self.selected = isSelected;
if (isSelected) {
tween(chamberBg, {
alpha: 0.7
}, {
duration: 200
});
LK.getSound('click').play(); // Play sound when chamber is selected
} else {
tween(chamberBg, {
alpha: 1
}, {
duration: 200
});
}
};
self.setAngle = function (newAngle) {
self.angle = newAngle;
};
self.down = function (x, y, obj) {
if (!gameActive || spinningCylinder) {
return;
}
self.setSelected(true);
selectedChamber = self;
};
return self;
});
var Gun = Container.expand(function () {
var self = Container.call(this);
// Gun body
var gunBody = self.attachAsset('gun', {
anchorX: 0.5,
anchorY: 0.5
});
// Gun handle
var gunHandle = self.attachAsset('gunHandle', {
anchorX: 0.5,
anchorY: 0,
y: 150
});
// Cylinder
var cylinder = self.attachAsset('cylinder', {
anchorX: 0.5,
anchorY: 0.5,
x: 50,
y: -50
});
// Create chambers
self.chambers = [];
self.setupLevel = function (level) {
// Clear existing chambers
for (var i = 0; i < self.chambers.length; i++) {
self.chambers[i].destroy();
}
self.chambers = [];
// Number of chambers based on level
var chamberCount = 6;
if (level > 5) {
chamberCount = 8;
}
if (level > 10) {
chamberCount = 10;
}
// Number of bullets based on level
var bulletCount = Math.min(Math.floor(level / 2) + 1, chamberCount - 1);
// Create chambers
for (var i = 0; i < chamberCount; i++) {
var chamber = new Chamber();
var angle = i / chamberCount * Math.PI * 2;
var radius = 180;
chamber.x = Math.cos(angle) * radius + 50;
chamber.y = Math.sin(angle) * radius - 50;
chamber.setAngle(angle);
self.chambers.push(chamber);
self.addChild(chamber);
}
// Randomly load chambers
var loadedIndexes = [];
while (loadedIndexes.length < bulletCount) {
var randomIndex = Math.floor(Math.random() * chamberCount);
if (loadedIndexes.indexOf(randomIndex) === -1) {
loadedIndexes.push(randomIndex);
}
}
for (var i = 0; i < loadedIndexes.length; i++) {
self.chambers[loadedIndexes[i]].setLoaded(true);
}
return {
chamberCount: chamberCount,
bulletCount: bulletCount
};
};
self.spinCylinder = function (callback) {
var spinDuration = 1500;
LK.getSound('spin').play();
// Add visual effect for spinning
tween(cylinder, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 200,
yoyo: true,
repeat: 3
});
// Spin animation
var rotations = 3 + Math.random() * 2;
var startRotation = cylinder.rotation;
var endRotation = startRotation + rotations * Math.PI * 2;
tween(cylinder, {
rotation: endRotation
}, {
duration: spinDuration,
easing: tween.easeOutQuad,
onFinish: function onFinish() {
if (callback) {
callback();
}
}
});
// Reorder the chambers randomly
var newOrder = [];
for (var i = 0; i < self.chambers.length; i++) {
newOrder.push(i);
}
// Fisher-Yates shuffle
for (var i = newOrder.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = newOrder[i];
newOrder[i] = newOrder[j];
newOrder[j] = temp;
}
// Store the chamber properties
var loadedStates = [];
for (var i = 0; i < self.chambers.length; i++) {
loadedStates.push({
loaded: self.chambers[i].loaded,
revealed: self.chambers[i].revealed
});
}
// Apply new order
for (var i = 0; i < self.chambers.length; i++) {
self.chambers[i].setLoaded(loadedStates[newOrder[i]].loaded);
self.chambers[i].setRevealed(loadedStates[newOrder[i]].revealed);
}
};
return self;
});
var LevelDisplay = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('levelIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
var levelText = new Text2("Level 1", {
size: 40,
fill: 0xFFFFFF
});
levelText.anchor.set(0.5, 0.5);
self.addChild(levelText);
self.updateLevel = function (level) {
levelText.setText("Level " + level);
};
return self;
});
var Timer = Container.expand(function () {
var self = Container.call(this);
var timerText = new Text2("", {
size: 50,
fill: 0xFFFFFF
});
timerText.anchor.set(0.5, 0.5);
self.addChild(timerText);
self.timeRemaining = 0;
self.active = false;
self.timerInterval = null;
self.startTimer = function (seconds) {
self.timeRemaining = seconds;
self.active = true;
self.updateDisplay();
if (self.timerInterval) {
LK.clearInterval(self.timerInterval);
}
self.timerInterval = LK.setInterval(function () {
if (self.active) {
self.timeRemaining -= 0.1;
if (self.timeRemaining <= 0) {
self.timeRemaining = 0;
self.active = false;
if (self.onTimeUp) {
self.onTimeUp();
}
}
self.updateDisplay();
}
}, 100);
};
self.stopTimer = function () {
self.active = false;
if (self.timerInterval) {
LK.clearInterval(self.timerInterval);
self.timerInterval = null;
}
};
self.updateDisplay = function () {
timerText.setText(Math.ceil(self.timeRemaining).toString());
// Change color based on time remaining
if (self.timeRemaining <= 5) {
timerText.setStyle({
fill: 0xFF3333 //{20} // Slightly lighter red for better visibility
});
} else if (self.timeRemaining <= 10) {
timerText.setStyle({
fill: 0xFFCC33 //{23} // Brighter orange for better visibility
});
} else {
timerText.setStyle({
fill: 0xDDDDDD //{27} // Softer white for better contrast
});
}
};
self.onTimeUp = null;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000022
});
/****
* Game Code
****/
// Game state variables
var currentLevel = storage.currentLevel || 1;
var score = 0;
var highScore = storage.highScore || 0;
var gameActive = false;
var spinningCylinder = false;
var selectedChamber = null;
var levelData = null;
var timePerTurn = 0;
var remainingEmptyChambers = 0;
// Game elements
var gun = new Gun();
var levelDisplay = new LevelDisplay();
var timer = new Timer();
var bulletIndicators = [];
// Score display
var scoreTxt = new Text2("Score: 0", {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
// High score display
var highScoreTxt = new Text2("High Score: " + highScore, {
size: 40,
fill: 0xFFAA00
});
highScoreTxt.anchor.set(0.5, 0);
// Game message text
var messageTxt = new Text2("", {
size: 70,
fill: 0xFFFFFF
});
messageTxt.anchor.set(0.5, 0.5);
// Instructions text
var instructionsTxt = new Text2("", {
size: 50,
fill: 0xFFFFFF
});
instructionsTxt.anchor.set(0.5, 0);
// "Pull Trigger" button
var pullTriggerBtn = new Text2("PULL TRIGGER", {
size: 80,
fill: 0xFFFFFF
});
pullTriggerBtn.anchor.set(0.5, 0.5);
pullTriggerBtn.interactive = true;
// "Spin" button
var spinBtn = new Text2("SPIN CYLINDER", {
size: 60,
fill: 0xFFFFFF
});
spinBtn.anchor.set(0.5, 0.5);
spinBtn.interactive = true;
function setupGame() {
// Add game elements
game.addChild(gun);
gun.x = 2048 / 2;
gun.y = 2732 / 2 - 200;
// Add level display
game.addChild(levelDisplay);
levelDisplay.x = 2048 / 2;
levelDisplay.y = 150;
// Add timer
game.addChild(timer);
timer.x = 2048 / 2;
timer.y = 250;
// Add score text
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 20;
// Add high score text
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 90;
// Add message text
game.addChild(messageTxt);
messageTxt.x = 2048 / 2;
messageTxt.y = 2732 / 2 - 600;
// Add instructions text
game.addChild(instructionsTxt);
instructionsTxt.x = 2048 / 2;
instructionsTxt.y = 2732 - 200;
// Add pull trigger button
game.addChild(pullTriggerBtn);
pullTriggerBtn.x = 2048 / 2;
pullTriggerBtn.y = 2732 - 350;
// Add spin button
game.addChild(spinBtn);
spinBtn.x = 2048 / 2;
spinBtn.y = 2732 - 450;
// Button events
pullTriggerBtn.down = onPullTrigger;
spinBtn.down = onSpinCylinder;
// Timer event
timer.onTimeUp = function () {
if (gameActive) {
showMessage("Time's up!");
LK.setTimeout(function () {
endGame();
}, 1500);
}
};
// Start level
startLevel(currentLevel);
// Play background music
LK.playMusic('tensionMusic', {
loop: true
});
}
function startLevel(level) {
// Reset state
gameActive = true;
spinningCylinder = false;
selectedChamber = null;
currentLevel = level;
// Update level display
levelDisplay.updateLevel(level);
// Set up gun with current level difficulty
levelData = gun.setupLevel(level);
// Calculate time per turn based on level
timePerTurn = Math.max(30 - level * 1.5, 10);
// Set remaining empty chambers
remainingEmptyChambers = levelData.chamberCount - levelData.bulletCount;
// Create bullet indicators
createBulletIndicators(levelData.bulletCount);
// Update score display
updateScoreDisplay();
// Update instructions
instructionsTxt.setText("Select a chamber or spin!");
instructionsTxt.setStyle({
size: 60
});
// Show level start message
showMessage("Level " + level + " - " + levelData.bulletCount + " bullet" + (levelData.bulletCount > 1 ? "s" : ""));
// Start timer
timer.startTimer(timePerTurn);
// Save current level
storage.currentLevel = currentLevel;
}
function createBulletIndicators(bulletCount) {
// Remove existing indicators
for (var i = 0; i < bulletIndicators.length; i++) {
bulletIndicators[i].destroy();
}
bulletIndicators = [];
// Create new indicators
var spacing = 50;
var totalWidth = (bulletCount - 1) * spacing;
var startX = 2048 / 2 - totalWidth / 2;
for (var i = 0; i < bulletCount; i++) {
var indicator = new BulletIndicator();
indicator.x = startX + i * spacing;
indicator.y = 350;
indicator.setActive(true);
game.addChild(indicator);
bulletIndicators.push(indicator);
}
}
function onSpinCylinder() {
if (!gameActive || spinningCylinder) {
return;
}
spinningCylinder = true;
instructionsTxt.setText("Spinning...");
// Reset selection
if (selectedChamber) {
selectedChamber.setSelected(false);
selectedChamber = null;
}
gun.spinCylinder(function () {
spinningCylinder = false;
instructionsTxt.setText("Choose a chamber!");
// Reset timer for this turn
timer.startTimer(timePerTurn);
});
}
function onPullTrigger() {
if (!gameActive || spinningCylinder || !selectedChamber) {
return;
}
gameActive = false; // Temporarily disable game while animation plays
// Reveal the chamber
selectedChamber.setRevealed(true);
LK.setTimeout(function () {
if (selectedChamber.loaded) {
// Chamber was loaded - game over
LK.getSound('bang').play();
// Flash screen red
LK.effects.flashScreen(0xff0000, 1000);
showMessage("BANG! Game Over!");
// Update bullet indicator
updateBulletIndicator();
LK.setTimeout(function () {
endGame();
}, 2000);
} else {
// Chamber was empty - continue game
LK.getSound('click').play();
// Disable the selected chamber
selectedChamber.setSelected(false);
selectedChamber.alpha = 0.3;
selectedChamber.interactive = false;
// Update score
score += currentLevel * 10;
updateScoreDisplay();
// Update remaining empty chambers
remainingEmptyChambers--;
// Check if level completed
if (remainingEmptyChambers <= 0) {
LK.getSound('win').play();
showMessage("Level Complete!");
LK.setTimeout(function () {
startLevel(currentLevel + 1);
}, 2000);
} else {
// Reset for next turn
selectedChamber = null;
gameActive = true;
// Reset timer for this turn
timer.startTimer(timePerTurn);
// Update instructions
instructionsTxt.setText("Choose another chamber!");
}
}
}, 1000);
}
function updateBulletIndicator() {
// Find the first active indicator and deactivate it
for (var i = 0; i < bulletIndicators.length; i++) {
if (bulletIndicators[i].alpha === 1) {
bulletIndicators[i].setActive(false);
break;
}
}
}
function showMessage(text) {
messageTxt.setText(text);
messageTxt.alpha = 1;
// Fade out after 2 seconds
LK.setTimeout(function () {
tween(messageTxt, {
alpha: 0
}, {
duration: 500
});
}, 2000);
}
function updateScoreDisplay() {
scoreTxt.setText("Score: " + score);
// Update high score if needed
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText("High Score: " + highScore);
}
}
function endGame() {
// Game over logic
LK.showGameOver();
// Reset variables for next game
currentLevel = 1;
score = 0;
gameActive = false;
}
// Game update function
game.update = function () {
// This function is called every frame (60 times per second)
// Pulse effect for the pull trigger button
if (gameActive && selectedChamber) {
pullTriggerBtn.alpha = 0.7 + Math.sin(LK.ticks / 10) * 0.3;
pullTriggerBtn.setStyle({
fill: 0xff0000,
size: 100
});
} else {
pullTriggerBtn.alpha = 0.5;
pullTriggerBtn.setStyle({
fill: 0xD3D3D3,
size: 100
});
}
// Spin button visibility
if (gameActive && !spinningCylinder) {
spinBtn.alpha = 1;
} else {
spinBtn.alpha = 0.5;
}
};
// Mouse or touch events
game.down = function (x, y, obj) {
// Handle global down events
};
game.up = function (x, y, obj) {
// Handle global up events
};
// Initialize the game
setupGame(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
currentLevel: 1
});
/****
* Classes
****/
var BulletIndicator = Container.expand(function () {
var self = Container.call(this);
var bulletShape = self.attachAsset('bulletIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
self.setActive = function (active) {
bulletShape.alpha = active ? 1 : 0.3;
};
return self;
});
var Chamber = Container.expand(function () {
var self = Container.call(this);
// Chamber properties
self.loaded = false;
self.selected = false;
self.angle = 0;
// Chamber visuals
var chamberBg = self.attachAsset('chamber', {
anchorX: 0.5,
anchorY: 0.5
});
var chamberHole = self.attachAsset('chamberHole', {
anchorX: 0.5,
anchorY: 0.5
});
self.setLoaded = function (isLoaded) {
self.loaded = isLoaded;
// Only show the red bullet indicator if revealed
if (self.revealed && isLoaded) {
chamberHole.destroy();
chamberHole = self.attachAsset('chamberLoaded', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.setRevealed = function (isRevealed) {
self.revealed = isRevealed;
if (isRevealed && self.loaded) {
chamberHole.destroy();
chamberHole = self.attachAsset('chamberLoaded', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.setSelected = function (isSelected) {
self.selected = isSelected;
if (isSelected) {
tween(chamberBg, {
alpha: 0.7
}, {
duration: 200
});
LK.getSound('click').play(); // Play sound when chamber is selected
} else {
tween(chamberBg, {
alpha: 1
}, {
duration: 200
});
}
};
self.setAngle = function (newAngle) {
self.angle = newAngle;
};
self.down = function (x, y, obj) {
if (!gameActive || spinningCylinder) {
return;
}
self.setSelected(true);
selectedChamber = self;
};
return self;
});
var Gun = Container.expand(function () {
var self = Container.call(this);
// Gun body
var gunBody = self.attachAsset('gun', {
anchorX: 0.5,
anchorY: 0.5
});
// Gun handle
var gunHandle = self.attachAsset('gunHandle', {
anchorX: 0.5,
anchorY: 0,
y: 150
});
// Cylinder
var cylinder = self.attachAsset('cylinder', {
anchorX: 0.5,
anchorY: 0.5,
x: 50,
y: -50
});
// Create chambers
self.chambers = [];
self.setupLevel = function (level) {
// Clear existing chambers
for (var i = 0; i < self.chambers.length; i++) {
self.chambers[i].destroy();
}
self.chambers = [];
// Number of chambers based on level
var chamberCount = 6;
if (level > 5) {
chamberCount = 8;
}
if (level > 10) {
chamberCount = 10;
}
// Number of bullets based on level
var bulletCount = Math.min(Math.floor(level / 2) + 1, chamberCount - 1);
// Create chambers
for (var i = 0; i < chamberCount; i++) {
var chamber = new Chamber();
var angle = i / chamberCount * Math.PI * 2;
var radius = 180;
chamber.x = Math.cos(angle) * radius + 50;
chamber.y = Math.sin(angle) * radius - 50;
chamber.setAngle(angle);
self.chambers.push(chamber);
self.addChild(chamber);
}
// Randomly load chambers
var loadedIndexes = [];
while (loadedIndexes.length < bulletCount) {
var randomIndex = Math.floor(Math.random() * chamberCount);
if (loadedIndexes.indexOf(randomIndex) === -1) {
loadedIndexes.push(randomIndex);
}
}
for (var i = 0; i < loadedIndexes.length; i++) {
self.chambers[loadedIndexes[i]].setLoaded(true);
}
return {
chamberCount: chamberCount,
bulletCount: bulletCount
};
};
self.spinCylinder = function (callback) {
var spinDuration = 1500;
LK.getSound('spin').play();
// Add visual effect for spinning
tween(cylinder, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 200,
yoyo: true,
repeat: 3
});
// Spin animation
var rotations = 3 + Math.random() * 2;
var startRotation = cylinder.rotation;
var endRotation = startRotation + rotations * Math.PI * 2;
tween(cylinder, {
rotation: endRotation
}, {
duration: spinDuration,
easing: tween.easeOutQuad,
onFinish: function onFinish() {
if (callback) {
callback();
}
}
});
// Reorder the chambers randomly
var newOrder = [];
for (var i = 0; i < self.chambers.length; i++) {
newOrder.push(i);
}
// Fisher-Yates shuffle
for (var i = newOrder.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = newOrder[i];
newOrder[i] = newOrder[j];
newOrder[j] = temp;
}
// Store the chamber properties
var loadedStates = [];
for (var i = 0; i < self.chambers.length; i++) {
loadedStates.push({
loaded: self.chambers[i].loaded,
revealed: self.chambers[i].revealed
});
}
// Apply new order
for (var i = 0; i < self.chambers.length; i++) {
self.chambers[i].setLoaded(loadedStates[newOrder[i]].loaded);
self.chambers[i].setRevealed(loadedStates[newOrder[i]].revealed);
}
};
return self;
});
var LevelDisplay = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('levelIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
var levelText = new Text2("Level 1", {
size: 40,
fill: 0xFFFFFF
});
levelText.anchor.set(0.5, 0.5);
self.addChild(levelText);
self.updateLevel = function (level) {
levelText.setText("Level " + level);
};
return self;
});
var Timer = Container.expand(function () {
var self = Container.call(this);
var timerText = new Text2("", {
size: 50,
fill: 0xFFFFFF
});
timerText.anchor.set(0.5, 0.5);
self.addChild(timerText);
self.timeRemaining = 0;
self.active = false;
self.timerInterval = null;
self.startTimer = function (seconds) {
self.timeRemaining = seconds;
self.active = true;
self.updateDisplay();
if (self.timerInterval) {
LK.clearInterval(self.timerInterval);
}
self.timerInterval = LK.setInterval(function () {
if (self.active) {
self.timeRemaining -= 0.1;
if (self.timeRemaining <= 0) {
self.timeRemaining = 0;
self.active = false;
if (self.onTimeUp) {
self.onTimeUp();
}
}
self.updateDisplay();
}
}, 100);
};
self.stopTimer = function () {
self.active = false;
if (self.timerInterval) {
LK.clearInterval(self.timerInterval);
self.timerInterval = null;
}
};
self.updateDisplay = function () {
timerText.setText(Math.ceil(self.timeRemaining).toString());
// Change color based on time remaining
if (self.timeRemaining <= 5) {
timerText.setStyle({
fill: 0xFF3333 //{20} // Slightly lighter red for better visibility
});
} else if (self.timeRemaining <= 10) {
timerText.setStyle({
fill: 0xFFCC33 //{23} // Brighter orange for better visibility
});
} else {
timerText.setStyle({
fill: 0xDDDDDD //{27} // Softer white for better contrast
});
}
};
self.onTimeUp = null;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000022
});
/****
* Game Code
****/
// Game state variables
var currentLevel = storage.currentLevel || 1;
var score = 0;
var highScore = storage.highScore || 0;
var gameActive = false;
var spinningCylinder = false;
var selectedChamber = null;
var levelData = null;
var timePerTurn = 0;
var remainingEmptyChambers = 0;
// Game elements
var gun = new Gun();
var levelDisplay = new LevelDisplay();
var timer = new Timer();
var bulletIndicators = [];
// Score display
var scoreTxt = new Text2("Score: 0", {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
// High score display
var highScoreTxt = new Text2("High Score: " + highScore, {
size: 40,
fill: 0xFFAA00
});
highScoreTxt.anchor.set(0.5, 0);
// Game message text
var messageTxt = new Text2("", {
size: 70,
fill: 0xFFFFFF
});
messageTxt.anchor.set(0.5, 0.5);
// Instructions text
var instructionsTxt = new Text2("", {
size: 50,
fill: 0xFFFFFF
});
instructionsTxt.anchor.set(0.5, 0);
// "Pull Trigger" button
var pullTriggerBtn = new Text2("PULL TRIGGER", {
size: 80,
fill: 0xFFFFFF
});
pullTriggerBtn.anchor.set(0.5, 0.5);
pullTriggerBtn.interactive = true;
// "Spin" button
var spinBtn = new Text2("SPIN CYLINDER", {
size: 60,
fill: 0xFFFFFF
});
spinBtn.anchor.set(0.5, 0.5);
spinBtn.interactive = true;
function setupGame() {
// Add game elements
game.addChild(gun);
gun.x = 2048 / 2;
gun.y = 2732 / 2 - 200;
// Add level display
game.addChild(levelDisplay);
levelDisplay.x = 2048 / 2;
levelDisplay.y = 150;
// Add timer
game.addChild(timer);
timer.x = 2048 / 2;
timer.y = 250;
// Add score text
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 20;
// Add high score text
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 90;
// Add message text
game.addChild(messageTxt);
messageTxt.x = 2048 / 2;
messageTxt.y = 2732 / 2 - 600;
// Add instructions text
game.addChild(instructionsTxt);
instructionsTxt.x = 2048 / 2;
instructionsTxt.y = 2732 - 200;
// Add pull trigger button
game.addChild(pullTriggerBtn);
pullTriggerBtn.x = 2048 / 2;
pullTriggerBtn.y = 2732 - 350;
// Add spin button
game.addChild(spinBtn);
spinBtn.x = 2048 / 2;
spinBtn.y = 2732 - 450;
// Button events
pullTriggerBtn.down = onPullTrigger;
spinBtn.down = onSpinCylinder;
// Timer event
timer.onTimeUp = function () {
if (gameActive) {
showMessage("Time's up!");
LK.setTimeout(function () {
endGame();
}, 1500);
}
};
// Start level
startLevel(currentLevel);
// Play background music
LK.playMusic('tensionMusic', {
loop: true
});
}
function startLevel(level) {
// Reset state
gameActive = true;
spinningCylinder = false;
selectedChamber = null;
currentLevel = level;
// Update level display
levelDisplay.updateLevel(level);
// Set up gun with current level difficulty
levelData = gun.setupLevel(level);
// Calculate time per turn based on level
timePerTurn = Math.max(30 - level * 1.5, 10);
// Set remaining empty chambers
remainingEmptyChambers = levelData.chamberCount - levelData.bulletCount;
// Create bullet indicators
createBulletIndicators(levelData.bulletCount);
// Update score display
updateScoreDisplay();
// Update instructions
instructionsTxt.setText("Select a chamber or spin!");
instructionsTxt.setStyle({
size: 60
});
// Show level start message
showMessage("Level " + level + " - " + levelData.bulletCount + " bullet" + (levelData.bulletCount > 1 ? "s" : ""));
// Start timer
timer.startTimer(timePerTurn);
// Save current level
storage.currentLevel = currentLevel;
}
function createBulletIndicators(bulletCount) {
// Remove existing indicators
for (var i = 0; i < bulletIndicators.length; i++) {
bulletIndicators[i].destroy();
}
bulletIndicators = [];
// Create new indicators
var spacing = 50;
var totalWidth = (bulletCount - 1) * spacing;
var startX = 2048 / 2 - totalWidth / 2;
for (var i = 0; i < bulletCount; i++) {
var indicator = new BulletIndicator();
indicator.x = startX + i * spacing;
indicator.y = 350;
indicator.setActive(true);
game.addChild(indicator);
bulletIndicators.push(indicator);
}
}
function onSpinCylinder() {
if (!gameActive || spinningCylinder) {
return;
}
spinningCylinder = true;
instructionsTxt.setText("Spinning...");
// Reset selection
if (selectedChamber) {
selectedChamber.setSelected(false);
selectedChamber = null;
}
gun.spinCylinder(function () {
spinningCylinder = false;
instructionsTxt.setText("Choose a chamber!");
// Reset timer for this turn
timer.startTimer(timePerTurn);
});
}
function onPullTrigger() {
if (!gameActive || spinningCylinder || !selectedChamber) {
return;
}
gameActive = false; // Temporarily disable game while animation plays
// Reveal the chamber
selectedChamber.setRevealed(true);
LK.setTimeout(function () {
if (selectedChamber.loaded) {
// Chamber was loaded - game over
LK.getSound('bang').play();
// Flash screen red
LK.effects.flashScreen(0xff0000, 1000);
showMessage("BANG! Game Over!");
// Update bullet indicator
updateBulletIndicator();
LK.setTimeout(function () {
endGame();
}, 2000);
} else {
// Chamber was empty - continue game
LK.getSound('click').play();
// Disable the selected chamber
selectedChamber.setSelected(false);
selectedChamber.alpha = 0.3;
selectedChamber.interactive = false;
// Update score
score += currentLevel * 10;
updateScoreDisplay();
// Update remaining empty chambers
remainingEmptyChambers--;
// Check if level completed
if (remainingEmptyChambers <= 0) {
LK.getSound('win').play();
showMessage("Level Complete!");
LK.setTimeout(function () {
startLevel(currentLevel + 1);
}, 2000);
} else {
// Reset for next turn
selectedChamber = null;
gameActive = true;
// Reset timer for this turn
timer.startTimer(timePerTurn);
// Update instructions
instructionsTxt.setText("Choose another chamber!");
}
}
}, 1000);
}
function updateBulletIndicator() {
// Find the first active indicator and deactivate it
for (var i = 0; i < bulletIndicators.length; i++) {
if (bulletIndicators[i].alpha === 1) {
bulletIndicators[i].setActive(false);
break;
}
}
}
function showMessage(text) {
messageTxt.setText(text);
messageTxt.alpha = 1;
// Fade out after 2 seconds
LK.setTimeout(function () {
tween(messageTxt, {
alpha: 0
}, {
duration: 500
});
}, 2000);
}
function updateScoreDisplay() {
scoreTxt.setText("Score: " + score);
// Update high score if needed
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText("High Score: " + highScore);
}
}
function endGame() {
// Game over logic
LK.showGameOver();
// Reset variables for next game
currentLevel = 1;
score = 0;
gameActive = false;
}
// Game update function
game.update = function () {
// This function is called every frame (60 times per second)
// Pulse effect for the pull trigger button
if (gameActive && selectedChamber) {
pullTriggerBtn.alpha = 0.7 + Math.sin(LK.ticks / 10) * 0.3;
pullTriggerBtn.setStyle({
fill: 0xff0000,
size: 100
});
} else {
pullTriggerBtn.alpha = 0.5;
pullTriggerBtn.setStyle({
fill: 0xD3D3D3,
size: 100
});
}
// Spin button visibility
if (gameActive && !spinningCylinder) {
spinBtn.alpha = 1;
} else {
spinBtn.alpha = 0.5;
}
};
// Mouse or touch events
game.down = function (x, y, obj) {
// Handle global down events
};
game.up = function (x, y, obj) {
// Handle global up events
};
// Initialize the game
setupGame();