/**** * Classes ****/ // Assets will be automatically created based on usage in the code. // Magnet class var Magnet = Container.expand(function () { var self = Container.call(this); var magnetGraphics = self.attachAsset('magnet', { anchorX: 0.5, anchorY: 0.5 }); self.pullStrength = 13; // Increased pull strength for stronger attraction self.rotationSpeed = -0.1; // Set the rotation speed for the magnet self.pull = function (object) { var dx = self.x - object.x; var dy = self.y - object.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 550) { // Only pull if within 500 pixels object.x += dx / distance * self.pullStrength; object.y += dy / distance * self.pullStrength; } }; }); // Object class var ObjectToPull = Container.expand(function () { var self = Container.call(this); var objectGraphics = self.attachAsset('object', { anchorX: 0.5, anchorY: 0.5 }); self.isPulled = false; self.isActive = false; // Attach a rower to the object var rower = new Rower(); self.addChild(rower); }); // Rower class with frame alternation var Rower = Container.expand(function () { var self = Container.call(this); // Initialize an array to hold the two frames of the rower graphics var rowerFrames = [self.attachAsset('rower', { anchorX: 0.5, anchorY: 0.5 }), self.attachAsset('rower_1', { anchorX: 0.5, anchorY: 0.5 })]; // Set the initial frame to be visible and the second to be invisible rowerFrames[0].visible = true; rowerFrames[1].visible = false; // Current frame index var currentFrame = 0; // Function to alternate frames self.alternateFrame = function () { // Toggle visibility of frames rowerFrames[currentFrame].visible = false; currentFrame = (currentFrame + 1) % rowerFrames.length; rowerFrames[currentFrame].visible = true; }; // Set an interval to alternate frames every 500ms LK.setInterval(self.alternateFrame, 250); self.speed = 5; self.move = function () { self.x += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var background = game.addChild(LK.getAsset('background', { anchorX: 0, anchorY: 0, x: 0, y: 0, width: 2048, height: 2732 })); var background2 = game.addChild(LK.getAsset('background', { anchorX: 0, anchorY: 0, x: 0, y: -2732, width: 2048, height: 2732 })); var magnet = game.addChild(new Magnet()); magnet.x = 1024; // Center horizontally magnet.y = 1366; // Center vertically var objects = []; // Array to hold objects var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff", stroke: "#000000", // Black outline strokeThickness: 10 // Thickness of the outline }); LK.gui.top.addChild(scoreTxt); scoreTxt.anchor.set(0.5, 0); // Function to spawn objects function spawnObject() { var newObject = new ObjectToPull(); // Randomly position objects at the top of the screen newObject.x = Math.random() * 2048; newObject.y = -100; objects.push(newObject); game.addChild(newObject); } // Game tick function LK.on('tick', function () { magnet.rotation += magnet.rotationSpeed; // Rotate the magnet counterclockwise background.y += 8; // Move the background downwards background2.y += 8; // Move the second background downwards // Spawn an object every 30 frames (about 0.5 second) // For every 2 points increment in the score, decrease the spawning value by 1 if (LK.ticks % Math.max(1, 60 - Math.floor(LK.getScore() / 2)) == 0) { spawnObject(); } // Move, rotate and check for magnet pull objects.forEach(function (object) { object.y += 15; // Objects move downwards magnet.pull(object); // Magnet tries to pull each object // Calculate the angle between the object and the magnet var dx = magnet.x - object.x; var dy = magnet.y - object.y; var angle = Math.atan2(dy, dx); // Rotate the object to face the magnet object.rotation = angle; // Check if object reaches the bottom of the screen if (object.y > 2732) { object.isActive = false; // Deactivate object } // Check if the bottom background has fully exited the screen view if (background.y >= 2732) { background.y = background2.y - 2732; // Reattach it above the existing background } if (background2.y >= 2732) { background2.y = background.y - 2732; // Reattach it above the existing background } // Trigger game over if object is within a certain distance of the magnet var dx = magnet.x - object.x; var dy = magnet.y - object.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < magnet.width / 2 + object.width / 2) { LK.showGameOver(); } // Check for collision with other objects // Implement spatial partitioning to reduce the number of collision checks var cellSize = 100; // Size of each cell in the grid var grid = []; // 2D array representing the grid for (var i = 0; i < 2048 / cellSize; i++) { grid[i] = []; } // Assign each object to a cell in the grid objects.forEach(function (object) { var cellX = Math.floor(object.x / cellSize); var cellY = Math.floor(object.y / cellSize); grid[cellX][cellY] = object; }); // Only check for collisions between objects in the same or adjacent cells for (var i = 0; i < grid.length; i++) { for (var j = 0; j < grid[i].length; j++) { var object1 = grid[i][j]; if (object1) { // Check for collisions with objects in the same cell for (var k = i - 1; k <= i + 1; k++) { for (var l = j - 1; l <= j + 1; l++) { if (k >= 0 && k < grid.length && l >= 0 && l < grid[i].length) { var object2 = grid[k][l]; if (object2 && object1 !== object2 && object1.intersects(object2)) { // Objects are too close, apply a repulsion force var dx = object2.x - object1.x; var dy = object2.y - object1.y; var distance = Math.sqrt(dx * dx + dy * dy); var force = 100 / distance; // The force is stronger when the objects are closer object1.x -= dx / distance * force; object1.y -= dy / distance * force; object2.x += dx / distance * force; object2.y += dy / distance * force; } } } } } } } }); // Update score and deactivate objects that have exited the screen objects = objects.filter(function (object) { if (object.y > 2832) { // Adjust condition to check if object is 100 pixels below the screen edge object.isActive = true; // Mark object as scored to prevent re-scoring LK.setScore(LK.getScore() + 1); // Increment score by 1 scoreTxt.setText(LK.getScore().toString()); // Update score text return false; // Remove object from the array } return true; }); }); // Touch event to move magnet var isFingerDown = false; // Track if the finger is held down game.on('down', function (obj) { isFingerDown = true; // Set flag to true when finger touches the screen }); var magnetSpeedLimit = 30; // Maximum speed of the magnet game.on('move', function (obj) { // Update target position regardless of whether finger is held down targetPos = obj.event.getLocalPosition(game); }); LK.on('tick', function () { if (targetPos) { // Move magnet towards target position var dx = targetPos.x - magnet.x; var dy = targetPos.y - magnet.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > magnetSpeedLimit) { // If the distance to the target position is greater than the speed limit, // move the magnet at the maximum speed in the direction of the target. magnet.x += dx / distance * magnetSpeedLimit; magnet.y += dy / distance * magnetSpeedLimit; } else { // If the distance to the target position is less than the speed limit, // move the magnet directly to the target. magnet.x = targetPos.x; magnet.y = targetPos.y; } } }); var targetPos = null; // Target position for the magnet game.on('up', function (obj) { isFingerDown = false; // Reset flag when finger is lifted targetPos = obj.event.getLocalPosition(game); // Set target position to the last position of the cursor }); // This simple game setup demonstrates the core mechanics of pulling objects with a magnet. // Additional features such as different object types, levels, and challenges can be added to enhance the gameplay.
/****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// Magnet class
var Magnet = Container.expand(function () {
var self = Container.call(this);
var magnetGraphics = self.attachAsset('magnet', {
anchorX: 0.5,
anchorY: 0.5
});
self.pullStrength = 13; // Increased pull strength for stronger attraction
self.rotationSpeed = -0.1; // Set the rotation speed for the magnet
self.pull = function (object) {
var dx = self.x - object.x;
var dy = self.y - object.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 550) {
// Only pull if within 500 pixels
object.x += dx / distance * self.pullStrength;
object.y += dy / distance * self.pullStrength;
}
};
});
// Object class
var ObjectToPull = Container.expand(function () {
var self = Container.call(this);
var objectGraphics = self.attachAsset('object', {
anchorX: 0.5,
anchorY: 0.5
});
self.isPulled = false;
self.isActive = false;
// Attach a rower to the object
var rower = new Rower();
self.addChild(rower);
});
// Rower class with frame alternation
var Rower = Container.expand(function () {
var self = Container.call(this);
// Initialize an array to hold the two frames of the rower graphics
var rowerFrames = [self.attachAsset('rower', {
anchorX: 0.5,
anchorY: 0.5
}), self.attachAsset('rower_1', {
anchorX: 0.5,
anchorY: 0.5
})];
// Set the initial frame to be visible and the second to be invisible
rowerFrames[0].visible = true;
rowerFrames[1].visible = false;
// Current frame index
var currentFrame = 0;
// Function to alternate frames
self.alternateFrame = function () {
// Toggle visibility of frames
rowerFrames[currentFrame].visible = false;
currentFrame = (currentFrame + 1) % rowerFrames.length;
rowerFrames[currentFrame].visible = true;
};
// Set an interval to alternate frames every 500ms
LK.setInterval(self.alternateFrame, 250);
self.speed = 5;
self.move = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732
}));
var background2 = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0,
x: 0,
y: -2732,
width: 2048,
height: 2732
}));
var magnet = game.addChild(new Magnet());
magnet.x = 1024; // Center horizontally
magnet.y = 1366; // Center vertically
var objects = []; // Array to hold objects
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
stroke: "#000000",
// Black outline
strokeThickness: 10 // Thickness of the outline
});
LK.gui.top.addChild(scoreTxt);
scoreTxt.anchor.set(0.5, 0);
// Function to spawn objects
function spawnObject() {
var newObject = new ObjectToPull();
// Randomly position objects at the top of the screen
newObject.x = Math.random() * 2048;
newObject.y = -100;
objects.push(newObject);
game.addChild(newObject);
}
// Game tick function
LK.on('tick', function () {
magnet.rotation += magnet.rotationSpeed; // Rotate the magnet counterclockwise
background.y += 8; // Move the background downwards
background2.y += 8; // Move the second background downwards
// Spawn an object every 30 frames (about 0.5 second)
// For every 2 points increment in the score, decrease the spawning value by 1
if (LK.ticks % Math.max(1, 60 - Math.floor(LK.getScore() / 2)) == 0) {
spawnObject();
}
// Move, rotate and check for magnet pull
objects.forEach(function (object) {
object.y += 15; // Objects move downwards
magnet.pull(object); // Magnet tries to pull each object
// Calculate the angle between the object and the magnet
var dx = magnet.x - object.x;
var dy = magnet.y - object.y;
var angle = Math.atan2(dy, dx);
// Rotate the object to face the magnet
object.rotation = angle;
// Check if object reaches the bottom of the screen
if (object.y > 2732) {
object.isActive = false; // Deactivate object
}
// Check if the bottom background has fully exited the screen view
if (background.y >= 2732) {
background.y = background2.y - 2732; // Reattach it above the existing background
}
if (background2.y >= 2732) {
background2.y = background.y - 2732; // Reattach it above the existing background
}
// Trigger game over if object is within a certain distance of the magnet
var dx = magnet.x - object.x;
var dy = magnet.y - object.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < magnet.width / 2 + object.width / 2) {
LK.showGameOver();
}
// Check for collision with other objects
// Implement spatial partitioning to reduce the number of collision checks
var cellSize = 100; // Size of each cell in the grid
var grid = []; // 2D array representing the grid
for (var i = 0; i < 2048 / cellSize; i++) {
grid[i] = [];
}
// Assign each object to a cell in the grid
objects.forEach(function (object) {
var cellX = Math.floor(object.x / cellSize);
var cellY = Math.floor(object.y / cellSize);
grid[cellX][cellY] = object;
});
// Only check for collisions between objects in the same or adjacent cells
for (var i = 0; i < grid.length; i++) {
for (var j = 0; j < grid[i].length; j++) {
var object1 = grid[i][j];
if (object1) {
// Check for collisions with objects in the same cell
for (var k = i - 1; k <= i + 1; k++) {
for (var l = j - 1; l <= j + 1; l++) {
if (k >= 0 && k < grid.length && l >= 0 && l < grid[i].length) {
var object2 = grid[k][l];
if (object2 && object1 !== object2 && object1.intersects(object2)) {
// Objects are too close, apply a repulsion force
var dx = object2.x - object1.x;
var dy = object2.y - object1.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var force = 100 / distance; // The force is stronger when the objects are closer
object1.x -= dx / distance * force;
object1.y -= dy / distance * force;
object2.x += dx / distance * force;
object2.y += dy / distance * force;
}
}
}
}
}
}
}
});
// Update score and deactivate objects that have exited the screen
objects = objects.filter(function (object) {
if (object.y > 2832) {
// Adjust condition to check if object is 100 pixels below the screen edge
object.isActive = true; // Mark object as scored to prevent re-scoring
LK.setScore(LK.getScore() + 1); // Increment score by 1
scoreTxt.setText(LK.getScore().toString()); // Update score text
return false; // Remove object from the array
}
return true;
});
});
// Touch event to move magnet
var isFingerDown = false; // Track if the finger is held down
game.on('down', function (obj) {
isFingerDown = true; // Set flag to true when finger touches the screen
});
var magnetSpeedLimit = 30; // Maximum speed of the magnet
game.on('move', function (obj) {
// Update target position regardless of whether finger is held down
targetPos = obj.event.getLocalPosition(game);
});
LK.on('tick', function () {
if (targetPos) {
// Move magnet towards target position
var dx = targetPos.x - magnet.x;
var dy = targetPos.y - magnet.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > magnetSpeedLimit) {
// If the distance to the target position is greater than the speed limit,
// move the magnet at the maximum speed in the direction of the target.
magnet.x += dx / distance * magnetSpeedLimit;
magnet.y += dy / distance * magnetSpeedLimit;
} else {
// If the distance to the target position is less than the speed limit,
// move the magnet directly to the target.
magnet.x = targetPos.x;
magnet.y = targetPos.y;
}
}
});
var targetPos = null; // Target position for the magnet
game.on('up', function (obj) {
isFingerDown = false; // Reset flag when finger is lifted
targetPos = obj.event.getLocalPosition(game); // Set target position to the last position of the cursor
});
// This simple game setup demonstrates the core mechanics of pulling objects with a magnet.
// Additional features such as different object types, levels, and challenges can be added to enhance the gameplay.