/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Car class var Car = Container.expand(function () { var self = Container.call(this); // Attach car asset, anchor center var carSprite = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); // For collision, store width/height self.carWidth = carSprite.width; self.carHeight = carSprite.height; // No update needed; car is moved by player return self; }); // Crosswalk class (visual only) var Crosswalk = Container.expand(function () { var self = Container.call(this); // Attach crosswalk asset, anchor center self.attachAsset('crosswalk', { anchorX: 0.5, anchorY: 0.5 }); return self; }); // Pedestrian class var Pedestrian = Container.expand(function () { var self = Container.call(this); // Attach pedestrian asset, anchor center var pedSprite = self.attachAsset('pedestrian', { anchorX: 0.5, anchorY: 0.5 }); // For collision, store width/height self.pedWidth = pedSprite.width; self.pedHeight = pedSprite.height; // Speed in px per frame (set on spawn) self.speed = 0; // Update: move right self.update = function () { // If assigned to a crosswalk, always follow its Y position if (typeof self.crosswalkIdx === "number" && crosswalks && crosswalks[self.crosswalkIdx]) { self.y = crosswalks[self.crosswalkIdx].y; } self.x += self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x444444 }); /**** * Game Code ****/ // Game area: 2048x2732, road/crosswalks 1300px centered // Road setup // Car asset: blue box, 200x300 // Pedestrian asset: yellow ellipse, 120x120 // Road asset: dark gray box, 900x2732 // Crosswalk asset: white box, 900x40 var GAME_WIDTH = 2048; var GAME_HEIGHT = 2732; var ROAD_WIDTH = 1300; var ROAD_X = GAME_WIDTH / 2; // Center road horizontally var road = LK.getAsset('road', { anchorX: 0.5, anchorY: 0 }); road.x = ROAD_X; road.y = 0; game.addChild(road); // Crosswalk Y positions (4 crosswalks, now with slightly less spacing for still large but reduced gaps) var crosswalkSpacing = 2200; // Reduced from 3400 to 2200 for smaller gaps var crosswalkStartY = 600; var crosswalkCount = 4; var crosswalkYs = []; var crosswalks = []; for (var i = 0; i < crosswalkCount; i++) { var y = crosswalkStartY + i * crosswalkSpacing; crosswalkYs.push(y); var cw = new Crosswalk(); // Center crosswalk on road cw.x = ROAD_X; cw.y = y; game.addChild(cw); crosswalks.push(cw); } // Pedestrian management var pedestrians = []; // Car setup var car = new Car(); car.x = ROAD_X; // We'll keep car.y fixed at this value (bottom area) var carFixedY = 2732 - 400; car.y = carFixedY; game.addChild(car); // Car movement variables var dragCar = false; var dragOffsetX = 0; // Car forward movement speed (pixels per frame) var carForwardSpeed = 13; // Faster road/crosswalk scroll for more action // Road boundaries (car can't leave road) var roadLeft = ROAD_X - ROAD_WIDTH / 2 + car.carWidth / 2; var roadRight = ROAD_X + ROAD_WIDTH / 2 - car.carWidth / 2; // Pedestrian management var pedestrians = []; // Pedestrian spawn timer var pedSpawnInterval = 90; // frames (~1.5 sec, slower spawn for less crowding) var pedSpawnTick = 0; // Track which crosswalks have a pedestrian currently var crosswalkHasPedestrian = []; for (var i = 0; i < crosswalkCount; i++) crosswalkHasPedestrian[i] = false; // --- Add start button logic --- var worldActive = false; var carDistanceDriven = 0; var worldReallyActive = false; // (Background for the start button removed as requested) // Create start button var startBtn = new Text2("BAŞLAT", { size: 180, fill: 0xffffff, font: "'GillSans-Bold',Impact,'Arial Black',Tahoma" }); startBtn.anchor.set(0.5, 0.5); startBtn.x = GAME_WIDTH / 2; startBtn.y = GAME_HEIGHT / 2; startBtn.interactive = true; startBtn.buttonMode = true; game.addChild(startBtn); // Remove both startBtn and its background on start startBtn.down = function (x, y, obj) { if (startBtn.parent) startBtn.parent.removeChild(startBtn); if (startBtnBg.parent) startBtnBg.parent.removeChild(startBtnBg); // Show score scoreTxt.visible = true; // Start world after 3s delay LK.setTimeout(function () { worldActive = true; carDistanceDriven = 0; worldReallyActive = false; }, 3000); }; // Score var scoreTxt = new Text2('0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Hide gameplay UI until started scoreTxt.visible = false; LK.setScore(0); // Start button handler startBtn.down = function (x, y, obj) { // Remove button if (startBtn.parent) startBtn.parent.removeChild(startBtn); // Show score scoreTxt.visible = true; // Start world after 3s delay LK.setTimeout(function () { worldActive = true; carDistanceDriven = 0; worldReallyActive = false; }, 3000); }; // Score LK.setScore(0); // Score timer: add 10 points every second var scoreTimer = LK.setInterval(function () { LK.setScore(LK.getScore() + 10); scoreTxt.setText(LK.getScore()); }, 1000); // Touch/mouse controls: press-and-hold left/right to move car continuously var carMoveDirection = 0; // -1 for left, 1 for right, 0 for none game.down = function (x, y, obj) { // Set direction based on which half of the screen is pressed if (x < GAME_WIDTH / 2) { carMoveDirection = -1; } else { carMoveDirection = 1; } }; game.move = function (x, y, obj) { // If finger moves to other side, update direction if (carMoveDirection !== 0) { if (x < GAME_WIDTH / 2) { carMoveDirection = -1; } else { carMoveDirection = 1; } } }; game.up = function (x, y, obj) { // Stop moving when released carMoveDirection = 0; }; // Main update loop game.update = function () { // Update score text every frame to reflect current score scoreTxt.setText(LK.getScore()); // Car stays at fixed Y (carFixedY), world scrolls instead car.y = carFixedY; // Move car left/right if holding var carMoveSpeed = 22; // px per frame, adjust for feel if (typeof carMoveDirection !== "undefined" && carMoveDirection !== 0) { var newX = car.x + carMoveDirection * carMoveSpeed; if (newX < roadLeft) newX = roadLeft; if (newX > roadRight) newX = roadRight; car.x = newX; } // Only scroll world and spawn pedestrians if worldActive (after 3s delay) // --- NEW: Only activate world after car has moved a certain distance --- if (worldActive) { // Track how much the car has "driven" before world starts if (typeof carDistanceDriven === "undefined") { carDistanceDriven = 0; worldReallyActive = false; } // Only count distance if car is moving forward (not just left/right) // We'll count every frame after worldActive, since car is always moving forward if (!worldReallyActive) { carDistanceDriven += carForwardSpeed; // Don't move road/crosswalks until car has driven enough if (carDistanceDriven < 400) { // Don't move road or crosswalks at all until car has driven 400px return; } else { worldReallyActive = true; } } // === Road and crosswalks scroll logic === // The car stays at a fixed Y on screen, but the world scrolls down // We'll keep car.y fixed, and move road/crosswalks/pedestrians down by carForwardSpeed each frame // Amount to scroll everything (except car) down var scrollAmount = carForwardSpeed; // Move road, crosswalks, and pedestrians together with the car road.y += scrollAmount; for (var i = 0; i < crosswalks.length; i++) { crosswalks[i].y += scrollAmount; } for (var i = 0; i < pedestrians.length; i++) { pedestrians[i].y += scrollAmount; } // When a crosswalk goes off the bottom, move it to the top (endless effect) for (var i = 0; i < crosswalks.length; i++) { if (crosswalks[i].y > 2732 + 40) { crosswalks[i].y -= crosswalkCount * crosswalkSpacing; } } // When road goes off the bottom, move it up (since it's as tall as screen, this is rare, but keeps it endless) if (road.y > 0) { road.y = -road.height + 2732; } // Spawn pedestrians pedSpawnTick++; if (pedSpawnTick >= pedSpawnInterval) { pedSpawnTick = 0; // Find crosswalks without a pedestrian var availableCrosswalks = []; for (var i = 0; i < crosswalks.length; i++) { if (!crosswalkHasPedestrian[i]) availableCrosswalks.push(i); } if (availableCrosswalks.length > 0) { // Pick a random available crosswalk var idx = availableCrosswalks[Math.floor(Math.random() * availableCrosswalks.length)]; // Spawn pedestrian at the Y position of the chosen crosswalk, but just off the left edge var ped = new Pedestrian(); ped.crosswalkIdx = idx; if (crosswalks && crosswalks[idx]) { ped.y = crosswalks[idx].y; } else { ped.y = 0 - ped.pedHeight / 2; // fallback } ped.x = ROAD_X - ROAD_WIDTH / 2 + ped.pedWidth / 2; // Random speed: 2-3 px/frame (slower for easier dodging) ped.speed = 2 + Math.floor(Math.random() * 2); pedestrians.push(ped); game.addChild(ped); // Mark this crosswalk as having a pedestrian crosswalkHasPedestrian[idx] = true; } } } // Update pedestrians for (var i = pedestrians.length - 1; i >= 0; i--) { var ped = pedestrians[i]; ped.update(); // Remove if off right edge or off bottom of screen if (ped.x > road.x + road.width / 2 + ped.pedWidth / 2 || ped.y > 2732 + ped.pedHeight / 2) { // Free up crosswalk slot if (typeof ped.crosswalkIdx === "number") { crosswalkHasPedestrian[ped.crosswalkIdx] = false; } ped.destroy(); pedestrians.splice(i, 1); continue; } // Collision with car? if (ped.intersects(car)) { // Flash screen red LK.effects.flashScreen(0xff0000, 1000); // Game over LK.showGameOver(); return; } } }; // Center score text at top, avoid top left 100x100 scoreTxt.x = 1024; scoreTxt.y = 20; // Clean up on game over (handled by LK, but clear timer) game.onDestroy = function () { LK.clearInterval(scoreTimer); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Car class
var Car = Container.expand(function () {
var self = Container.call(this);
// Attach car asset, anchor center
var carSprite = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
// For collision, store width/height
self.carWidth = carSprite.width;
self.carHeight = carSprite.height;
// No update needed; car is moved by player
return self;
});
// Crosswalk class (visual only)
var Crosswalk = Container.expand(function () {
var self = Container.call(this);
// Attach crosswalk asset, anchor center
self.attachAsset('crosswalk', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
// Pedestrian class
var Pedestrian = Container.expand(function () {
var self = Container.call(this);
// Attach pedestrian asset, anchor center
var pedSprite = self.attachAsset('pedestrian', {
anchorX: 0.5,
anchorY: 0.5
});
// For collision, store width/height
self.pedWidth = pedSprite.width;
self.pedHeight = pedSprite.height;
// Speed in px per frame (set on spawn)
self.speed = 0;
// Update: move right
self.update = function () {
// If assigned to a crosswalk, always follow its Y position
if (typeof self.crosswalkIdx === "number" && crosswalks && crosswalks[self.crosswalkIdx]) {
self.y = crosswalks[self.crosswalkIdx].y;
}
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x444444
});
/****
* Game Code
****/
// Game area: 2048x2732, road/crosswalks 1300px centered
// Road setup
// Car asset: blue box, 200x300
// Pedestrian asset: yellow ellipse, 120x120
// Road asset: dark gray box, 900x2732
// Crosswalk asset: white box, 900x40
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var ROAD_WIDTH = 1300;
var ROAD_X = GAME_WIDTH / 2;
// Center road horizontally
var road = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0
});
road.x = ROAD_X;
road.y = 0;
game.addChild(road);
// Crosswalk Y positions (4 crosswalks, now with slightly less spacing for still large but reduced gaps)
var crosswalkSpacing = 2200; // Reduced from 3400 to 2200 for smaller gaps
var crosswalkStartY = 600;
var crosswalkCount = 4;
var crosswalkYs = [];
var crosswalks = [];
for (var i = 0; i < crosswalkCount; i++) {
var y = crosswalkStartY + i * crosswalkSpacing;
crosswalkYs.push(y);
var cw = new Crosswalk();
// Center crosswalk on road
cw.x = ROAD_X;
cw.y = y;
game.addChild(cw);
crosswalks.push(cw);
}
// Pedestrian management
var pedestrians = [];
// Car setup
var car = new Car();
car.x = ROAD_X;
// We'll keep car.y fixed at this value (bottom area)
var carFixedY = 2732 - 400;
car.y = carFixedY;
game.addChild(car);
// Car movement variables
var dragCar = false;
var dragOffsetX = 0;
// Car forward movement speed (pixels per frame)
var carForwardSpeed = 13; // Faster road/crosswalk scroll for more action
// Road boundaries (car can't leave road)
var roadLeft = ROAD_X - ROAD_WIDTH / 2 + car.carWidth / 2;
var roadRight = ROAD_X + ROAD_WIDTH / 2 - car.carWidth / 2;
// Pedestrian management
var pedestrians = [];
// Pedestrian spawn timer
var pedSpawnInterval = 90; // frames (~1.5 sec, slower spawn for less crowding)
var pedSpawnTick = 0;
// Track which crosswalks have a pedestrian currently
var crosswalkHasPedestrian = [];
for (var i = 0; i < crosswalkCount; i++) crosswalkHasPedestrian[i] = false;
// --- Add start button logic ---
var worldActive = false;
var carDistanceDriven = 0;
var worldReallyActive = false;
// (Background for the start button removed as requested)
// Create start button
var startBtn = new Text2("BAŞLAT", {
size: 180,
fill: 0xffffff,
font: "'GillSans-Bold',Impact,'Arial Black',Tahoma"
});
startBtn.anchor.set(0.5, 0.5);
startBtn.x = GAME_WIDTH / 2;
startBtn.y = GAME_HEIGHT / 2;
startBtn.interactive = true;
startBtn.buttonMode = true;
game.addChild(startBtn);
// Remove both startBtn and its background on start
startBtn.down = function (x, y, obj) {
if (startBtn.parent) startBtn.parent.removeChild(startBtn);
if (startBtnBg.parent) startBtnBg.parent.removeChild(startBtnBg);
// Show score
scoreTxt.visible = true;
// Start world after 3s delay
LK.setTimeout(function () {
worldActive = true;
carDistanceDriven = 0;
worldReallyActive = false;
}, 3000);
};
// Score
var scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Hide gameplay UI until started
scoreTxt.visible = false;
LK.setScore(0);
// Start button handler
startBtn.down = function (x, y, obj) {
// Remove button
if (startBtn.parent) startBtn.parent.removeChild(startBtn);
// Show score
scoreTxt.visible = true;
// Start world after 3s delay
LK.setTimeout(function () {
worldActive = true;
carDistanceDriven = 0;
worldReallyActive = false;
}, 3000);
};
// Score
LK.setScore(0);
// Score timer: add 10 points every second
var scoreTimer = LK.setInterval(function () {
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
}, 1000);
// Touch/mouse controls: press-and-hold left/right to move car continuously
var carMoveDirection = 0; // -1 for left, 1 for right, 0 for none
game.down = function (x, y, obj) {
// Set direction based on which half of the screen is pressed
if (x < GAME_WIDTH / 2) {
carMoveDirection = -1;
} else {
carMoveDirection = 1;
}
};
game.move = function (x, y, obj) {
// If finger moves to other side, update direction
if (carMoveDirection !== 0) {
if (x < GAME_WIDTH / 2) {
carMoveDirection = -1;
} else {
carMoveDirection = 1;
}
}
};
game.up = function (x, y, obj) {
// Stop moving when released
carMoveDirection = 0;
};
// Main update loop
game.update = function () {
// Update score text every frame to reflect current score
scoreTxt.setText(LK.getScore());
// Car stays at fixed Y (carFixedY), world scrolls instead
car.y = carFixedY;
// Move car left/right if holding
var carMoveSpeed = 22; // px per frame, adjust for feel
if (typeof carMoveDirection !== "undefined" && carMoveDirection !== 0) {
var newX = car.x + carMoveDirection * carMoveSpeed;
if (newX < roadLeft) newX = roadLeft;
if (newX > roadRight) newX = roadRight;
car.x = newX;
}
// Only scroll world and spawn pedestrians if worldActive (after 3s delay)
// --- NEW: Only activate world after car has moved a certain distance ---
if (worldActive) {
// Track how much the car has "driven" before world starts
if (typeof carDistanceDriven === "undefined") {
carDistanceDriven = 0;
worldReallyActive = false;
}
// Only count distance if car is moving forward (not just left/right)
// We'll count every frame after worldActive, since car is always moving forward
if (!worldReallyActive) {
carDistanceDriven += carForwardSpeed;
// Don't move road/crosswalks until car has driven enough
if (carDistanceDriven < 400) {
// Don't move road or crosswalks at all until car has driven 400px
return;
} else {
worldReallyActive = true;
}
}
// === Road and crosswalks scroll logic ===
// The car stays at a fixed Y on screen, but the world scrolls down
// We'll keep car.y fixed, and move road/crosswalks/pedestrians down by carForwardSpeed each frame
// Amount to scroll everything (except car) down
var scrollAmount = carForwardSpeed;
// Move road, crosswalks, and pedestrians together with the car
road.y += scrollAmount;
for (var i = 0; i < crosswalks.length; i++) {
crosswalks[i].y += scrollAmount;
}
for (var i = 0; i < pedestrians.length; i++) {
pedestrians[i].y += scrollAmount;
}
// When a crosswalk goes off the bottom, move it to the top (endless effect)
for (var i = 0; i < crosswalks.length; i++) {
if (crosswalks[i].y > 2732 + 40) {
crosswalks[i].y -= crosswalkCount * crosswalkSpacing;
}
}
// When road goes off the bottom, move it up (since it's as tall as screen, this is rare, but keeps it endless)
if (road.y > 0) {
road.y = -road.height + 2732;
}
// Spawn pedestrians
pedSpawnTick++;
if (pedSpawnTick >= pedSpawnInterval) {
pedSpawnTick = 0;
// Find crosswalks without a pedestrian
var availableCrosswalks = [];
for (var i = 0; i < crosswalks.length; i++) {
if (!crosswalkHasPedestrian[i]) availableCrosswalks.push(i);
}
if (availableCrosswalks.length > 0) {
// Pick a random available crosswalk
var idx = availableCrosswalks[Math.floor(Math.random() * availableCrosswalks.length)];
// Spawn pedestrian at the Y position of the chosen crosswalk, but just off the left edge
var ped = new Pedestrian();
ped.crosswalkIdx = idx;
if (crosswalks && crosswalks[idx]) {
ped.y = crosswalks[idx].y;
} else {
ped.y = 0 - ped.pedHeight / 2; // fallback
}
ped.x = ROAD_X - ROAD_WIDTH / 2 + ped.pedWidth / 2;
// Random speed: 2-3 px/frame (slower for easier dodging)
ped.speed = 2 + Math.floor(Math.random() * 2);
pedestrians.push(ped);
game.addChild(ped);
// Mark this crosswalk as having a pedestrian
crosswalkHasPedestrian[idx] = true;
}
}
}
// Update pedestrians
for (var i = pedestrians.length - 1; i >= 0; i--) {
var ped = pedestrians[i];
ped.update();
// Remove if off right edge or off bottom of screen
if (ped.x > road.x + road.width / 2 + ped.pedWidth / 2 || ped.y > 2732 + ped.pedHeight / 2) {
// Free up crosswalk slot
if (typeof ped.crosswalkIdx === "number") {
crosswalkHasPedestrian[ped.crosswalkIdx] = false;
}
ped.destroy();
pedestrians.splice(i, 1);
continue;
}
// Collision with car?
if (ped.intersects(car)) {
// Flash screen red
LK.effects.flashScreen(0xff0000, 1000);
// Game over
LK.showGameOver();
return;
}
}
};
// Center score text at top, avoid top left 100x100
scoreTxt.x = 1024;
scoreTxt.y = 20;
// Clean up on game over (handled by LK, but clear timer)
game.onDestroy = function () {
LK.clearInterval(scoreTimer);
};