/**** * Classes ****/ // SpeedPowerUp class var SpeedPowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.createAsset('speedPowerUp', 'Speed Power Up', 0.5, 0.5); self.activate = function (ai) { var originalSpeed = ai.speed; ai.speed *= 0.5; // Halve the AI's speed LK.setTimeout(function () { ai.speed = originalSpeed; // Reset the AI's speed after 10 seconds }, 10000); }; }); // Score class var Score = Container.expand(function () { var self = Container.call(this); self.scoreValue = 0; var scoreText = new Text2(self.scoreValue.toString(), { size: 150, fill: "#ffffff" }); scoreText.anchor.set(.5, 0); LK.gui.top.addChild(scoreText); self.updateScore = function (value) { self.scoreValue += value; scoreText.setText(self.scoreValue.toString()); }; }); // SpawnMarker class var SpawnMarker = Container.expand(function () { var self = Container.call(this); var markerGraphics = self.createAsset('spawnMarker', 'Spawn Marker', 0.5, 0.5); markerGraphics.tint = 0xff0000; // Color the marker red }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.createAsset('playerCube', 'Player Cube', 0.5, 0.5); self.speed = 5; self.moveUp = function () { self.y -= self.speed; }; self.moveDown = function () { self.y += self.speed; }; self.moveLeft = function () { self.x -= self.speed; }; self.moveRight = function () { self.x += self.speed; }; self.teleportUses = 2; self.teleport = function () { if (self.teleportUses > 0) { self.x = Math.random() * 2048; self.y = Math.random() * 2732; self.teleportUses--; } }; self.die = function () { LK.effects.flashObject(self, 0xff0000, 1000); LK.showGameOver(); }; }); // AI class var AI = Container.expand(function () { var self = Container.call(this); var aiGraphics = self.createAsset('aiCube', 'AI Cube', 0.5, 0.5); self.speed = 1.5; self.speedModifier = 0.05; // Speed increases by 0.05 each second self.follow = function (target) { var dx = target.x - self.x; var dy = target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 1) { self.speed += self.speedModifier * 0.5; self.x += dx / distance * (self.speed + self.speedModifier * Math.floor(LK.ticks / 60) * 0.5); self.y += dy / distance * (self.speed + self.speedModifier * Math.floor(LK.ticks / 60) * 0.5); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ if (LK.ticks % 900 == 0) { // Every 15 seconds var powerUpX = Math.random() * 2048; var powerUpY = Math.random() * 2732; var speedPowerUp = new SpeedPowerUp(); speedPowerUp.x = powerUpX; speedPowerUp.y = powerUpY; game.addChild(speedPowerUp); LK.setTimeout(function () { if (game.children.indexOf(speedPowerUp) !== -1) { speedPowerUp.destroy(); } }, 10000); // Remove the power up after 10 seconds if not collected } var score = game.addChild(new Score()); // Initialize player and AI var player = game.addChild(new Player()); player.x = 1024; // Center of the screen horizontally player.y = 1366; // Center of the screen vertically var ai = game.addChild(new AI()); ai.x = 1024; // Start in the center horizontally ai.y = 500; // Start above the player // Handle keyboard input for player movement function handleKeyDown(event) { switch (event.key) { case 'ArrowUp': player.moveUp(); break; case 'ArrowDown': player.moveDown(); break; case 'ArrowLeft': player.moveLeft(); break; case 'ArrowRight': player.moveRight(); break; case 't': player.teleport(); break; } } // Add event listener for touch controls function handleTouchMove(obj) { var touchPos = obj.event.getLocalPosition(game); player.x = touchPos.x; player.y = touchPos.y; } game.on('move', handleTouchMove); // Main game loop LK.on('tick', function () { // Increment score based on survival score.updateScore(1); ai.follow(player); if (LK.ticks % 300 == 0) { var spawnX = Math.random() * 2048; var spawnY = Math.random() * 2732; var spawnMarker = new SpawnMarker(); spawnMarker.x = spawnX; spawnMarker.y = spawnY; game.addChild(spawnMarker); LK.setTimeout(function () { var newAI = new AI(); newAI.x = spawnX; newAI.y = spawnY; game.addChild(newAI); spawnMarker.destroy(); }, 1000); // Spawn AI after 1 second and then remove the marker } // Spawn SpeedPowerUp randomly }); // Add keyboard event listener LK.on('keydown', handleKeyDown); // Ensure the player cannot move outside the game boundaries player.update = function () { this.x = Math.max(0, Math.min(this.x, 2048)); this.y = Math.max(0, Math.min(this.y, 2732)); // Check for collision with SpeedPowerUp and slow down the AI var powerUps = game.children.filter(function (child) { return child instanceof SpeedPowerUp; }); var ais = game.children.filter(function (child) { return child instanceof AI; }); for (var i = 0; i < powerUps.length; i++) { if (this.intersects(powerUps[i])) { ais.forEach(function (ai) { powerUps[i].activate(ai); }); powerUps[i].destroy(); } } }; // Ensure the AI cannot move outside the game boundaries AI.prototype.update = function () { this.x = Math.max(0, Math.min(this.x, 2048)); this.y = Math.max(0, Math.min(this.y, 2732)); }; // Update entities on each tick and check for collision LK.on('tick', function () { player.update(); var playerCollidedWithAI = game.children.some(function (child) { if (child instanceof AI && player.intersects(child)) { player.die(); return true; } if (child instanceof AI) { child.update(); } return false; }); if (playerCollidedWithAI) { return; } });
/****
* Classes
****/
// SpeedPowerUp class
var SpeedPowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.createAsset('speedPowerUp', 'Speed Power Up', 0.5, 0.5);
self.activate = function (ai) {
var originalSpeed = ai.speed;
ai.speed *= 0.5; // Halve the AI's speed
LK.setTimeout(function () {
ai.speed = originalSpeed; // Reset the AI's speed after 10 seconds
}, 10000);
};
});
// Score class
var Score = Container.expand(function () {
var self = Container.call(this);
self.scoreValue = 0;
var scoreText = new Text2(self.scoreValue.toString(), {
size: 150,
fill: "#ffffff"
});
scoreText.anchor.set(.5, 0);
LK.gui.top.addChild(scoreText);
self.updateScore = function (value) {
self.scoreValue += value;
scoreText.setText(self.scoreValue.toString());
};
});
// SpawnMarker class
var SpawnMarker = Container.expand(function () {
var self = Container.call(this);
var markerGraphics = self.createAsset('spawnMarker', 'Spawn Marker', 0.5, 0.5);
markerGraphics.tint = 0xff0000; // Color the marker red
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.createAsset('playerCube', 'Player Cube', 0.5, 0.5);
self.speed = 5;
self.moveUp = function () {
self.y -= self.speed;
};
self.moveDown = function () {
self.y += self.speed;
};
self.moveLeft = function () {
self.x -= self.speed;
};
self.moveRight = function () {
self.x += self.speed;
};
self.teleportUses = 2;
self.teleport = function () {
if (self.teleportUses > 0) {
self.x = Math.random() * 2048;
self.y = Math.random() * 2732;
self.teleportUses--;
}
};
self.die = function () {
LK.effects.flashObject(self, 0xff0000, 1000);
LK.showGameOver();
};
});
// AI class
var AI = Container.expand(function () {
var self = Container.call(this);
var aiGraphics = self.createAsset('aiCube', 'AI Cube', 0.5, 0.5);
self.speed = 1.5;
self.speedModifier = 0.05; // Speed increases by 0.05 each second
self.follow = function (target) {
var dx = target.x - self.x;
var dy = target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 1) {
self.speed += self.speedModifier * 0.5;
self.x += dx / distance * (self.speed + self.speedModifier * Math.floor(LK.ticks / 60) * 0.5);
self.y += dy / distance * (self.speed + self.speedModifier * Math.floor(LK.ticks / 60) * 0.5);
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
if (LK.ticks % 900 == 0) {
// Every 15 seconds
var powerUpX = Math.random() * 2048;
var powerUpY = Math.random() * 2732;
var speedPowerUp = new SpeedPowerUp();
speedPowerUp.x = powerUpX;
speedPowerUp.y = powerUpY;
game.addChild(speedPowerUp);
LK.setTimeout(function () {
if (game.children.indexOf(speedPowerUp) !== -1) {
speedPowerUp.destroy();
}
}, 10000); // Remove the power up after 10 seconds if not collected
}
var score = game.addChild(new Score());
// Initialize player and AI
var player = game.addChild(new Player());
player.x = 1024; // Center of the screen horizontally
player.y = 1366; // Center of the screen vertically
var ai = game.addChild(new AI());
ai.x = 1024; // Start in the center horizontally
ai.y = 500; // Start above the player
// Handle keyboard input for player movement
function handleKeyDown(event) {
switch (event.key) {
case 'ArrowUp':
player.moveUp();
break;
case 'ArrowDown':
player.moveDown();
break;
case 'ArrowLeft':
player.moveLeft();
break;
case 'ArrowRight':
player.moveRight();
break;
case 't':
player.teleport();
break;
}
}
// Add event listener for touch controls
function handleTouchMove(obj) {
var touchPos = obj.event.getLocalPosition(game);
player.x = touchPos.x;
player.y = touchPos.y;
}
game.on('move', handleTouchMove);
// Main game loop
LK.on('tick', function () {
// Increment score based on survival
score.updateScore(1);
ai.follow(player);
if (LK.ticks % 300 == 0) {
var spawnX = Math.random() * 2048;
var spawnY = Math.random() * 2732;
var spawnMarker = new SpawnMarker();
spawnMarker.x = spawnX;
spawnMarker.y = spawnY;
game.addChild(spawnMarker);
LK.setTimeout(function () {
var newAI = new AI();
newAI.x = spawnX;
newAI.y = spawnY;
game.addChild(newAI);
spawnMarker.destroy();
}, 1000); // Spawn AI after 1 second and then remove the marker
}
// Spawn SpeedPowerUp randomly
});
// Add keyboard event listener
LK.on('keydown', handleKeyDown);
// Ensure the player cannot move outside the game boundaries
player.update = function () {
this.x = Math.max(0, Math.min(this.x, 2048));
this.y = Math.max(0, Math.min(this.y, 2732));
// Check for collision with SpeedPowerUp and slow down the AI
var powerUps = game.children.filter(function (child) {
return child instanceof SpeedPowerUp;
});
var ais = game.children.filter(function (child) {
return child instanceof AI;
});
for (var i = 0; i < powerUps.length; i++) {
if (this.intersects(powerUps[i])) {
ais.forEach(function (ai) {
powerUps[i].activate(ai);
});
powerUps[i].destroy();
}
}
};
// Ensure the AI cannot move outside the game boundaries
AI.prototype.update = function () {
this.x = Math.max(0, Math.min(this.x, 2048));
this.y = Math.max(0, Math.min(this.y, 2732));
};
// Update entities on each tick and check for collision
LK.on('tick', function () {
player.update();
var playerCollidedWithAI = game.children.some(function (child) {
if (child instanceof AI && player.intersects(child)) {
player.die();
return true;
}
if (child instanceof AI) {
child.update();
}
return false;
});
if (playerCollidedWithAI) {
return;
}
});