/**** * Classes ****/ // Bus class var Bus = Container.expand(function () { var self = Container.call(this); var busGraphics = self.attachAsset('bus', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Assets will be automatically created and loaded during gameplay // Car class var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Car2 class var Car2 = Container.expand(function () { var self = Container.call(this); var car2Graphics = self.attachAsset('car2', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Down Move Button class var DownMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.y += 200; }; }); // Left Move Button class var LeftMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.moveLeft(); }; }); // Life class var Life = Container.expand(function () { var self = Container.call(this); var lifeGraphics = self.attachAsset('life', { anchorX: 0.5, anchorY: 0.5 }); }); // Pause/Play Button class var PausePlayButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.isPaused = false; self.update = function () { if (self.isPaused) { buttonGraphics.tint = 0x00ff00; // Green for play } else { buttonGraphics.tint = 0xff0000; // Red for pause } }; self.down = function () { self.isPaused = !self.isPaused; LK.setPaused(self.isPaused); }; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.lane = 2; self.lives = 3; self.moveLeft = function () { if (self.lane > 1) { self.lane--; self.x = self.lane * 512 - 256; } }; self.moveRight = function () { if (self.lane < 4) { self.lane++; self.x = self.lane * 512 - 256; } }; self.loseLife = function () { self.lives--; if (lives[self.lives]) { lives[self.lives].destroy(); // Remove one life image lives.splice(self.lives, 1); } if (self.lives <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }; }); // Right Move Button class var RightMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.moveRight(); }; }); // Up Move Button class var UpMoveButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('moveButton', { anchorX: 0.5, anchorY: 0.5 }); self.down = function () { player.y -= 200; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Set lines as a background image var background = game.attachAsset('lines', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); var player = game.addChild(new Player()); var pausePlayButton = game.addChild(new PausePlayButton()); pausePlayButton.x = 2048 - 100; // Position the button at the top-right corner pausePlayButton.y = 100; var leftMoveButton = game.addChild(new LeftMoveButton()); leftMoveButton.x = 100; // Position the button at the bottom-left corner leftMoveButton.y = 2732 - 100; var upMoveButton = game.addChild(new UpMoveButton()); upMoveButton.x = 2048 / 2; // Position the button at the middle-top of the screen upMoveButton.y = 100; var downMoveButton = game.addChild(new DownMoveButton()); downMoveButton.x = 2048 / 2; // Position the button at the middle-bottom of the screen downMoveButton.y = 2732 - 100; var rightMoveButton = game.addChild(new RightMoveButton()); rightMoveButton.x = 2048 - 100; // Position the button at the bottom-right corner rightMoveButton.y = 2732 - 100; player.x = 2 * 512 - 256; // Set player's initial x position to the third lane player.y = 2400 - 200; var cars = []; var buses = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var lives = []; for (var i = 0; i < 3; i++) { var life = game.addChild(new Life()); life.x = (i + 1) * 60; // Position the life images at the top-left corner life.y = 60; lives.push(life); } function spawnVehicle() { var lane = Math.floor(Math.random() * 4) + 1; var vehicle; if (Math.random() < 0.33) { vehicle = new Car(); cars.push(vehicle); } else if (Math.random() < 0.66) { vehicle = new Car2(); cars.push(vehicle); } else { vehicle = new Bus(); buses.push(vehicle); } vehicle.x = lane * 512 - 256; vehicle.y = -100; game.addChild(vehicle); } // Removed game's down event handler game.update = function () { for (var i = cars.length - 1; i >= 0; i--) { if (cars[i].intersects(player)) { player.loseLife(); cars[i].destroy(); cars.splice(i, 1); continue; } for (var j = i - 1; j >= 0; j--) { if (cars[i].intersects(cars[j])) { cars[j].speed = cars[i].speed; } } for (var j = buses.length - 1; j >= 0; j--) { if (cars[i].intersects(buses[j])) { cars[i].speed = buses[j].speed; } } // Despawn the car or car2 if it is off-screen if (cars[i].y > 2732) { cars[i].destroy(); cars.splice(i, 1); continue; } cars[i].update(); } for (var j = buses.length - 1; j >= 0; j--) { if (buses[j].intersects(player)) { player.loseLife(); buses[j].destroy(); buses.splice(j, 1); continue; } // Despawn the bus if it is off-screen if (buses[j].y > 2732) { buses[j].destroy(); buses.splice(j, 1); continue; } buses[j].update(); } if (LK.ticks % Math.max(10, 60 - Math.floor(score / 10)) == 0) { spawnVehicle(); score++; scoreTxt.setText(score); } };
/****
* Classes
****/
// Bus class
var Bus = Container.expand(function () {
var self = Container.call(this);
var busGraphics = self.attachAsset('bus', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Assets will be automatically created and loaded during gameplay
// Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Car2 class
var Car2 = Container.expand(function () {
var self = Container.call(this);
var car2Graphics = self.attachAsset('car2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Down Move Button class
var DownMoveButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('moveButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function () {
player.y += 200;
};
});
// Left Move Button class
var LeftMoveButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('moveButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function () {
player.moveLeft();
};
});
// Life class
var Life = Container.expand(function () {
var self = Container.call(this);
var lifeGraphics = self.attachAsset('life', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Pause/Play Button class
var PausePlayButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('moveButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.isPaused = false;
self.update = function () {
if (self.isPaused) {
buttonGraphics.tint = 0x00ff00; // Green for play
} else {
buttonGraphics.tint = 0xff0000; // Red for pause
}
};
self.down = function () {
self.isPaused = !self.isPaused;
LK.setPaused(self.isPaused);
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.lane = 2;
self.lives = 3;
self.moveLeft = function () {
if (self.lane > 1) {
self.lane--;
self.x = self.lane * 512 - 256;
}
};
self.moveRight = function () {
if (self.lane < 4) {
self.lane++;
self.x = self.lane * 512 - 256;
}
};
self.loseLife = function () {
self.lives--;
if (lives[self.lives]) {
lives[self.lives].destroy(); // Remove one life image
lives.splice(self.lives, 1);
}
if (self.lives <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
});
// Right Move Button class
var RightMoveButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('moveButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function () {
player.moveRight();
};
});
// Up Move Button class
var UpMoveButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('moveButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function () {
player.y -= 200;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Set lines as a background image
var background = game.attachAsset('lines', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
var player = game.addChild(new Player());
var pausePlayButton = game.addChild(new PausePlayButton());
pausePlayButton.x = 2048 - 100; // Position the button at the top-right corner
pausePlayButton.y = 100;
var leftMoveButton = game.addChild(new LeftMoveButton());
leftMoveButton.x = 100; // Position the button at the bottom-left corner
leftMoveButton.y = 2732 - 100;
var upMoveButton = game.addChild(new UpMoveButton());
upMoveButton.x = 2048 / 2; // Position the button at the middle-top of the screen
upMoveButton.y = 100;
var downMoveButton = game.addChild(new DownMoveButton());
downMoveButton.x = 2048 / 2; // Position the button at the middle-bottom of the screen
downMoveButton.y = 2732 - 100;
var rightMoveButton = game.addChild(new RightMoveButton());
rightMoveButton.x = 2048 - 100; // Position the button at the bottom-right corner
rightMoveButton.y = 2732 - 100;
player.x = 2 * 512 - 256; // Set player's initial x position to the third lane
player.y = 2400 - 200;
var cars = [];
var buses = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var lives = [];
for (var i = 0; i < 3; i++) {
var life = game.addChild(new Life());
life.x = (i + 1) * 60; // Position the life images at the top-left corner
life.y = 60;
lives.push(life);
}
function spawnVehicle() {
var lane = Math.floor(Math.random() * 4) + 1;
var vehicle;
if (Math.random() < 0.33) {
vehicle = new Car();
cars.push(vehicle);
} else if (Math.random() < 0.66) {
vehicle = new Car2();
cars.push(vehicle);
} else {
vehicle = new Bus();
buses.push(vehicle);
}
vehicle.x = lane * 512 - 256;
vehicle.y = -100;
game.addChild(vehicle);
}
// Removed game's down event handler
game.update = function () {
for (var i = cars.length - 1; i >= 0; i--) {
if (cars[i].intersects(player)) {
player.loseLife();
cars[i].destroy();
cars.splice(i, 1);
continue;
}
for (var j = i - 1; j >= 0; j--) {
if (cars[i].intersects(cars[j])) {
cars[j].speed = cars[i].speed;
}
}
for (var j = buses.length - 1; j >= 0; j--) {
if (cars[i].intersects(buses[j])) {
cars[i].speed = buses[j].speed;
}
}
// Despawn the car or car2 if it is off-screen
if (cars[i].y > 2732) {
cars[i].destroy();
cars.splice(i, 1);
continue;
}
cars[i].update();
}
for (var j = buses.length - 1; j >= 0; j--) {
if (buses[j].intersects(player)) {
player.loseLife();
buses[j].destroy();
buses.splice(j, 1);
continue;
}
// Despawn the bus if it is off-screen
if (buses[j].y > 2732) {
buses[j].destroy();
buses.splice(j, 1);
continue;
}
buses[j].update();
}
if (LK.ticks % Math.max(10, 60 - Math.floor(score / 10)) == 0) {
spawnVehicle();
score++;
scoreTxt.setText(score);
}
};
delete white line
four-lane road on full image. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
left turn signal of the car. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
round car steering wheel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.