User prompt
delete road line
User prompt
make a barrier animation ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
add a barrier animation
User prompt
make a road animatipn
User prompt
add a backround image
User prompt
use backround image road
User prompt
make it move in the barrier
User prompt
Let the barrier heat up with the player
User prompt
use barriyer image for road barrier
User prompt
add barriers outside the road
User prompt
increase traffic vehicles and delete the environment
User prompt
road bigger
User prompt
add environment to the game
User prompt
polices dont spawn players behind
User prompt
police speed slower
User prompt
police dont spawn in game starts
User prompt
police chasing player back
User prompt
add police chase
User prompt
make me a car chase game
User prompt
reload game code
Code edit (1 edits merged)
Please save this source code
User prompt
Highway Pursuit
Initial prompt
make me a car chase game
/**** * Classes ****/ // Player's car class var PlayerCar = Container.expand(function () { var self = Container.call(this); var car = self.attachAsset('playerCar', { anchorX: 0.5, anchorY: 0.5 }); self.width = car.width; self.height = car.height; self.lane = 1; // 0: left, 1: center, 2: right self.lastX = 0; self.lastY = 0; self.update = function () { self.lastX = self.x; self.lastY = self.y; }; return self; }); // Police car class var PoliceCar = Container.expand(function () { var self = Container.call(this); var car = self.attachAsset('policeCar', { anchorX: 0.5, anchorY: 0.5 }); self.width = car.width; self.height = car.height; self.speed = 18 + Math.random() * 8; self.lastY = 0; self.update = function () { self.lastY = self.y; self.y += self.speed; }; return self; }); // Power-up class var PowerUp = Container.expand(function () { var self = Container.call(this); var pu = self.attachAsset('powerup', { anchorX: 0.5, anchorY: 0.5 }); self.width = pu.width; self.height = pu.height; self.speed = 22; self.lastY = 0; self.update = function () { self.lastY = self.y; self.y += self.speed; }; return self; }); // Traffic car class var TrafficCar = Container.expand(function () { var self = Container.call(this); var car = self.attachAsset('trafficCar', { anchorX: 0.5, anchorY: 0.5 }); self.width = car.width; self.height = car.height; self.speed = 20 + Math.random() * 10; self.lastY = 0; self.update = function () { self.lastY = self.y; self.y += self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ ; // --- Car Chase Game Variables --- var playerCar; var policeCars = []; var trafficCars = []; var powerUps = []; var roadLines = []; var score = 0; var gameSpeed = 22; var laneCount = 3; var laneWidth = 410; var roadLeft = (2048 - laneCount * laneWidth) / 2 + laneWidth / 2; var roadRight = 2048 - roadLeft; var dragNode = null; var lastTouchX = 0; var scoreTxt; // --- Helper: Get lane X position --- function getLaneX(lane) { return roadLeft + lane * laneWidth; } // --- Setup Player Car --- playerCar = new PlayerCar(); playerCar.x = getLaneX(1); playerCar.y = 2732 - 400; game.addChild(playerCar); // --- Setup Score Text --- scoreTxt = new Text2('0', { size: 120, fill: "#fff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // --- Setup Road Lines --- for (var i = 0; i < 10; i++) { var line = LK.getAsset('roadLine', { anchorX: 0.5, anchorY: 0.5 }); line.x = 2048 / 2; line.y = i * 300 + 100; roadLines.push(line); game.addChild(line); } // --- Touch Controls for Player Car --- game.down = function (x, y, obj) { if (x > playerCar.x - playerCar.width / 2 && x < playerCar.x + playerCar.width / 2 && y > playerCar.y - playerCar.height / 2 && y < playerCar.y + playerCar.height / 2) { dragNode = playerCar; lastTouchX = x; } }; game.move = function (x, y, obj) { if (dragNode === playerCar) { var dx = x - lastTouchX; playerCar.x += dx; // Clamp to road if (playerCar.x < roadLeft) playerCar.x = roadLeft; if (playerCar.x > roadRight) playerCar.x = roadRight; lastTouchX = x; } }; game.up = function (x, y, obj) { dragNode = null; }; // --- Main Game Update Loop --- game.update = function () { // Move road lines for (var i = 0; i < roadLines.length; i++) { var line = roadLines[i]; line.y += gameSpeed; if (line.y > 2732 + 60) { line.y -= 300 * roadLines.length; } } // Spawn traffic cars if (LK.ticks % 45 === 0) { var lane = Math.floor(Math.random() * laneCount); var tcar = new TrafficCar(); tcar.x = getLaneX(lane); tcar.y = -200; trafficCars.push(tcar); game.addChild(tcar); } // Spawn police cars if (LK.ticks % 180 === 0) { var lane = Math.floor(Math.random() * laneCount); var pcar = new PoliceCar(); pcar.x = getLaneX(lane); pcar.y = -220; policeCars.push(pcar); game.addChild(pcar); } // Spawn power-ups if (LK.ticks % 300 === 0) { var lane = Math.floor(Math.random() * laneCount); var pu = new PowerUp(); pu.x = getLaneX(lane); pu.y = -100; powerUps.push(pu); game.addChild(pu); } // Update player car playerCar.update(); // Update and remove off-screen traffic cars for (var i = trafficCars.length - 1; i >= 0; i--) { var tcar = trafficCars[i]; tcar.update(); if (tcar.y > 2732 + 200) { tcar.destroy(); trafficCars.splice(i, 1); continue; } // Collision with player if (tcar.intersects(playerCar)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } // Update and remove off-screen police cars for (var i = policeCars.length - 1; i >= 0; i--) { var pcar = policeCars[i]; pcar.update(); if (pcar.y > 2732 + 220) { pcar.destroy(); policeCars.splice(i, 1); continue; } // Collision with player if (pcar.intersects(playerCar)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } // Update and remove off-screen power-ups for (var i = powerUps.length - 1; i >= 0; i--) { var pu = powerUps[i]; pu.update(); if (pu.y > 2732 + 100) { pu.destroy(); powerUps.splice(i, 1); continue; } // Collision with player if (pu.intersects(playerCar)) { score += 10; scoreTxt.setText(score); LK.effects.flashObject(playerCar, 0xffeb3b, 500); pu.destroy(); powerUps.splice(i, 1); continue; } } // Score increases with distance if (LK.ticks % 6 === 0) { score += 1; scoreTxt.setText(score); } // Gradually increase game speed if (LK.ticks % 180 === 0 && gameSpeed < 40) { gameSpeed += 1; } };
===================================================================
--- original.js
+++ change.js
@@ -1,13 +1,8 @@
/****
-* Plugins
-****/
-var tween = LK.import("@upit/tween.v1");
-
-/****
* Classes
****/
-// Player Car
+// Player's car class
var PlayerCar = Container.expand(function () {
var self = Container.call(this);
var car = self.attachAsset('playerCar', {
anchorX: 0.5,
@@ -15,87 +10,63 @@
});
self.width = car.width;
self.height = car.height;
self.lane = 1; // 0: left, 1: center, 2: right
- self.invincible = false;
- self.invincibleTimer = 0;
+ self.lastX = 0;
+ self.lastY = 0;
self.update = function () {
- // Invincibility timer
- if (self.invincible) {
- self.invincibleTimer--;
- if (self.invincibleTimer <= 0) {
- self.invincible = false;
- car.alpha = 1;
- }
- }
+ self.lastX = self.x;
+ self.lastY = self.y;
};
- // Flash effect for invincibility
- self.setInvincible = function (duration) {
- self.invincible = true;
- self.invincibleTimer = duration;
- car.alpha = 0.5;
- };
return self;
});
-// Police Car
+// Police car class
var PoliceCar = Container.expand(function () {
var self = Container.call(this);
var car = self.attachAsset('policeCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = car.width;
self.height = car.height;
- self.speed = 0;
- self.lane = 1;
+ self.speed = 18 + Math.random() * 8;
+ self.lastY = 0;
self.update = function () {
+ self.lastY = self.y;
self.y += self.speed;
};
return self;
});
-// Powerup
-var Powerup = Container.expand(function () {
+// Power-up class
+var PowerUp = Container.expand(function () {
var self = Container.call(this);
- var p = self.attachAsset('powerup', {
+ var pu = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
- self.width = p.width;
- self.height = p.height;
- self.speed = 0;
- self.type = 'invincible'; // Only one type for MVP
+ self.width = pu.width;
+ self.height = pu.height;
+ self.speed = 22;
+ self.lastY = 0;
self.update = function () {
+ self.lastY = self.y;
self.y += self.speed;
};
return self;
});
-// Road Line
-var RoadLine = Container.expand(function () {
- var self = Container.call(this);
- var line = self.attachAsset('roadLine', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.width = line.width;
- self.height = line.height;
- self.speed = 0;
- self.update = function () {
- self.y += self.speed;
- };
- return self;
-});
-// Traffic Car
+// Traffic car class
var TrafficCar = Container.expand(function () {
var self = Container.call(this);
var car = self.attachAsset('trafficCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = car.width;
self.height = car.height;
- self.speed = 0;
- self.lane = 1;
+ self.speed = 20 + Math.random() * 10;
+ self.lastY = 0;
self.update = function () {
+ self.lastY = self.y;
self.y += self.speed;
};
return self;
});
@@ -103,265 +74,173 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x222222
+ backgroundColor: 0x000000
});
/****
* Game Code
****/
-// Game constants
-// Car assets
-var ROAD_WIDTH = 1200;
-var ROAD_LEFT = (2048 - ROAD_WIDTH) / 2;
-var ROAD_RIGHT = ROAD_LEFT + ROAD_WIDTH;
-var LANE_COUNT = 3;
-var LANE_WIDTH = ROAD_WIDTH / LANE_COUNT;
-var PLAYER_Y = 2732 - 400;
-var SPAWN_TOP = -300;
-var ROAD_LINE_SPACING = 300;
-var ROAD_LINE_COUNT = Math.ceil(2732 / ROAD_LINE_SPACING) + 2;
-// Game state
-var player;
-var policeCars;
-var trafficCars;
-var powerups;
-var roadLines;
-var score;
-var distance;
-var lastLane;
-var gameSpeed;
-var policeSpawnTimer;
-var trafficSpawnTimer;
-var powerupSpawnTimer;
-var difficultyTimer;
-var dragStartX;
-var dragStartLane;
-var dragNode;
-// Reset function to initialize all game state variables and objects
-function resetGameState() {
- policeCars = [];
- trafficCars = [];
- powerups = [];
- roadLines = [];
- score = 0;
- distance = 0;
- lastLane = 1;
- gameSpeed = 24;
- policeSpawnTimer = 0;
- trafficSpawnTimer = 0;
- powerupSpawnTimer = 0;
- difficultyTimer = 0;
- dragStartX = null;
- dragStartLane = null;
- dragNode = null;
+;
+// --- Car Chase Game Variables ---
+var playerCar;
+var policeCars = [];
+var trafficCars = [];
+var powerUps = [];
+var roadLines = [];
+var score = 0;
+var gameSpeed = 22;
+var laneCount = 3;
+var laneWidth = 410;
+var roadLeft = (2048 - laneCount * laneWidth) / 2 + laneWidth / 2;
+var roadRight = 2048 - roadLeft;
+var dragNode = null;
+var lastTouchX = 0;
+var scoreTxt;
+// --- Helper: Get lane X position ---
+function getLaneX(lane) {
+ return roadLeft + lane * laneWidth;
}
-resetGameState();
-// Score display
-var scoreTxt = new Text2('0', {
+// --- Setup Player Car ---
+playerCar = new PlayerCar();
+playerCar.x = getLaneX(1);
+playerCar.y = 2732 - 400;
+game.addChild(playerCar);
+// --- Setup Score Text ---
+scoreTxt = new Text2('0', {
size: 120,
fill: "#fff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
-// Distance display
-var distTxt = new Text2('0 m', {
- size: 60,
- fill: "#fff"
-});
-distTxt.anchor.set(0.5, 0);
-LK.gui.top.addChild(distTxt);
-distTxt.y = 130;
-// Helper: get lane center X
-function laneToX(lane) {
- return ROAD_LEFT + LANE_WIDTH * (lane + 0.5);
-}
-// Helper: pick random lane
-function randomLane() {
- return Math.floor(Math.random() * LANE_COUNT);
-}
-// Helper: clamp lane
-function clampLane(lane) {
- if (lane < 0) return 0;
- if (lane >= LANE_COUNT) return LANE_COUNT - 1;
- return lane;
-}
-// Create road lines
-for (var i = 0; i < ROAD_LINE_COUNT; i++) {
- var line = new RoadLine();
+// --- Setup Road Lines ---
+for (var i = 0; i < 10; i++) {
+ var line = LK.getAsset('roadLine', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
line.x = 2048 / 2;
- line.y = i * ROAD_LINE_SPACING;
- line.speed = gameSpeed;
+ line.y = i * 300 + 100;
roadLines.push(line);
game.addChild(line);
}
-// Create player
-player = new PlayerCar();
-player.lane = 1;
-player.x = laneToX(player.lane);
-player.y = PLAYER_Y;
-game.addChild(player);
-// Touch controls: drag to steer
+// --- Touch Controls for Player Car ---
game.down = function (x, y, obj) {
- // Only start drag if touch is on lower half of screen
- if (y > 1366) {
- dragStartX = x;
- dragStartLane = player.lane;
- dragNode = player;
+ if (x > playerCar.x - playerCar.width / 2 && x < playerCar.x + playerCar.width / 2 && y > playerCar.y - playerCar.height / 2 && y < playerCar.y + playerCar.height / 2) {
+ dragNode = playerCar;
+ lastTouchX = x;
}
};
game.move = function (x, y, obj) {
- if (dragNode === player && dragStartX !== null) {
- var dx = x - dragStartX;
- var laneDelta = Math.round(dx / LANE_WIDTH);
- var newLane = clampLane(dragStartLane + laneDelta);
- if (newLane !== player.lane) {
- player.lane = newLane;
- // Animate car to new lane
- tween.stop(player, {
- x: true
- });
- tween(player, {
- x: laneToX(player.lane)
- }, {
- duration: 120,
- easing: tween.cubicOut
- });
- }
+ if (dragNode === playerCar) {
+ var dx = x - lastTouchX;
+ playerCar.x += dx;
+ // Clamp to road
+ if (playerCar.x < roadLeft) playerCar.x = roadLeft;
+ if (playerCar.x > roadRight) playerCar.x = roadRight;
+ lastTouchX = x;
}
};
game.up = function (x, y, obj) {
dragNode = null;
- dragStartX = null;
- dragStartLane = null;
};
-// Main update loop
+// --- Main Game Update Loop ---
game.update = function () {
- // Increase difficulty over time
- difficultyTimer++;
- if (difficultyTimer % 600 === 0 && gameSpeed < 40) {
- gameSpeed += 2;
- }
// Move road lines
for (var i = 0; i < roadLines.length; i++) {
var line = roadLines[i];
- line.speed = gameSpeed;
- line.update();
- if (line.y > 2732 + 100) {
- line.y -= ROAD_LINE_COUNT * ROAD_LINE_SPACING;
+ line.y += gameSpeed;
+ if (line.y > 2732 + 60) {
+ line.y -= 300 * roadLines.length;
}
}
- // Update player
- player.update();
- // Move police cars
- for (var i = policeCars.length - 1; i >= 0; i--) {
- var p = policeCars[i];
- p.speed = gameSpeed + 4;
- p.update();
- // Remove if off screen
- if (p.y > 2732 + 300) {
- p.destroy();
- policeCars.splice(i, 1);
+ // Spawn traffic cars
+ if (LK.ticks % 45 === 0) {
+ var lane = Math.floor(Math.random() * laneCount);
+ var tcar = new TrafficCar();
+ tcar.x = getLaneX(lane);
+ tcar.y = -200;
+ trafficCars.push(tcar);
+ game.addChild(tcar);
+ }
+ // Spawn police cars
+ if (LK.ticks % 180 === 0) {
+ var lane = Math.floor(Math.random() * laneCount);
+ var pcar = new PoliceCar();
+ pcar.x = getLaneX(lane);
+ pcar.y = -220;
+ policeCars.push(pcar);
+ game.addChild(pcar);
+ }
+ // Spawn power-ups
+ if (LK.ticks % 300 === 0) {
+ var lane = Math.floor(Math.random() * laneCount);
+ var pu = new PowerUp();
+ pu.x = getLaneX(lane);
+ pu.y = -100;
+ powerUps.push(pu);
+ game.addChild(pu);
+ }
+ // Update player car
+ playerCar.update();
+ // Update and remove off-screen traffic cars
+ for (var i = trafficCars.length - 1; i >= 0; i--) {
+ var tcar = trafficCars[i];
+ tcar.update();
+ if (tcar.y > 2732 + 200) {
+ tcar.destroy();
+ trafficCars.splice(i, 1);
continue;
}
// Collision with player
- if (p.intersects(player) && !player.invincible) {
- LK.effects.flashScreen(0xff0000, 800);
+ if (tcar.intersects(playerCar)) {
+ LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
- resetGameState();
return;
}
}
- // Move traffic cars
- for (var i = trafficCars.length - 1; i >= 0; i--) {
- var t = trafficCars[i];
- t.speed = gameSpeed;
- t.update();
- if (t.y > 2732 + 300) {
- t.destroy();
- trafficCars.splice(i, 1);
+ // Update and remove off-screen police cars
+ for (var i = policeCars.length - 1; i >= 0; i--) {
+ var pcar = policeCars[i];
+ pcar.update();
+ if (pcar.y > 2732 + 220) {
+ pcar.destroy();
+ policeCars.splice(i, 1);
continue;
}
// Collision with player
- if (t.intersects(player) && !player.invincible) {
- LK.effects.flashScreen(0xff0000, 800);
+ if (pcar.intersects(playerCar)) {
+ LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
- resetGameState();
return;
}
}
- // Move powerups
- for (var i = powerups.length - 1; i >= 0; i--) {
- var pu = powerups[i];
- pu.speed = gameSpeed;
+ // Update and remove off-screen power-ups
+ for (var i = powerUps.length - 1; i >= 0; i--) {
+ var pu = powerUps[i];
pu.update();
- if (pu.y > 2732 + 200) {
+ if (pu.y > 2732 + 100) {
pu.destroy();
- powerups.splice(i, 1);
+ powerUps.splice(i, 1);
continue;
}
- // Pickup
- if (pu.intersects(player)) {
- if (pu.type === 'invincible') {
- player.setInvincible(180); // 3 seconds at 60fps
- }
- score += 100;
- LK.setScore(score);
+ // Collision with player
+ if (pu.intersects(playerCar)) {
+ score += 10;
scoreTxt.setText(score);
+ LK.effects.flashObject(playerCar, 0xffeb3b, 500);
pu.destroy();
- powerups.splice(i, 1);
+ powerUps.splice(i, 1);
continue;
}
}
- // Spawn police car
- policeSpawnTimer--;
- if (policeSpawnTimer <= 0) {
- var lane = randomLane();
- // Don't spawn in same lane as last police
- if (policeCars.length > 0 && policeCars[policeCars.length - 1].lane === lane) {
- lane = clampLane((lane + 1) % LANE_COUNT);
- }
- var p = new PoliceCar();
- p.lane = lane;
- p.x = laneToX(lane);
- p.y = SPAWN_TOP;
- policeCars.push(p);
- game.addChild(p);
- policeSpawnTimer = Math.max(90, 240 - Math.floor(distance / 1000) * 10);
+ // Score increases with distance
+ if (LK.ticks % 6 === 0) {
+ score += 1;
+ scoreTxt.setText(score);
}
- // Spawn traffic car
- trafficSpawnTimer--;
- if (trafficSpawnTimer <= 0) {
- var lane = randomLane();
- // Avoid spawning on player
- if (lane === player.lane) {
- lane = clampLane((lane + 1) % LANE_COUNT);
- }
- var t = new TrafficCar();
- t.lane = lane;
- t.x = laneToX(lane);
- t.y = SPAWN_TOP - 200;
- trafficCars.push(t);
- game.addChild(t);
- trafficSpawnTimer = Math.max(40, 120 - Math.floor(distance / 1000) * 5);
+ // Gradually increase game speed
+ if (LK.ticks % 180 === 0 && gameSpeed < 40) {
+ gameSpeed += 1;
}
- // Spawn powerup
- powerupSpawnTimer--;
- if (powerupSpawnTimer <= 0) {
- var lane = randomLane();
- var pu = new Powerup();
- pu.x = laneToX(lane);
- pu.y = SPAWN_TOP - 400;
- powerups.push(pu);
- game.addChild(pu);
- powerupSpawnTimer = 600 + Math.floor(Math.random() * 600);
- }
- // Update score and distance
- distance += gameSpeed;
- distTxt.setText(Math.floor(distance / 10) + " m");
- var newScore = Math.floor(distance / 10) + score;
- if (newScore !== LK.getScore()) {
- LK.setScore(newScore);
- scoreTxt.setText(LK.getScore());
- }
};
\ No newline at end of file
car top view. In-Game asset. 2d. High contrast. No shadows
police car top view. In-Game asset. 2d. High contrast. No shadows
add highway barrier top view. In-Game asset. 2d. High contrast. No shadows
power coin. In-Game asset. 2d. High contrast. No shadows. power coin
a highway road but top view. In-Game asset. 2d. High contrast. No shadows