User prompt
Make sure numbers are alwatlys displayed after counter finishes in new level
Code edit (1 edits merged)
Please save this source code
User prompt
if correct guesse equals correct combinations, update countdown to the number depending the rule of the level they are int
Code edit (1 edits merged)
Please save this source code
User prompt
Do not diasplay original countdown number, only the updated one
Code edit (1 edits merged)
Please save this source code
User prompt
when a new level starts, hide countdown for one second
User prompt
only show coundown from intializehextiles
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught ReferenceError: countdown is not defined' in or related to this line: 'var countdownText = new Text2(countdown, {' Line Number: 240
Code edit (1 edits merged)
Please save this source code
User prompt
Countdown should be 10 seconds
Code edit (4 edits merged)
Please save this source code
User prompt
move lifes and level 50 pixels to the left
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
when a hextile is toggled, make it not clickable, until it is toggled again
User prompt
add a new validation to not allow the player to toggle a hextile back after it touches it for the first time. it will only be toggled back after the second hextile is touched
User prompt
add soft blue as random backround color
User prompt
on game start use startscreenbackground as background image over the current background color. however hide starscrennbackgournd after first touch
User prompt
show startscreenbackground insted of background color, unntil the first touch is done in the screen
User prompt
If hextile is selected, it cant be unselected until another hextile is selected
Code edit (3 edits merged)
Please save this source code
User prompt
on hextile class, if there has not been any touch in the screen set numbertext.visible = false, if not set it to true.
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
// HexTile class for the hexagon tiles
var HexTile = Container.expand(function (id, colorIndex) {
var self = Container.call(this);
self.id = id;
var hexGraphics = self.attachAsset('hexTile', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
var numberText = new Text2('', {
size: 100,
fill: '#333333' // Slightly lighter black
});
numberText.anchor.set(0.5, 0.5);
// Initially set numberText visibility to false
numberText.visible = false;
// Fade in effect for numberText
var fadeInDuration = 60; // Duration for fade in in ticks (1 second at 60FPS)
var fadeInCounter = 0; // Counter to track the number of ticks for fading
LK.on('tick', function () {
if (!numberText.visible && playerTouchedScreen) {
fadeInCounter++;
var progress = fadeInCounter / fadeInDuration;
numberText.alpha = progress;
if (fadeInCounter >= fadeInDuration) {
numberText.visible = true; // Ensure the text is visible after fading
numberText.alpha = 1; // Reset alpha after complete fade in
}
}
});
self.addChild(numberText);
self.setNumber = function (number) {
self.number = number;
numberText.setText(number.toString());
};
self.isSelected = false;
self.toggleSelect = function () {
self.isSelected = !self.isSelected;
self.interactive = !self.isSelected; // Toggle interactive state
hexGraphics.alpha = self.isSelected ? 0.5 : 1;
if (self.isSelected) {
numberText.setText(self.number.toString());
} else {
numberText.setText('');
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: [0x8AACB8, 0x61AC84, 0xcc919a, 0xFFA07A, 0xB39BDB, 0x73be73][Math.floor(Math.random() * 6)] // Randomly choose from soft light blue, soft light orange, soft pink, soft light red, soft light yellow, soft light green
});
/****
* Game Code
****/
var scoreText = new Text2('Score 0', {
size: 75,
fill: '#ffffff'
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.visible = false;
var missedAttemptMessages = ["Ooops!", "Missed!", "Not Quite!", "Close One!"];
var gameTitle = new Text2('Memo-Hex', {
size: 200,
fill: '#ffffff',
alpha: 1
});
gameTitle.anchor.set(0.5, 0);
gameTitle.x = 0; // Center horizontally
gameTitle.y = 100; // Move down 100 pixels
LK.gui.top.addChild(gameTitle);
// Blinking 'Touch to start' text
var touchToStartText = new Text2('Touch to start', {
size: 100,
fill: '#ffffff',
alpha: 1
});
touchToStartText.anchor.set(0.5, 0);
touchToStartText.x = 0; // Center horizontally
touchToStartText.y = gameTitle.y + gameTitle.height + 80; // Position below the game title
LK.gui.top.addChild(touchToStartText);
// Blinking effect
var blink = true;
LK.setInterval(function () {
blink = !blink;
touchToStartText.alpha = blink ? 1 : 0;
}, 500);
var howToPlayText = new Text2('How to Play: \n⢠Memorize the hex numbers on countdown \n⢠Wait for goal number to appear above hex tiles \n⢠Touch two hexes that add up to the number \n⢠Repeat until all combinations possible are found', {
size: 60,
fill: '#ffffff',
alpha: 1
});
howToPlayText.anchor.set(0.5, 0.5);
howToPlayText.x = 0;
howToPlayText.y = 1500;
LK.gui.top.addChild(howToPlayText);
// Modify game's 'down' event listener to hide howToPlayText after first touch
var originalGameDownHandler = game._events && game._events['down'] ? game._events['down'][0] : function () {};
function newGameDownHandler(obj) {
// Start fading out the 'How to Play' text
var howToPlayFadeOut = true;
var howToPlayFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
var howToPlayFadeCounter = 0; // Counter to track the number of ticks for fading
LK.on('tick', function () {
if (howToPlayFadeOut) {
howToPlayFadeCounter++;
var progress = howToPlayFadeCounter / howToPlayFadeDuration;
howToPlayText.alpha = 1 - progress;
if (howToPlayFadeCounter >= howToPlayFadeDuration) {
howToPlayFadeOut = false; // Stop fading
howToPlayText.visible = false; // Ensure the text is invisible after fading
}
}
});
originalGameDownHandler(obj);
// Make level text visible on first touch
levelText.alpha = 1;
levelText.visible = true;
scoreText.visible = true;
LK.setTimeout(function () {
countdownText.visible = true;
}, 1500);
touchToStartText.visible = false;
// Start fading out the game title
var gameTitleFadeOut = true;
var gameTitleFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
var gameTitleFadeCounter = 0; // Counter to track the number of ticks for fading
// Update the game tick function to handle game title fading
LK.on('tick', function () {
if (gameTitleFadeOut) {
gameTitleFadeCounter++;
var progress = gameTitleFadeCounter / gameTitleFadeDuration;
gameTitle.alpha = 1 - progress;
if (gameTitleFadeCounter >= gameTitleFadeDuration) {
gameTitleFadeOut = false; // Stop fading
gameTitle.visible = false; // Ensure the title is invisible after fading
}
}
});
// Make hexTile numbers visible on first touch
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].getChildAt(1).visible = true;
}
}
game.off('down', originalGameDownHandler);
game.on('down', newGameDownHandler);
var firstTouch = true;
game.on('down', function (obj) {
playerTouchedScreen = true;
if (countdown > 1 && !firstTouch) {
showRandomMotivationalText();
}
firstTouch = false;
// Make life icons visible on first touch
// Update life icons visibility based on current lives
for (var i = 0; i < lifeIcons.length; i++) {
lifeIcons[i].alpha = i < lives ? 1 : 0;
}
});
var playerTouchedScreen = false;
var motivationalTexts = ["Memorize!", "Wait for countdown!", "Wait for goal number!", "Stay focused!"];
var motivationalText = new Text2('', {
size: 100,
fill: '#ffffff',
alpha: 0 // Initially invisible
});
motivationalText.anchor.set(0.5, 0);
motivationalText.x = 0; // Center horizontally
motivationalText.y = 400; // Position at the top of the screen
LK.gui.top.addChild(motivationalText);
var fadeState = 'in'; // Track the fade state of the motivational text
var fadeDuration = 60; // Duration for fade in and fade out in ticks (1 second at 60FPS)
var fadeCounter = 0; // Counter to track the number of ticks for fading
function showRandomMotivationalText() {
var randomIndex = Math.floor(Math.random() * motivationalTexts.length);
motivationalText.setText(motivationalTexts[randomIndex]);
fadeState = 'in';
fadeCounter = 0; // Reset fade counter
}
// Update the game tick function to handle motivational text fading
LK.on('tick', function () {
if (fadeState !== 'none') {
fadeCounter++;
var progress = fadeCounter / fadeDuration;
if (fadeState === 'in') {
motivationalText.alpha = progress;
if (fadeCounter >= fadeDuration) {
fadeState = 'out'; // Switch to fading out
fadeCounter = 0; // Reset counter for fade out
}
} else if (fadeState === 'out') {
motivationalText.alpha = 1 - progress;
if (fadeCounter >= fadeDuration) {
fadeState = 'none'; // Stop fading
}
}
}
});
function resetGameForNextLevel() {
// Reset hexTiles numbers
for (var i = 0; i < hexTiles.length; i++) {
// Number generation and assignment removed from here to be handled inside initializeHexTiles function.
}
// Reset goal
var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
while (randomTile1 === randomTile2) {
randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
}
goalNumber = randomTile1.number + randomTile2.number;
goalText.setText(goalNumber);
// Reset combinations
correctCombinations = 0;
for (var i = 0; i < hexTiles.length; i++) {
for (var j = i + 1; j < hexTiles.length; j++) {
if (hexTiles[i].number + hexTiles[j].number === goalNumber) {
correctCombinations++;
}
}
}
combinationsText.setText(correctGuess + '/' + correctCombinations);
}
var level = 1;
var levelText = new Text2('Level ' + level, {
size: 75,
fill: '#ffffff',
alpha: 0 // Make level text invisible initially
});
levelText.anchor.set(1, 0);
levelText.x = -50; // Move level text 50 pixels to the left
// Hide level text on game start
levelText.visible = false;
LK.gui.topRight.addChild(levelText);
correctGuess = 0;
var correctGuess = 0;
var lives = 3;
var lifeIcons = [];
for (var i = 0; i < lives; i++) {
var lifeIcon = LK.getAsset('lifeIcon', {
anchorX: 1,
anchorY: 0,
x: -i * 90 - 50,
// Move life icons 50 pixels to the left
y: 100,
alpha: 0 // Make life icons invisible initially
});
LK.gui.topRight.addChild(lifeIcon);
lifeIcons.push(lifeIcon);
}
var countdown = 10; // Start from 10 and make it visible for one second before countdown starts
if (hexTiles) {
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].interactive = false;
}
}
var countdownText = new Text2(countdown, {
size: 200,
fill: '#ffffff',
alpha: 0 // Make invisible initially
});
countdownText.visible = false;
countdownText.anchor.set(0.5, 1);
LK.gui.top.addChild(countdownText);
var countdownInterval = LK.setInterval(function () {
if (playerTouchedScreen) {
countdown--;
if (countdown <= 0) {
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].interactive = true;
}
countdownText.alpha = 0;
goalText.visible = true;
goalTextFadeIn = true; // Start fading in the goal text
goalTextFadeCounter = 0; // Reset fade counter
combinationsText.visible = true;
combinationsTextFadeIn = true; // Start fading in the combinations text
combinationsTextFadeCounter = 0; // Reset fade counter
LK.clearInterval(countdownInterval);
// This block is removed to ensure numbers remain visible after countdown.
} else {
countdownText.setText(countdown);
}
}
}, 1000);
countdownText.y += 1600;
//var scoreText = new Text2('Score: 0', {
// size: 75,
// fill: '#ffffff'
//});
//scoreText.anchor.set(0.5, 0);
//LK.gui.top.addChild(scoreText);
// Create goal number and display it below the score
if (hexTiles && hexTiles.length > 1) {
sumOfNeighbours = hexTiles[hexTiles.length - 2].number + hexTiles[hexTiles.length - 1].number;
}
// sumText initialization moved to after hexTiles initialization
var sumOfNeighbours = 0;
var hexTiles = [];
var selectedTiles = [];
var guessedPairs = {};
var gridRows = [];
function initializeHexTiles() {
function initializeHexTiles() {
// Define gridRows based on the current game level
var gridRows;
if (level <= 5) {
gridRows = [2, 3, 2];
countdown = 11;
} else if (level <= 10) {
gridRows = [1, 2, 3, 4];
countdown = 16;
} else if (level <= 15) {
gridRows = [2, 3, 4, 3, 2];
} else {
// Default to the highest defined grid for levels beyond those explicitly defined
gridRows = [3, 4, 5, 4, 3];
}
var tileSpacingX = 250;
var tileSpacingY = 210;
var startX = (2048 - Math.max.apply(Math, gridRows) * tileSpacingX) / 2 + tileSpacingX / 2;
var startY = (2732 - gridRows.length * tileSpacingY) / 2 + tileSpacingY / 2;
// Initialize hex tiles
for (var i = 0; i < gridRows.length; i++) {
for (var j = 0; j < gridRows[i]; j++) {
var hexTile = new HexTile(i * gridRows.length + j);
var numberRange;
if (level <= 2) {
numberRange = 3;
} else if (level <= 5) {
numberRange = 7;
} else if (level > 5 && level <= 7) {
numberRange = 5;
} else if (level <= 12) {
numberRange = 7;
} else if (level <= 15) {
numberRange = 7;
} else {
numberRange = 9;
}
hexTile.setNumber(Math.floor(Math.random() * numberRange) + 1);
hexTile.x = startX + j * tileSpacingX + (Math.max.apply(Math, gridRows) - gridRows[i]) * tileSpacingX / 2;
hexTile.y = startY + i * tileSpacingY;
game.addChild(hexTile);
hexTiles.push(hexTile);
(function (hexTile) {
hexTile.on('down', function (obj) {
if (countdown > 0) {
return;
}
if (selectedTiles.length < 2) {
hexTile.toggleSelect();
if (hexTile.isSelected) {
selectedTiles.push(hexTile);
if (selectedTiles.length == 2) {
if (selectedTiles[0].number + selectedTiles[1].number === goalNumber) {
var pairId = [selectedTiles[0].id, selectedTiles[1].id].sort().join('-');
if (!guessedPairs[pairId]) {
guessedPairs[pairId] = true;
correctGuess++;
LK.setScore(LK.getScore() + 10 * level);
combinationsText.setText(correctGuess + '/' + correctCombinations);
// Show 'Yes!' or 'Right!' text on correct combination
var correctCombinationMessages = ["Yes!", "Right!", "Correct!", "Nice!"];
var randomMessageIndex = Math.floor(Math.random() * correctCombinationMessages.length);
var correctCombinationMessage = new Text2(correctCombinationMessages[randomMessageIndex], {
size: 100,
fill: '#ffffff',
alpha: 1 // Initially visible
});
correctCombinationMessage.anchor.set(0.5, 0);
correctCombinationMessage.x = 0; // Center horizontally
correctCombinationMessage.y = 1650; // Position at specified height
LK.gui.top.addChild(correctCombinationMessage);
// Directly start fading out the correct combination message without fade in effect
var correctCombinationMessageFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
var correctCombinationMessageFadeCounter = correctCombinationMessageFadeDuration; // Start counter at full duration for immediate fade out
LK.on('tick', function () {
correctCombinationMessageFadeCounter--;
scoreText.setText('Score ' + LK.getScore());
var progress = correctCombinationMessageFadeCounter / correctCombinationMessageFadeDuration;
correctCombinationMessage.alpha = progress;
if (correctCombinationMessageFadeCounter <= 0) {
LK.gui.top.removeChild(correctCombinationMessage); // Remove the message after fading out
}
});
} else {
// Show 'Tried already!' text for a previously guessed pair
var triedAlreadyMessage = new Text2("Tried already!", {
size: 100,
fill: '#ffffff',
alpha: 0 // Initially invisible
});
triedAlreadyMessage.anchor.set(0.5, 0);
triedAlreadyMessage.x = 0; // Center horizontally
triedAlreadyMessage.y = 1650; // Position at specified height
LK.gui.top.addChild(triedAlreadyMessage);
// Directly start fading out 'Tried already!' message without fade in effect
var triedAlreadyMessageFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
var triedAlreadyMessageFadeCounter = triedAlreadyMessageFadeDuration; // Start counter at full duration for immediate fade out
LK.on('tick', function () {
triedAlreadyMessageFadeCounter--;
var progress = triedAlreadyMessageFadeCounter / triedAlreadyMessageFadeDuration;
triedAlreadyMessage.alpha = progress;
if (triedAlreadyMessageFadeCounter <= 0) {
LK.gui.top.removeChild(triedAlreadyMessage); // Remove the message after fading out
}
});
}
LK.setTimeout(function () {
if (selectedTiles.length > 0) {
selectedTiles[0].toggleSelect();
}
if (selectedTiles.length > 1) {
selectedTiles[1].toggleSelect();
}
selectedTiles = [];
}, 300);
if (correctGuess === correctCombinations) {
// Disable interaction with hex tiles immediately after all combinations are found
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].interactive = false;
}
LK.setTimeout(function () {
// Temporarily bring the background to the top of the z axis
game.addChildAt(game.background, game.children.length);
LK.setTimeout(function () {
// Move the background back to its original position after 2 seconds
game.addChildAt(game.background, 0);
}, 2000);
level++;
lives = 3; // Reset lives back to initial value
// Delete existing life icons
for (var i = 0; i < lifeIcons.length; i++) {
lifeIcons[i].destroy();
}
lifeIcons = []; // Clear the lifeIcons array
// Recreate life icons
for (var i = 0; i < lives; i++) {
var lifeIcon = LK.getAsset('lifeIcon', {
anchorX: 1,
anchorY: 0,
x: -i * 90 - 50,
// Move life icons 50 pixels to the left
y: 100,
alpha: 1 // Make life icons visible
});
LK.gui.topRight.addChild(lifeIcon);
lifeIcons.push(lifeIcon);
}
// Update score text to reflect current score dynamically
LK.setScore(LK.getScore() + 10 * level);
correctGuess = 0;
guessedPairs = {};
combinationsText.setText(correctGuess + '/' + correctCombinations);
levelText.setText('Level ' + level);
countdown = 10;
goalText.visible = false;
combinationsText.visible = false;
countdownText.alpha = 1;
countdownText.setText(countdown);
LK.clearInterval(countdownInterval);
countdownInterval = LK.setInterval(function () {
countdown--;
if (countdown <= 0) {
countdownText.alpha = 0;
goalText.visible = true;
combinationsText.visible = true;
LK.clearInterval(countdownInterval);
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].getChildAt(1).setText('');
}
} else {
countdownText.setText(countdown);
}
}, 1000);
LK.effects.flashScreen(0x90EE90, 500);
// Show success message
var successMessages = ["Great Job!", "Level Cleared!", "Awesome!", "Well Done!", "Fantastic!"];
var randomSuccessMessageIndex = Math.floor(Math.random() * successMessages.length);
var successMessage = new Text2(successMessages[randomSuccessMessageIndex], {
size: 150,
// Adjust size to match motivational text
fill: '#ffffff',
alpha: 1
});
successMessage.anchor.set(0.5, 0); // Adjust anchor to match motivational text
successMessage.x = 0; // Center horizontally
successMessage.y = 400; // Position at the same height as motivational text
LK.gui.top.addChild(successMessage);
// Initialize variables for success message fade out
var successMessageFadeOut = true;
var successMessageFadeDuration = 120; // Duration for fade out in ticks (2 seconds at 60FPS), starts 1 second after appearing
var successMessageFadeCounter = 0; // Counter to track the number of ticks for fading
LK.on('tick', function () {
if (successMessageFadeOut) {
successMessageFadeCounter++;
var progress = successMessageFadeCounter / successMessageFadeDuration;
successMessage.alpha = 1 - progress;
if (successMessageFadeCounter >= successMessageFadeDuration) {
successMessageFadeOut = false; // Stop fading
LK.gui.top.removeChild(successMessage); // Ensure the message is removed after fading
}
}
});
// Remove previous hex tiles
for (var i = 0; i < hexTiles.length; i++) {
hexTiles[i].destroy();
}
hexTiles = [];
initializeHexTiles();
// Update goal and guesses with new numbers on the new grid
// Ensure a different random background color is chosen when the level changes, different from the previous one.
var previousBackgroundColor = game.backgroundColor;
var newColors = [0x8AACB8, 0x61AC84, 0xcc919a, 0xFFA07A, 0xB39BDB, 0x73be73].filter(function (color) {
return color !== previousBackgroundColor;
});
game.setBackgroundColor(newColors[Math.floor(Math.random() * newColors.length)]);
var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
while (randomTile1 === randomTile2) {
randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
}
goalNumber = randomTile1.number + randomTile2.number;
goalText.setText(goalNumber);
// Reset combinations
correctCombinations = 0;
for (var i = 0; i < hexTiles.length; i++) {
for (var j = i + 1; j < hexTiles.length; j++) {
if (hexTiles[i].number + hexTiles[j].number === goalNumber) {
correctCombinations++;
}
}
}
combinationsText.setText(correctGuess + '/' + correctCombinations);
}, 1000); // Wait one second before executing level transition
} // End of level transition delay
} else {
lives--;
lifeIcons[lives].destroy();
if (lives <= 0) {
LK.showGameOver("Your Score: " + LK.getScore());
} else {
LK.effects.flashScreen(0xFFA07A, 500);
// Select a random missed attempt message
var missedMessageIndex = Math.floor(Math.random() * missedAttemptMessages.length);
var missedMessage = new Text2(missedAttemptMessages[missedMessageIndex], {
size: 100,
fill: '#ffffff',
alpha: 0 // Initially invisible
});
missedMessage.anchor.set(0.5, 0);
missedMessage.x = 0; // Center horizontally
missedMessage.y = 1650; // Position at the top of the screen
LK.gui.top.addChild(missedMessage);
// Fade in the missed attempt message
var missedMessageFadeIn = true;
var missedMessageFadeDuration = 120; // Duration for fade in and out in ticks (1 second at 60FPS)
var missedMessageFadeCounter = 0; // Counter to track the number of ticks for fading
LK.on('tick', function () {
if (missedMessageFadeIn) {
missedMessageFadeCounter++;
var progress = missedMessageFadeCounter / missedMessageFadeDuration;
missedMessage.alpha = progress;
if (missedMessageFadeCounter >= missedMessageFadeDuration / 2) {
missedMessageFadeIn = false; // Start fading out after reaching half duration
}
} else {
missedMessageFadeCounter--;
var progress = missedMessageFadeCounter / missedMessageFadeDuration;
missedMessage.alpha = progress;
if (missedMessageFadeCounter <= 0) {
LK.gui.top.removeChild(missedMessage); // Remove the message after fading out
}
}
});
}
LK.setTimeout(function () {
if (selectedTiles.length > 0) {
selectedTiles[0].toggleSelect();
}
if (selectedTiles.length > 1) {
selectedTiles[1].toggleSelect();
}
selectedTiles = [];
}, 300);
}
}
} else {
var index = selectedTiles.indexOf(hexTile);
if (index > -1) {
selectedTiles.splice(index, 1);
}
}
}
});
})(hexTile);
}
}
}
initializeHexTiles();
}
initializeHexTiles();
var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
while (randomTile1 === randomTile2) {
randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
}
var goalNumber = randomTile1.number + randomTile2.number;
var goalText = new Text2(goalNumber, {
size: 300,
fill: '#ffffff',
alpha: 0 // Make invisible initially
});
// Initialize goal text fade-in variables
var goalTextFadeIn = false;
var goalTextFadeDuration = 60; // Duration for fade in in ticks (1 second at 60FPS)
var goalTextFadeCounter = 0; // Counter to track the number of ticks for fading
goalText.visible = false; // Ensure goal text is initially invisible
goalText.anchor.set(0.5, 1);
goalText.y += 550;
LK.gui.top.addChild(goalText);
// Calculate the number of unique correct combinations
var correctCombinations = 0;
for (var i = 0; i < hexTiles.length; i++) {
for (var j = i + 1; j < hexTiles.length; j++) {
if (hexTiles[i].number + hexTiles[j].number === goalNumber) {
correctCombinations++;
}
}
}
// Display the number of unique correct combinations below the goal
var combinationsText = new Text2(correctGuess + '/' + correctCombinations, {
size: 100,
fill: '#ffffff',
alpha: 1 // Make visible
});
// Initialize combinations text fade-in variables
var combinationsTextFadeIn = false;
var combinationsTextFadeDuration = 60; // Duration for fade in in ticks (1 second at 60FPS)
var combinationsTextFadeCounter = 0; // Counter to track the number of ticks for fading
combinationsText.visible = false; // Ensure combinations text is initially invisible
combinationsText.anchor.set(0.5, 0);
combinationsText.y += 1400;
LK.gui.top.addChild(combinationsText);
// Game tick function
LK.on('tick', function () {
// Handle goal text fade in effect
if (goalTextFadeIn) {
goalTextFadeCounter++;
var progress = goalTextFadeCounter / goalTextFadeDuration;
goalText.alpha = progress;
if (goalTextFadeCounter >= goalTextFadeDuration) {
goalTextFadeIn = false; // Stop fading
}
}
// Handle combinations text fade in effect
if (combinationsTextFadeIn) {
combinationsTextFadeCounter++;
var progress = combinationsTextFadeCounter / combinationsTextFadeDuration;
combinationsText.alpha = progress;
if (combinationsTextFadeCounter >= combinationsTextFadeDuration) {
combinationsTextFadeIn = false; // Stop fading
}
}
// Game logic that needs to be executed every frame
}); ===================================================================
--- original.js
+++ change.js
@@ -15,9 +15,24 @@
size: 100,
fill: '#333333' // Slightly lighter black
});
numberText.anchor.set(0.5, 0.5);
- numberText.visible = playerTouchedScreen;
+ // Initially set numberText visibility to false
+ numberText.visible = false;
+ // Fade in effect for numberText
+ var fadeInDuration = 60; // Duration for fade in in ticks (1 second at 60FPS)
+ var fadeInCounter = 0; // Counter to track the number of ticks for fading
+ LK.on('tick', function () {
+ if (!numberText.visible && playerTouchedScreen) {
+ fadeInCounter++;
+ var progress = fadeInCounter / fadeInDuration;
+ numberText.alpha = progress;
+ if (fadeInCounter >= fadeInDuration) {
+ numberText.visible = true; // Ensure the text is visible after fading
+ numberText.alpha = 1; // Reset alpha after complete fade in
+ }
+ }
+ });
self.addChild(numberText);
self.setNumber = function (number) {
self.number = number;
numberText.setText(number.toString());
@@ -44,8 +59,16 @@
/****
* Game Code
****/
+var scoreText = new Text2('Score 0', {
+ size: 75,
+ fill: '#ffffff'
+});
+scoreText.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreText);
+scoreText.visible = false;
+var missedAttemptMessages = ["Ooops!", "Missed!", "Not Quite!", "Close One!"];
var gameTitle = new Text2('Memo-Hex', {
size: 200,
fill: '#ffffff',
alpha: 1
@@ -100,11 +123,12 @@
originalGameDownHandler(obj);
// Make level text visible on first touch
levelText.alpha = 1;
levelText.visible = true;
+ scoreText.visible = true;
LK.setTimeout(function () {
countdownText.visible = true;
- }, 1000);
+ }, 1500);
touchToStartText.visible = false;
// Start fading out the game title
var gameTitleFadeOut = true;
var gameTitleFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
@@ -135,16 +159,17 @@
showRandomMotivationalText();
}
firstTouch = false;
// Make life icons visible on first touch
+ // Update life icons visibility based on current lives
for (var i = 0; i < lifeIcons.length; i++) {
- lifeIcons[i].alpha = 1;
+ lifeIcons[i].alpha = i < lives ? 1 : 0;
}
});
var playerTouchedScreen = false;
var motivationalTexts = ["Memorize!", "Wait for countdown!", "Wait for goal number!", "Stay focused!"];
var motivationalText = new Text2('', {
- size: 75,
+ size: 100,
fill: '#ffffff',
alpha: 0 // Initially invisible
});
motivationalText.anchor.set(0.5, 0);
@@ -229,10 +254,15 @@
});
LK.gui.topRight.addChild(lifeIcon);
lifeIcons.push(lifeIcon);
}
-var countdown = 10; // Initialize countdown with a starting value of 10 seconds
-var countdownText = new Text2(countdown.toString(), {
+var countdown = 10; // Start from 10 and make it visible for one second before countdown starts
+if (hexTiles) {
+ for (var i = 0; i < hexTiles.length; i++) {
+ hexTiles[i].interactive = false;
+ }
+}
+var countdownText = new Text2(countdown, {
size: 200,
fill: '#ffffff',
alpha: 0 // Make invisible initially
});
@@ -253,11 +283,9 @@
combinationsText.visible = true;
combinationsTextFadeIn = true; // Start fading in the combinations text
combinationsTextFadeCounter = 0; // Reset fade counter
LK.clearInterval(countdownInterval);
- for (var i = 0; i < hexTiles.length; i++) {
- hexTiles[i].getChildAt(1).setText('');
- }
+ // This block is removed to ensure numbers remain visible after countdown.
} else {
countdownText.setText(countdown);
}
}
@@ -282,16 +310,16 @@
function initializeHexTiles() {
function initializeHexTiles() {
// Define gridRows based on the current game level
var gridRows;
- if (level <= 1) {
+ if (level <= 5) {
gridRows = [2, 3, 2];
countdown = 11;
- } else if (level <= 2) {
- gridRows = [3, 4, 3];
- countdown = 15;
+ } else if (level <= 10) {
+ gridRows = [1, 2, 3, 4];
+ countdown = 16;
} else if (level <= 15) {
- gridRows = [4, 5, 4];
+ gridRows = [2, 3, 4, 3, 2];
} else {
// Default to the highest defined grid for levels beyond those explicitly defined
gridRows = [3, 4, 5, 4, 3];
}
@@ -307,12 +335,16 @@
if (level <= 2) {
numberRange = 3;
} else if (level <= 5) {
numberRange = 7;
- } else if (level <= 10) {
- numberRange = 9;
+ } else if (level > 5 && level <= 7) {
+ numberRange = 5;
+ } else if (level <= 12) {
+ numberRange = 7;
+ } else if (level <= 15) {
+ numberRange = 7;
} else {
- numberRange = 12;
+ numberRange = 9;
}
hexTile.setNumber(Math.floor(Math.random() * numberRange) + 1);
hexTile.x = startX + j * tileSpacingX + (Math.max.apply(Math, gridRows) - gridRows[i]) * tileSpacingX / 2;
hexTile.y = startY + i * tileSpacingY;
@@ -332,9 +364,56 @@
var pairId = [selectedTiles[0].id, selectedTiles[1].id].sort().join('-');
if (!guessedPairs[pairId]) {
guessedPairs[pairId] = true;
correctGuess++;
+ LK.setScore(LK.getScore() + 10 * level);
combinationsText.setText(correctGuess + '/' + correctCombinations);
+ // Show 'Yes!' or 'Right!' text on correct combination
+ var correctCombinationMessages = ["Yes!", "Right!", "Correct!", "Nice!"];
+ var randomMessageIndex = Math.floor(Math.random() * correctCombinationMessages.length);
+ var correctCombinationMessage = new Text2(correctCombinationMessages[randomMessageIndex], {
+ size: 100,
+ fill: '#ffffff',
+ alpha: 1 // Initially visible
+ });
+ correctCombinationMessage.anchor.set(0.5, 0);
+ correctCombinationMessage.x = 0; // Center horizontally
+ correctCombinationMessage.y = 1650; // Position at specified height
+ LK.gui.top.addChild(correctCombinationMessage);
+ // Directly start fading out the correct combination message without fade in effect
+ var correctCombinationMessageFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
+ var correctCombinationMessageFadeCounter = correctCombinationMessageFadeDuration; // Start counter at full duration for immediate fade out
+ LK.on('tick', function () {
+ correctCombinationMessageFadeCounter--;
+ scoreText.setText('Score ' + LK.getScore());
+ var progress = correctCombinationMessageFadeCounter / correctCombinationMessageFadeDuration;
+ correctCombinationMessage.alpha = progress;
+ if (correctCombinationMessageFadeCounter <= 0) {
+ LK.gui.top.removeChild(correctCombinationMessage); // Remove the message after fading out
+ }
+ });
+ } else {
+ // Show 'Tried already!' text for a previously guessed pair
+ var triedAlreadyMessage = new Text2("Tried already!", {
+ size: 100,
+ fill: '#ffffff',
+ alpha: 0 // Initially invisible
+ });
+ triedAlreadyMessage.anchor.set(0.5, 0);
+ triedAlreadyMessage.x = 0; // Center horizontally
+ triedAlreadyMessage.y = 1650; // Position at specified height
+ LK.gui.top.addChild(triedAlreadyMessage);
+ // Directly start fading out 'Tried already!' message without fade in effect
+ var triedAlreadyMessageFadeDuration = 60; // Duration for fade out in ticks (1 second at 60FPS)
+ var triedAlreadyMessageFadeCounter = triedAlreadyMessageFadeDuration; // Start counter at full duration for immediate fade out
+ LK.on('tick', function () {
+ triedAlreadyMessageFadeCounter--;
+ var progress = triedAlreadyMessageFadeCounter / triedAlreadyMessageFadeDuration;
+ triedAlreadyMessage.alpha = progress;
+ if (triedAlreadyMessageFadeCounter <= 0) {
+ LK.gui.top.removeChild(triedAlreadyMessage); // Remove the message after fading out
+ }
+ });
}
LK.setTimeout(function () {
if (selectedTiles.length > 0) {
selectedTiles[0].toggleSelect();
@@ -344,110 +423,165 @@
}
selectedTiles = [];
}, 300);
if (correctGuess === correctCombinations) {
- level++;
- LK.setScore(LK.getScore() + 1);
- correctGuess = 0;
- guessedPairs = {};
- combinationsText.setText(correctGuess + '/' + correctCombinations);
- levelText.setText('Level ' + level);
- // Update countdown based on level
- if (level <= 2) {
- countdown = 16;
- } else if (level <= 5) {
- countdown = 21;
- } else if (level <= 10) {
- countdown = 26;
- } else {
- countdown = 31;
+ // Disable interaction with hex tiles immediately after all combinations are found
+ for (var i = 0; i < hexTiles.length; i++) {
+ hexTiles[i].interactive = false;
}
- goalText.visible = false;
- combinationsText.visible = false;
- countdownText.alpha = 1;
- countdownText.setText(countdown.toString());
- LK.clearInterval(countdownInterval);
- countdownInterval = LK.setInterval(function () {
- countdown--;
- if (countdown <= 0) {
- countdownText.alpha = 0;
- goalText.visible = true;
- combinationsText.visible = true;
- LK.clearInterval(countdownInterval);
- for (var i = 0; i < hexTiles.length; i++) {
- hexTiles[i].getChildAt(1).setText('');
- }
- } else {
- countdownText.setText(countdown.toString());
+ LK.setTimeout(function () {
+ // Temporarily bring the background to the top of the z axis
+ game.addChildAt(game.background, game.children.length);
+ LK.setTimeout(function () {
+ // Move the background back to its original position after 2 seconds
+ game.addChildAt(game.background, 0);
+ }, 2000);
+ level++;
+ lives = 3; // Reset lives back to initial value
+ // Delete existing life icons
+ for (var i = 0; i < lifeIcons.length; i++) {
+ lifeIcons[i].destroy();
}
- }, 1000);
- LK.effects.flashScreen(0x90EE90, 500);
- // Show success message
- var successMessages = ["Great Job!", "Level Cleared!", "Awesome!", "Well Done!", "Fantastic!"];
- var randomSuccessMessageIndex = Math.floor(Math.random() * successMessages.length);
- var successMessage = new Text2(successMessages[randomSuccessMessageIndex], {
- size: 75,
- // Adjust size to match motivational text
- fill: '#ffffff',
- alpha: 1
- });
- successMessage.anchor.set(0.5, 0); // Adjust anchor to match motivational text
- successMessage.x = 0; // Center horizontally
- successMessage.y = 400; // Position at the same height as motivational text
- LK.gui.top.addChild(successMessage);
- // Initialize variables for success message fade out
- var successMessageFadeOut = true;
- var successMessageFadeDuration = 120; // Duration for fade out in ticks (2 seconds at 60FPS), starts 1 second after appearing
- var successMessageFadeCounter = 0; // Counter to track the number of ticks for fading
- LK.on('tick', function () {
- if (successMessageFadeOut) {
- successMessageFadeCounter++;
- var progress = successMessageFadeCounter / successMessageFadeDuration;
- successMessage.alpha = 1 - progress;
- if (successMessageFadeCounter >= successMessageFadeDuration) {
- successMessageFadeOut = false; // Stop fading
- LK.gui.top.removeChild(successMessage); // Ensure the message is removed after fading
+ lifeIcons = []; // Clear the lifeIcons array
+ // Recreate life icons
+ for (var i = 0; i < lives; i++) {
+ var lifeIcon = LK.getAsset('lifeIcon', {
+ anchorX: 1,
+ anchorY: 0,
+ x: -i * 90 - 50,
+ // Move life icons 50 pixels to the left
+ y: 100,
+ alpha: 1 // Make life icons visible
+ });
+ LK.gui.topRight.addChild(lifeIcon);
+ lifeIcons.push(lifeIcon);
+ }
+ // Update score text to reflect current score dynamically
+ LK.setScore(LK.getScore() + 10 * level);
+ correctGuess = 0;
+ guessedPairs = {};
+ combinationsText.setText(correctGuess + '/' + correctCombinations);
+ levelText.setText('Level ' + level);
+ countdown = 10;
+ goalText.visible = false;
+ combinationsText.visible = false;
+ countdownText.alpha = 1;
+ countdownText.setText(countdown);
+ LK.clearInterval(countdownInterval);
+ countdownInterval = LK.setInterval(function () {
+ countdown--;
+ if (countdown <= 0) {
+ countdownText.alpha = 0;
+ goalText.visible = true;
+ combinationsText.visible = true;
+ LK.clearInterval(countdownInterval);
+ for (var i = 0; i < hexTiles.length; i++) {
+ hexTiles[i].getChildAt(1).setText('');
+ }
+ } else {
+ countdownText.setText(countdown);
}
+ }, 1000);
+ LK.effects.flashScreen(0x90EE90, 500);
+ // Show success message
+ var successMessages = ["Great Job!", "Level Cleared!", "Awesome!", "Well Done!", "Fantastic!"];
+ var randomSuccessMessageIndex = Math.floor(Math.random() * successMessages.length);
+ var successMessage = new Text2(successMessages[randomSuccessMessageIndex], {
+ size: 150,
+ // Adjust size to match motivational text
+ fill: '#ffffff',
+ alpha: 1
+ });
+ successMessage.anchor.set(0.5, 0); // Adjust anchor to match motivational text
+ successMessage.x = 0; // Center horizontally
+ successMessage.y = 400; // Position at the same height as motivational text
+ LK.gui.top.addChild(successMessage);
+ // Initialize variables for success message fade out
+ var successMessageFadeOut = true;
+ var successMessageFadeDuration = 120; // Duration for fade out in ticks (2 seconds at 60FPS), starts 1 second after appearing
+ var successMessageFadeCounter = 0; // Counter to track the number of ticks for fading
+ LK.on('tick', function () {
+ if (successMessageFadeOut) {
+ successMessageFadeCounter++;
+ var progress = successMessageFadeCounter / successMessageFadeDuration;
+ successMessage.alpha = 1 - progress;
+ if (successMessageFadeCounter >= successMessageFadeDuration) {
+ successMessageFadeOut = false; // Stop fading
+ LK.gui.top.removeChild(successMessage); // Ensure the message is removed after fading
+ }
+ }
+ });
+ // Remove previous hex tiles
+ for (var i = 0; i < hexTiles.length; i++) {
+ hexTiles[i].destroy();
}
- });
- // Remove previous hex tiles
- for (var i = 0; i < hexTiles.length; i++) {
- hexTiles[i].destroy();
- }
- hexTiles = [];
- initializeHexTiles();
- // Update goal and guesses with new numbers on the new grid
- // Ensure a different random background color is chosen when the level changes, different from the previous one.
- var previousBackgroundColor = game.backgroundColor;
- var newColors = [0x8AACB8, 0x61AC84, 0xcc919a, 0xFFA07A, 0xB39BDB, 0x73be73].filter(function (color) {
- return color !== previousBackgroundColor;
- });
- game.setBackgroundColor(newColors[Math.floor(Math.random() * newColors.length)]);
- var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
- var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
- while (randomTile1 === randomTile2) {
- randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
- }
- goalNumber = randomTile1.number + randomTile2.number;
- goalText.setText(goalNumber);
- // Reset combinations
- correctCombinations = 0;
- for (var i = 0; i < hexTiles.length; i++) {
- for (var j = i + 1; j < hexTiles.length; j++) {
- if (hexTiles[i].number + hexTiles[j].number === goalNumber) {
- correctCombinations++;
+ hexTiles = [];
+ initializeHexTiles();
+ // Update goal and guesses with new numbers on the new grid
+ // Ensure a different random background color is chosen when the level changes, different from the previous one.
+ var previousBackgroundColor = game.backgroundColor;
+ var newColors = [0x8AACB8, 0x61AC84, 0xcc919a, 0xFFA07A, 0xB39BDB, 0x73be73].filter(function (color) {
+ return color !== previousBackgroundColor;
+ });
+ game.setBackgroundColor(newColors[Math.floor(Math.random() * newColors.length)]);
+ var randomTile1 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
+ var randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
+ while (randomTile1 === randomTile2) {
+ randomTile2 = hexTiles[Math.floor(Math.random() * hexTiles.length)];
+ }
+ goalNumber = randomTile1.number + randomTile2.number;
+ goalText.setText(goalNumber);
+ // Reset combinations
+ correctCombinations = 0;
+ for (var i = 0; i < hexTiles.length; i++) {
+ for (var j = i + 1; j < hexTiles.length; j++) {
+ if (hexTiles[i].number + hexTiles[j].number === goalNumber) {
+ correctCombinations++;
+ }
}
}
- }
- combinationsText.setText(correctGuess + '/' + correctCombinations);
- }
+ combinationsText.setText(correctGuess + '/' + correctCombinations);
+ }, 1000); // Wait one second before executing level transition
+ } // End of level transition delay
} else {
lives--;
lifeIcons[lives].destroy();
if (lives <= 0) {
- LK.showGameOver();
+ LK.showGameOver("Your Score: " + LK.getScore());
} else {
LK.effects.flashScreen(0xFFA07A, 500);
+ // Select a random missed attempt message
+ var missedMessageIndex = Math.floor(Math.random() * missedAttemptMessages.length);
+ var missedMessage = new Text2(missedAttemptMessages[missedMessageIndex], {
+ size: 100,
+ fill: '#ffffff',
+ alpha: 0 // Initially invisible
+ });
+ missedMessage.anchor.set(0.5, 0);
+ missedMessage.x = 0; // Center horizontally
+ missedMessage.y = 1650; // Position at the top of the screen
+ LK.gui.top.addChild(missedMessage);
+ // Fade in the missed attempt message
+ var missedMessageFadeIn = true;
+ var missedMessageFadeDuration = 120; // Duration for fade in and out in ticks (1 second at 60FPS)
+ var missedMessageFadeCounter = 0; // Counter to track the number of ticks for fading
+ LK.on('tick', function () {
+ if (missedMessageFadeIn) {
+ missedMessageFadeCounter++;
+ var progress = missedMessageFadeCounter / missedMessageFadeDuration;
+ missedMessage.alpha = progress;
+ if (missedMessageFadeCounter >= missedMessageFadeDuration / 2) {
+ missedMessageFadeIn = false; // Start fading out after reaching half duration
+ }
+ } else {
+ missedMessageFadeCounter--;
+ var progress = missedMessageFadeCounter / missedMessageFadeDuration;
+ missedMessage.alpha = progress;
+ if (missedMessageFadeCounter <= 0) {
+ LK.gui.top.removeChild(missedMessage); // Remove the message after fading out
+ }
+ }
+ });
}
LK.setTimeout(function () {
if (selectedTiles.length > 0) {
selectedTiles[0].toggleSelect();