Code edit (1 edits merged)
Please save this source code
User prompt
make stroke 20, and set align 'center'
User prompt
add a line break after Congratulations, set font size to 100 and stroke the text
User prompt
make the win condition getting to 8
User prompt
add the win message to gui.center rather than trying to position it yourself
User prompt
If any tile reaches a value of 64, add a "congratulations you win message" text to the center of the screen for 5 seconds. Only do this once
User prompt
Fix Bug: 'TypeError: LK.showWinMessage is not a function. (In 'LK.showWinMessage()', 'LK.showWinMessage' is undefined)' in this line: 'LK.showWinMessage();' Line Number: 225
User prompt
If any tile reaches a value of 128, show a "congratulations you win message" over the screen for 5 seconds. Only do this once
Code edit (2 edits merged)
Please save this source code
User prompt
you are multiplying scale factor in the text scale test, you should devide
Code edit (3 edits merged)
Please save this source code
User prompt
when calculating scale, factor in the text's current scale factor
Code edit (1 edits merged)
Please save this source code
User prompt
in updateToRealValue use scale to set the size of text such that width of text is never more than 400px. Make this efficient
User prompt
don't set boardChanged in up
Code edit (1 edits merged)
Please save this source code
User prompt
in up simply set the value of boardChanged to the inverse of return of the squares some
Code edit (1 edits merged)
Please save this source code
User prompt
in up simply set the value of boardChanged to the return of the squares some
User prompt
Instead of adding tiles right away, add a new tile when everything stop moving
User prompt
update the move speed of tiles from 15 to 100
Code edit (1 edits merged)
Please save this source code
User prompt
in tile move, tiles should only keep moving if distance is larger than 15
User prompt
update the distance threshold to 15px in move
User prompt
Make speed of tiles 15px per move, also update the distance threshold to this
var Tile = Container.expand(function (value) { var self = Container.call(this); self.value = value || 2; var tileGraphics = self.createAsset('tile', 'Tile Graphics', 0, 0); var tileLabel = new Text2(self.value.toString(), { size: 300, fill: '#ffffff', font: 'bold' }); tileLabel.anchor.set(0.5, 0.5); tileLabel.x = tileGraphics.width / 2; tileLabel.y = tileGraphics.height / 2; tileGraphics.addChild(tileLabel); self.setValue = function (value) { self.newValue = value; }; self.updateToRealValue = function () { if (self.newValue) { self.value = self.newValue; tileLabel.setText(self.value.toString()); self.newValue = null; } }; self.targetX = 0; self.targetY = 0; self.isMoving = false; self.merged = false; self.setPosition = function (x, y, instant) { self.targetX = x * 500; self.targetY = y * 500; if (instant) { self.x = self.targetX; self.y = self.targetY; } }; self.move = function () { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 0) { self.isMoving = true; self.x += dx / distance * 15; self.y += dy / distance * 15; return false; } self.isMoving = false; self.x = self.targetX; self.y = self.targetY; return true; }; }); var Game = Container.expand(function () { var self = Container.call(this); var toBeRemoved = []; self.addRandomTile = function () { var emptyPositions = []; for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { if (squares[i][j] === null) { emptyPositions.push({ x: i, y: j }); } } } if (emptyPositions.length > 0) { var randomPosition = emptyPositions[Math.floor(Math.random() * emptyPositions.length)]; var square = new Tile(); square.setPosition(randomPosition.x, randomPosition.y, true); squares[randomPosition.x][randomPosition.y] = square; gridContainer.addChild(square); } }; self.mergeTiles = function (x1, y1, x2, y2) { if (squares[x1][y1] !== null && squares[x2][y2] !== null && squares[x1][y1].value === squares[x2][y2].value && !squares[x1][y1].merged && !squares[x2][y2].merged) { squares[x2][y2].setValue(squares[x2][y2].value * 2, true); squares[x2][y2].merged = true; squares[x1][y1].targetX = squares[x2][y2].targetX; squares[x1][y1].targetY = squares[x2][y2].targetY; toBeRemoved.push(squares[x1][y1]); squares[x1][y1] = null; gridContainer.removeChild(squares[x2][y2]); gridContainer.addChild(squares[x2][y2]); return true; } return false; }; var gridSize = 4; LK.stageContainer.setBackgroundColor(0xD3D3D3); var gridContainer = new Container(); gridContainer.x = 44; gridContainer.y = 200; self.addChild(gridContainer); var squares = new Array(gridSize).fill().map(() => new Array(gridSize).fill(null)); for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { squares[i][j] = null; } } var initialTiles = 2; for (var i = 0; i < initialTiles; i++) { var x, y; do { x = Math.floor(Math.random() * gridSize); y = Math.floor(Math.random() * gridSize); } while (squares[x][y] !== null); var square = new Tile(); square.setPosition(x, y, true); squares[x][y] = square; gridContainer.addChild(square); } var swipeStart = { x: 0, y: 0 }; var swipeEnd = { x: 0, y: 0 }; var boardChanged = false; self.moveTiles = function (direction) { var dx = (direction === 'right') - (direction === 'left'); var dy = (direction === 'down') - (direction === 'up'); var moved = false; do { moved = false; var iStart = dx > 0 ? gridSize - 1 : 0; var iEnd = dx > 0 ? -1 : gridSize; var iStep = dx > 0 ? -1 : 1; var jStart = dy > 0 ? gridSize - 1 : 0; var jEnd = dy > 0 ? -1 : gridSize; var jStep = dy > 0 ? -1 : 1; for (var i = iStart; i !== iEnd; i += iStep) { for (var j = jStart; j !== jEnd; j += jStep) { var x = i + dx; var y = j + dy; if (x >= 0 && x < gridSize && y >= 0 && y < gridSize && squares[i][j] !== null) { if (squares[x][y] === null) { squares[x][y] = squares[i][j]; squares[i][j] = null; squares[x][y].setPosition(x, y); moved = true; boardChanged = true; } else if (squares[x][y] !== null && squares[i][j] !== null && squares[x][y].value === squares[i][j].value) { moved = self.mergeTiles(i, j, x, y); if (moved) boardChanged = true; } } } } } while (moved); for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { if (squares[i][j] !== null) { squares[i][j].merged = false; } } } }; stage.on('down', function (obj) { var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving)); if (!anyTileMoving) { var pos = obj.event.getLocalPosition(self); swipeStart = { x: pos.x, y: pos.y }; } }); stage.on('up', function (obj) { var pos = obj.event.getLocalPosition(self); swipeEnd = { x: pos.x, y: pos.y }; var dx = swipeEnd.x - swipeStart.x; var dy = swipeEnd.y - swipeStart.y; var threshold = 50; if (Math.abs(dx) > threshold || Math.abs(dy) > threshold) { if (Math.abs(dx) > Math.abs(dy)) { if (dx > 0) { self.moveTiles('right'); } else { self.moveTiles('left'); } } else { if (dy > 0) { self.moveTiles('down'); } else { self.moveTiles('up'); } } } var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving)); if (!anyTileMoving && boardChanged) { self.addRandomTile(); boardChanged = false; } }); LK.on('tick', function () { var allTilesStopped = true; for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { if (squares[i][j] !== null) { if (!squares[i][j].move()) { allTilesStopped = false; } } } } for (var i = 0; i < toBeRemoved.length; i++) { if (!toBeRemoved[i].move()) { allTilesStopped = false; } else { toBeRemoved[i].destroy(); toBeRemoved.splice(i, 1); i--; } } if (allTilesStopped) { for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { if (squares[i][j] !== null && squares[i][j].newValue) { squares[i][j].updateToRealValue(); } } } } }); });
var Tile = Container.expand(function (value) {
var self = Container.call(this);
self.value = value || 2;
var tileGraphics = self.createAsset('tile', 'Tile Graphics', 0, 0);
var tileLabel = new Text2(self.value.toString(), {
size: 300,
fill: '#ffffff',
font: 'bold'
});
tileLabel.anchor.set(0.5, 0.5);
tileLabel.x = tileGraphics.width / 2;
tileLabel.y = tileGraphics.height / 2;
tileGraphics.addChild(tileLabel);
self.setValue = function (value) {
self.newValue = value;
};
self.updateToRealValue = function () {
if (self.newValue) {
self.value = self.newValue;
tileLabel.setText(self.value.toString());
self.newValue = null;
}
};
self.targetX = 0;
self.targetY = 0;
self.isMoving = false;
self.merged = false;
self.setPosition = function (x, y, instant) {
self.targetX = x * 500;
self.targetY = y * 500;
if (instant) {
self.x = self.targetX;
self.y = self.targetY;
}
};
self.move = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.isMoving = true;
self.x += dx / distance * 15;
self.y += dy / distance * 15;
return false;
}
self.isMoving = false;
self.x = self.targetX;
self.y = self.targetY;
return true;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var toBeRemoved = [];
self.addRandomTile = function () {
var emptyPositions = [];
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] === null) {
emptyPositions.push({
x: i,
y: j
});
}
}
}
if (emptyPositions.length > 0) {
var randomPosition = emptyPositions[Math.floor(Math.random() * emptyPositions.length)];
var square = new Tile();
square.setPosition(randomPosition.x, randomPosition.y, true);
squares[randomPosition.x][randomPosition.y] = square;
gridContainer.addChild(square);
}
};
self.mergeTiles = function (x1, y1, x2, y2) {
if (squares[x1][y1] !== null && squares[x2][y2] !== null && squares[x1][y1].value === squares[x2][y2].value && !squares[x1][y1].merged && !squares[x2][y2].merged) {
squares[x2][y2].setValue(squares[x2][y2].value * 2, true);
squares[x2][y2].merged = true;
squares[x1][y1].targetX = squares[x2][y2].targetX;
squares[x1][y1].targetY = squares[x2][y2].targetY;
toBeRemoved.push(squares[x1][y1]);
squares[x1][y1] = null;
gridContainer.removeChild(squares[x2][y2]);
gridContainer.addChild(squares[x2][y2]);
return true;
}
return false;
};
var gridSize = 4;
LK.stageContainer.setBackgroundColor(0xD3D3D3);
var gridContainer = new Container();
gridContainer.x = 44;
gridContainer.y = 200;
self.addChild(gridContainer);
var squares = new Array(gridSize).fill().map(() => new Array(gridSize).fill(null));
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
squares[i][j] = null;
}
}
var initialTiles = 2;
for (var i = 0; i < initialTiles; i++) {
var x, y;
do {
x = Math.floor(Math.random() * gridSize);
y = Math.floor(Math.random() * gridSize);
} while (squares[x][y] !== null);
var square = new Tile();
square.setPosition(x, y, true);
squares[x][y] = square;
gridContainer.addChild(square);
}
var swipeStart = {
x: 0,
y: 0
};
var swipeEnd = {
x: 0,
y: 0
};
var boardChanged = false;
self.moveTiles = function (direction) {
var dx = (direction === 'right') - (direction === 'left');
var dy = (direction === 'down') - (direction === 'up');
var moved = false;
do {
moved = false;
var iStart = dx > 0 ? gridSize - 1 : 0;
var iEnd = dx > 0 ? -1 : gridSize;
var iStep = dx > 0 ? -1 : 1;
var jStart = dy > 0 ? gridSize - 1 : 0;
var jEnd = dy > 0 ? -1 : gridSize;
var jStep = dy > 0 ? -1 : 1;
for (var i = iStart; i !== iEnd; i += iStep) {
for (var j = jStart; j !== jEnd; j += jStep) {
var x = i + dx;
var y = j + dy;
if (x >= 0 && x < gridSize && y >= 0 && y < gridSize && squares[i][j] !== null) {
if (squares[x][y] === null) {
squares[x][y] = squares[i][j];
squares[i][j] = null;
squares[x][y].setPosition(x, y);
moved = true;
boardChanged = true;
} else if (squares[x][y] !== null && squares[i][j] !== null && squares[x][y].value === squares[i][j].value) {
moved = self.mergeTiles(i, j, x, y);
if (moved) boardChanged = true;
}
}
}
}
} while (moved);
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] !== null) {
squares[i][j].merged = false;
}
}
}
};
stage.on('down', function (obj) {
var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving));
if (!anyTileMoving) {
var pos = obj.event.getLocalPosition(self);
swipeStart = {
x: pos.x,
y: pos.y
};
}
});
stage.on('up', function (obj) {
var pos = obj.event.getLocalPosition(self);
swipeEnd = {
x: pos.x,
y: pos.y
};
var dx = swipeEnd.x - swipeStart.x;
var dy = swipeEnd.y - swipeStart.y;
var threshold = 50;
if (Math.abs(dx) > threshold || Math.abs(dy) > threshold) {
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
self.moveTiles('right');
} else {
self.moveTiles('left');
}
} else {
if (dy > 0) {
self.moveTiles('down');
} else {
self.moveTiles('up');
}
}
}
var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving));
if (!anyTileMoving && boardChanged) {
self.addRandomTile();
boardChanged = false;
}
});
LK.on('tick', function () {
var allTilesStopped = true;
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] !== null) {
if (!squares[i][j].move()) {
allTilesStopped = false;
}
}
}
}
for (var i = 0; i < toBeRemoved.length; i++) {
if (!toBeRemoved[i].move()) {
allTilesStopped = false;
} else {
toBeRemoved[i].destroy();
toBeRemoved.splice(i, 1);
i--;
}
}
if (allTilesStopped) {
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] !== null && squares[i][j].newValue) {
squares[i][j].updateToRealValue();
}
}
}
}
});
});