User prompt
speed increase 2 times,
User prompt
decrease diamonds
User prompt
make player move with a 360 degree console button make cars moving in all directions like real cars
User prompt
show player's car in different colour decrease number of diamonds make a continuous background with like a city map
User prompt
put chasing car in the middle, and make the map is moving with pace (increases after each crash with a 1 percent) ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
Police Chase Escape
Initial prompt
police runner, 2D, top-down, controlling car, escaping from police cars, there are exceptional gold diamonds etc., police and background changes smoothly after milestones, aim is to crush police cars with each other (like police pursuit game) while not crushing with them
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var CityBackground = Container.expand(function () {
var self = Container.call(this);
var backgroundElements = [];
self.tileHeight = 400;
self.tilesPerRow = 10;
self.rows = 10;
// Create initial background tiles
for (var row = 0; row < self.rows; row++) {
for (var col = 0; col < self.tilesPerRow; col++) {
var element;
var elementType = Math.floor(Math.random() * 3);
if (elementType === 0) {
// Road
element = LK.getAsset('road', {
anchorX: 0,
anchorY: 0
});
element.width = 200;
element.height = 100;
} else if (elementType === 1) {
// Building
element = LK.getAsset('building', {
anchorX: 0,
anchorY: 0
});
element.width = 150;
element.height = 200;
} else {
// City block
element = LK.getAsset('cityBlock', {
anchorX: 0,
anchorY: 0
});
element.width = 200;
element.height = 200;
}
element.x = col * 200;
element.y = (row - 2) * self.tileHeight; // Start some tiles above screen
element.alpha = 0.7;
self.addChild(element);
backgroundElements.push(element);
}
}
self.update = function () {
// Move all background elements with map speed
for (var i = backgroundElements.length - 1; i >= 0; i--) {
var element = backgroundElements[i];
element.y += mapSpeed;
// Remove elements that went off screen bottom
if (element.y > 2732 + 400) {
element.destroy();
backgroundElements.splice(i, 1);
}
}
// Add new row of tiles at the top when needed
if (backgroundElements.length < self.tilesPerRow * 8) {
var topY = -800;
for (var col = 0; col < self.tilesPerRow; col++) {
var element;
var elementType = Math.floor(Math.random() * 3);
if (elementType === 0) {
element = LK.getAsset('road', {
anchorX: 0,
anchorY: 0
});
element.width = 200;
element.height = 100;
} else if (elementType === 1) {
element = LK.getAsset('building', {
anchorX: 0,
anchorY: 0
});
element.width = 150;
element.height = 200;
} else {
element = LK.getAsset('cityBlock', {
anchorX: 0,
anchorY: 0
});
element.width = 200;
element.height = 200;
}
element.x = col * 200;
element.y = topY;
element.alpha = 0.7;
self.addChild(element);
backgroundElements.push(element);
}
}
};
return self;
});
var Diamond = Container.expand(function () {
var self = Container.call(this);
var diamondGraphics = self.attachAsset('diamond', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.rotationSpeed = 0.05;
self.update = function () {
diamondGraphics.rotation += self.rotationSpeed;
// Move with map scrolling
self.y += mapSpeed;
// Remove diamonds that go off screen
if (self.y > 2732 + 100) {
self.destroy();
}
};
return self;
});
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
self.lifetime = 30;
self.age = 0;
self.update = function () {
self.age++;
explosionGraphics.alpha = 1 - self.age / self.lifetime;
explosionGraphics.scaleX = 1 + self.age / self.lifetime;
explosionGraphics.scaleY = 1 + self.age / self.lifetime;
// Move with map scrolling
self.y += mapSpeed;
if (self.age >= self.lifetime) {
self.destroy();
}
};
return self;
});
var GetawayCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('getawayCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.maxSpeed = 8;
self.velocityX = 0;
self.velocityY = 0;
self.acceleration = 0.3;
self.friction = 0.9;
self.update = function () {
// Apply joystick input if available
if (joystick && joystick.isActive) {
self.velocityX += joystick.currentX * self.acceleration;
self.velocityY += joystick.currentY * self.acceleration;
// Rotate car to face movement direction
if (joystick.currentX !== 0 || joystick.currentY !== 0) {
var angle = Math.atan2(joystick.currentY, joystick.currentX);
carGraphics.rotation = angle + Math.PI / 2;
}
}
// Apply friction
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Limit velocity
var speed = Math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY);
if (speed > self.maxSpeed) {
self.velocityX = self.velocityX / speed * self.maxSpeed;
self.velocityY = self.velocityY / speed * self.maxSpeed;
}
// Move player
self.x += self.velocityX;
self.y += self.velocityY;
// Keep player within bounds
self.x = Math.max(100, Math.min(1948, self.x));
self.y = Math.max(100, Math.min(2632, self.y));
};
return self;
});
var JoystickControl = Container.expand(function () {
var self = Container.call(this);
var joystickBase = self.attachAsset('joystickBase', {
anchorX: 0.5,
anchorY: 0.5
});
var joystickKnob = self.attachAsset('joystickKnob', {
anchorX: 0.5,
anchorY: 0.5
});
joystickBase.alpha = 0.3;
joystickKnob.alpha = 0.6;
self.isActive = false;
self.currentX = 0;
self.currentY = 0;
self.maxDistance = 80;
self.down = function (x, y, obj) {
self.isActive = true;
self.updateKnob(x, y);
};
self.move = function (x, y, obj) {
if (self.isActive) {
self.updateKnob(x, y);
}
};
self.up = function (x, y, obj) {
self.isActive = false;
joystickKnob.x = 0;
joystickKnob.y = 0;
self.currentX = 0;
self.currentY = 0;
};
self.updateKnob = function (x, y) {
var deltaX = x;
var deltaY = y;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > self.maxDistance) {
deltaX = deltaX / distance * self.maxDistance;
deltaY = deltaY / distance * self.maxDistance;
}
joystickKnob.x = deltaX;
joystickKnob.y = deltaY;
self.currentX = deltaX / self.maxDistance;
self.currentY = deltaY / self.maxDistance;
};
return self;
});
var PoliceCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('policeCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.targetCar = null;
self.lastCollisionCheck = false;
self.velocityX = 0;
self.velocityY = 0;
self.acceleration = 0.2;
self.friction = 0.95;
self.update = function () {
if (self.targetCar) {
var deltaX = self.targetCar.x - self.x;
var deltaY = self.targetCar.y - self.y;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > 5) {
// Apply acceleration towards target
self.velocityX += deltaX / distance * self.acceleration;
self.velocityY += deltaY / distance * self.acceleration;
// Rotate car to face movement direction
var angle = Math.atan2(deltaY, deltaX);
carGraphics.rotation = angle + Math.PI / 2;
}
}
// Apply friction
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Limit velocity
var speed = Math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY);
if (speed > self.speed) {
self.velocityX = self.velocityX / speed * self.speed;
self.velocityY = self.velocityY / speed * self.speed;
}
// Move police car
self.x += self.velocityX;
self.y += self.velocityY;
// Move with map scrolling
self.y += mapSpeed;
// Remove police cars that go off screen
if (self.y > 2732 + 100) {
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game variables
var player;
var policeCars = [];
var diamonds = [];
var explosions = [];
var gameSpeed = 1;
var milestone = 0;
var diamondsCollected = 0;
var policeKills = 0;
var mapSpeed = 3;
var mapOffsetY = 0;
var baseMapSpeed = 3;
// Background colors for different milestones
var backgroundColors = [0x2c3e50, 0x34495e, 0x1abc9c, 0x16a085, 0x27ae60, 0x2980b9];
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var diamondsTxt = new Text2('Diamonds: 0', {
size: 50,
fill: 0xF1C40F
});
diamondsTxt.anchor.set(0, 0);
diamondsTxt.x = 120;
diamondsTxt.y = 100;
LK.gui.topLeft.addChild(diamondsTxt);
var killsTxt = new Text2('Police Crashes: 0', {
size: 50,
fill: 0xE74C3C
});
killsTxt.anchor.set(1, 0);
killsTxt.x = -20;
killsTxt.y = 100;
LK.gui.topRight.addChild(killsTxt);
// Initialize city background
var cityBackground = game.addChild(new CityBackground());
// Initialize joystick control
var joystick = new JoystickControl();
joystick.x = 200;
joystick.y = 2532;
LK.gui.bottomLeft.addChild(joystick);
// Initialize player
player = game.addChild(new GetawayCar());
player.x = 1024; // Center horizontally
player.y = 1366; // Center vertically
// Spawn initial police cars
function spawnPoliceCar() {
var police = new PoliceCar();
// Spawn from random edge, favoring top and sides for moving map
var edge = Math.floor(Math.random() * 4);
switch (edge) {
case 0:
// Top (ahead of player)
police.x = Math.random() * 2048;
police.y = -60;
break;
case 1:
// Right
police.x = 2048 + 60;
police.y = Math.random() * 1000; // Spawn closer to top
break;
case 2:
// Bottom (behind player, will move forward)
police.x = Math.random() * 2048;
police.y = 2732 + 60;
break;
case 3:
// Left
police.x = -60;
police.y = Math.random() * 1000; // Spawn closer to top
break;
}
police.targetCar = player;
police.speed = 2 + milestone * 0.5;
policeCars.push(police);
game.addChild(police);
}
// Spawn initial diamonds
function spawnDiamond() {
var diamond = new Diamond();
diamond.x = 100 + Math.random() * 1848;
diamond.y = -100 - Math.random() * 500; // Spawn ahead of player
diamonds.push(diamond);
game.addChild(diamond);
}
// Initialize game objects
for (var i = 0; i < 3; i++) {
spawnPoliceCar();
}
for (var i = 0; i < 1; i++) {
spawnDiamond();
}
// Joystick controls are now handled by the JoystickControl class
// Update score display
function updateScore() {
var totalScore = diamondsCollected * 100 + policeKills * 500;
LK.setScore(totalScore);
scoreTxt.setText('Score: ' + totalScore);
diamondsTxt.setText('Diamonds: ' + diamondsCollected);
killsTxt.setText('Police Crashes: ' + policeKills);
}
// Check milestone progression
function checkMilestone() {
var newMilestone = Math.floor(LK.getScore() / 1000);
if (newMilestone > milestone) {
milestone = newMilestone;
// Change background color
if (milestone < backgroundColors.length) {
tween(game, {
backgroundColor: backgroundColors[milestone]
}, {
duration: 2000
});
}
// Spawn additional police car
spawnPoliceCar();
}
}
// Create explosion effect
function createExplosion(x, y) {
var explosion = new Explosion();
explosion.x = x;
explosion.y = y;
explosions.push(explosion);
game.addChild(explosion);
}
// Main game loop
game.update = function () {
// Check diamond collection
for (var i = diamonds.length - 1; i >= 0; i--) {
var diamond = diamonds[i];
if (!diamond.collected && player.intersects(diamond)) {
diamond.collected = true;
diamondsCollected++;
LK.getSound('collectDiamond').play();
diamond.destroy();
diamonds.splice(i, 1);
// Spawn new diamond
spawnDiamond();
}
}
// Check police car collisions with player
for (var i = 0; i < policeCars.length; i++) {
var police = policeCars[i];
if (player.intersects(police)) {
LK.getSound('gameOver').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
// Check police car collisions with each other
for (var i = policeCars.length - 1; i >= 0; i--) {
var police1 = policeCars[i];
var collided = false;
for (var j = i + 1; j < policeCars.length; j++) {
var police2 = policeCars[j];
if (police1.intersects(police2)) {
// Create explosion
var explosionX = (police1.x + police2.x) / 2;
var explosionY = (police1.y + police2.y) / 2;
createExplosion(explosionX, explosionY);
policeKills++;
LK.getSound('policeExplode').play();
// Increase map speed by 1%
mapSpeed = mapSpeed * 1.01;
// Remove both police cars
police1.destroy();
police2.destroy();
policeCars.splice(j, 1);
policeCars.splice(i, 1);
collided = true;
break;
}
}
if (collided) break;
}
// Clean up police cars that went off screen
for (var i = policeCars.length - 1; i >= 0; i--) {
var police = policeCars[i];
if (police.y > 2732 + 100) {
police.destroy();
policeCars.splice(i, 1);
}
}
// Clean up diamonds that went off screen
for (var i = diamonds.length - 1; i >= 0; i--) {
var diamond = diamonds[i];
if (diamond.y > 2732 + 100) {
diamond.destroy();
diamonds.splice(i, 1);
// Spawn new diamond
spawnDiamond();
}
}
// Clean up explosions
for (var i = explosions.length - 1; i >= 0; i--) {
var explosion = explosions[i];
if (explosion.age >= explosion.lifetime) {
explosions.splice(i, 1);
}
}
// Spawn new police cars periodically
if (LK.ticks % (300 - milestone * 30) === 0) {
spawnPoliceCar();
}
// Spawn new diamonds periodically
if (LK.ticks % 500 === 0) {
spawnDiamond();
}
// Update score and check milestones
updateScore();
checkMilestone();
// Win condition
if (LK.getScore() >= 10000) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -376,9 +376,9 @@
// Initialize game objects
for (var i = 0; i < 3; i++) {
spawnPoliceCar();
}
-for (var i = 0; i < 2; i++) {
+for (var i = 0; i < 1; i++) {
spawnDiamond();
}
// Joystick controls are now handled by the JoystickControl class
// Update score display
@@ -494,9 +494,9 @@
if (LK.ticks % (300 - milestone * 30) === 0) {
spawnPoliceCar();
}
// Spawn new diamonds periodically
- if (LK.ticks % 360 === 0) {
+ if (LK.ticks % 500 === 0) {
spawnDiamond();
}
// Update score and check milestones
updateScore();