User prompt
put more spaces to station spawns and put take travellers button when we next to the station
User prompt
make street smaller. fix stations spawn on street. Put up and down overside of the street and same line all stations
User prompt
make background and station not same assets
User prompt
fix street is not unlimited
User prompt
make always bus on middle of the street. The screen follow the bus
User prompt
fix bus can go to overscreen
User prompt
when we hold on the screen bus go
User prompt
when we tap right of the screen bus move. not automatic
User prompt
draw a street background
Code edit (1 edits merged)
Please save this source code
User prompt
Rightward Road: Bus Stop Shuffle
Initial prompt
draw a street and we only can go right, put bus station random places and random 1-10 amount random traveller and make travellers leave random bus stations.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Bus (player) class var Bus = Container.expand(function () { var self = Container.call(this); var busAsset = self.attachAsset('bus', { anchorX: 0.5, anchorY: 1 }); // For touch drag self.dragging = false; self.dragOffsetY = 0; // Called every tick self.update = function () { // Clamp bus to road (vertical bounds) if (self.y < 400) self.y = 400; if (self.y > 2200) self.y = 2200; }; // Touch down on bus self.down = function (x, y, obj) { self.dragging = true; self.dragOffsetY = y - self.y; }; // Touch up on bus self.up = function (x, y, obj) { self.dragging = false; }; return self; }); // Bus Station class var BusStation = Container.expand(function () { var self = Container.call(this); var stationAsset = self.attachAsset('station', { anchorX: 0.5, anchorY: 1 }); // List of travelers at this station self.travelers = []; // Generate random number of travelers (1-10) var numTravelers = 1 + Math.floor(Math.random() * 10); // Place travelers in a row directly above (for above street) or below (for below street) the station var travelerYOffset = self.y < streetBgY ? -70 : 70; for (var i = 0; i < numTravelers; i++) { var traveler = new Traveler(); traveler.station = self; // Random leave time: 2-6 seconds from now traveler.leaveTick = LK.ticks + 120 + Math.floor(Math.random() * 240); // Position travelers spaced out in a straight line traveler.x = (i - (numTravelers - 1) / 2) * 70; traveler.y = travelerYOffset; self.addChild(traveler); self.travelers.push(traveler); } // Remove traveler from this station self.removeTraveler = function (traveler) { for (var i = 0; i < self.travelers.length; i++) { if (self.travelers[i] === traveler) { self.travelers.splice(i, 1); break; } } }; // Called every tick self.update = function () { // Remove station if no travelers left and bus has passed if (self.travelers.length === 0 && self.x + self.width / 2 < bus.x - 200) { self.destroy(); for (var i = 0; i < stations.length; i++) { if (stations[i] === self) { stations.splice(i, 1); break; } } } }; return self; }); // Traveler class var Traveler = Container.expand(function () { var self = Container.call(this); var travelerAsset = self.attachAsset('traveler', { anchorX: 0.5, anchorY: 1 }); // Reference to the station this traveler belongs to self.station = null; // Whether this traveler is still waiting at the station self.waiting = true; // Time (in ticks) until this traveler leaves self.leaveTick = 0; // Called every tick self.update = function () { // If not waiting, do nothing if (!self.waiting) return; // If it's time to leave, remove from station if (LK.ticks >= self.leaveTick) { self.waiting = false; if (self.station) { self.station.removeTraveler(self); } // Animate leaving (fade out and move up) tween(self, { y: self.y - 100, alpha: 0 }, { duration: 500, easing: tween.easeIn, onFinish: function onFinish() { self.destroy(); } }); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x222222 }); /**** * Game Code ****/ // Draw street background as a wide gray rectangle (repeatable) var streetBgWidth = 900; var streetBgHeight = 400; var streetBgY = 1500; var streetBgs = []; for (var i = 0; i < 3; i++) { var streetBg = LK.getAsset('streetBg', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 + (i - 1) * streetBgWidth, y: streetBgY }); game.addChild(streetBg); streetBgs.push(streetBg); } // Traveler // Bus station // Bus (player vehicle) // Game world scroll variables var scrollX = 0; // How far the world has scrolled right var scrollSpeed = 10; // Pixels per tick // Bus (player) var bus = new Bus(); game.addChild(bus); bus.x = 400; bus.y = 1500; // List of stations in the world var stations = []; // GUI: Score (number of travelers picked up) var score = 0; var scoreTxt = new Text2('0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // For dragging bus var dragBus = false; // Generate the first station function spawnStation(minX) { // Alternate station placement: above or below the street, always aligned horizontally var station = new BusStation(); var streetCenterY = streetBgY; var offsetY = 260; // distance from street center to place stations // Alternate up/down for each station if (stations.length % 2 === 0) { station.y = streetCenterY - streetBgHeight / 2 - 40; // above street } else { station.y = streetCenterY + streetBgHeight / 2 + 40; // below street } // Place station at minX + random offset (400-800 px ahead) station.x = minX + 400 + Math.floor(Math.random() * 400); game.addChild(station); stations.push(station); } // Initial stations var lastStationX = 1200; for (var i = 0; i < 3; i++) { spawnStation(lastStationX); lastStationX = stations[stations.length - 1].x; } // Move handler for dragging bus function handleMove(x, y, obj) { // Only drag if started on bus if (bus.dragging) { bus.y = y - bus.dragOffsetY; } } game.move = handleMove; // Down handler: start drag if on bus, or start holding for rightward movement var holdingRight = false; game.down = function (x, y, obj) { // Convert to bus local coordinates var local = bus.toLocal(game.toGlobal({ x: x, y: y })); // If within bus bounds, start drag if (local.x > -bus.width / 2 && local.x < bus.width / 2 && local.y > -bus.height && local.y < 0) { bus.dragging = true; bus.dragOffsetY = y - bus.y; } else { // Start holding for rightward movement holdingRight = true; } }; // Up handler: stop drag and stop holding rightward movement game.up = function (x, y, obj) { bus.dragging = false; holdingRight = false; }; // Main game update loop game.update = function () { // Move bus/world right if holding if (typeof holdingRight !== "undefined" && holdingRight) { var moveAmount = scrollSpeed; // Instead of moving the bus, move the world and keep bus centered scrollX += moveAmount; for (var i = 0; i < stations.length; i++) { stations[i].x -= moveAmount; } // Move and repeat street backgrounds for (var i = 0; i < streetBgs.length; i++) { streetBgs[i].x -= moveAmount; // If streetBg is fully off the left, move it to the right end if (streetBgs[i].x < -streetBgWidth / 2) { // Find the rightmost streetBg var maxX = streetBgs[0].x; for (var j = 1; j < streetBgs.length; j++) { if (streetBgs[j].x > maxX) maxX = streetBgs[j].x; } streetBgs[i].x = maxX + streetBgWidth; } } } // Always keep bus in the center of the screen horizontally bus.x = 2048 / 2; // Move all stations and their travelers (no auto scroll) for (var i = 0; i < stations.length; i++) { var station = stations[i]; // No station.x -= scrollSpeed; station.update(); for (var j = 0; j < station.travelers.length; j++) { station.travelers[j].update(); } } // Update bus bus.update(); // Spawn new station if needed (always keep at least 3 ahead) if (stations.length === 0 || stations[stations.length - 1].x < 2048 + 600) { var minX = stations.length === 0 ? bus.x + 800 : stations[stations.length - 1].x; spawnStation(minX); } // Check for bus-traveler interaction (pickup) for (var i = 0; i < stations.length; i++) { var station = stations[i]; // Only check if station is close to bus if (Math.abs(station.x - bus.x) < 180) { for (var j = station.travelers.length - 1; j >= 0; j--) { var traveler = station.travelers[j]; // Only pick up if traveler is waiting and close vertically if (traveler.waiting && Math.abs(traveler.y + station.y - bus.y) < 80) { // Pick up traveler traveler.waiting = false; station.removeTraveler(traveler); // Animate pickup (move to bus, fade out) var globalTraveler = { x: station.x + traveler.x, y: station.y + traveler.y }; tween(traveler, { x: bus.x - station.x, y: bus.y - station.y - 40, alpha: 0 }, { duration: 400, easing: tween.easeIn, onFinish: function onFinish() { traveler.destroy(); } }); // Update score score += 1; scoreTxt.setText(score); } } } } };
===================================================================
--- original.js
+++ change.js
@@ -43,17 +43,18 @@
// List of travelers at this station
self.travelers = [];
// Generate random number of travelers (1-10)
var numTravelers = 1 + Math.floor(Math.random() * 10);
- // Place travelers in a row above the station
+ // Place travelers in a row directly above (for above street) or below (for below street) the station
+ var travelerYOffset = self.y < streetBgY ? -70 : 70;
for (var i = 0; i < numTravelers; i++) {
var traveler = new Traveler();
traveler.station = self;
// Random leave time: 2-6 seconds from now
traveler.leaveTick = LK.ticks + 120 + Math.floor(Math.random() * 240);
- // Position travelers spaced out
+ // Position travelers spaced out in a straight line
traveler.x = (i - (numTravelers - 1) / 2) * 70;
- traveler.y = -10;
+ traveler.y = travelerYOffset;
self.addChild(traveler);
self.travelers.push(traveler);
}
// Remove traveler from this station
@@ -129,17 +130,17 @@
/****
* Game Code
****/
// Draw street background as a wide gray rectangle (repeatable)
-var streetBgWidth = 2048;
-var streetBgHeight = 900;
+var streetBgWidth = 900;
+var streetBgHeight = 400;
var streetBgY = 1500;
var streetBgs = [];
for (var i = 0; i < 3; i++) {
var streetBg = LK.getAsset('streetBg', {
anchorX: 0.5,
anchorY: 0.5,
- x: streetBgWidth / 2 + i * streetBgWidth,
+ x: 2048 / 2 + (i - 1) * streetBgWidth,
y: streetBgY
});
game.addChild(streetBg);
streetBgs.push(streetBg);
@@ -168,14 +169,20 @@
// For dragging bus
var dragBus = false;
// Generate the first station
function spawnStation(minX) {
- // Place station at a random y along the "road"
- var y = 700 + Math.floor(Math.random() * 1200);
+ // Alternate station placement: above or below the street, always aligned horizontally
var station = new BusStation();
+ var streetCenterY = streetBgY;
+ var offsetY = 260; // distance from street center to place stations
+ // Alternate up/down for each station
+ if (stations.length % 2 === 0) {
+ station.y = streetCenterY - streetBgHeight / 2 - 40; // above street
+ } else {
+ station.y = streetCenterY + streetBgHeight / 2 + 40; // below street
+ }
// Place station at minX + random offset (400-800 px ahead)
station.x = minX + 400 + Math.floor(Math.random() * 400);
- station.y = y;
game.addChild(station);
stations.push(station);
}
// Initial stations