/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.normalSpeed = 8; self.turboSpeed = 16; self.targetX = self.x; self.isTurbo = false; self.turboEndTime = 0; self.update = function () { // Check if turbo should end if (self.isTurbo && LK.ticks > self.turboEndTime) { self.isTurbo = false; self.speed = self.normalSpeed; } // Smooth movement towards target position var diff = self.targetX - self.x; var moveSpeed = self.isTurbo ? 0.25 : 0.15; // Faster movement during turbo if (Math.abs(diff) > 1) { self.x += diff * moveSpeed; } // Keep car within screen bounds var halfWidth = carGraphics.width / 2; if (self.x < halfWidth) { self.x = halfWidth; self.targetX = halfWidth; } if (self.x > 2048 - halfWidth) { self.x = 2048 - halfWidth; self.targetX = 2048 - halfWidth; } }; self.moveLeft = function () { self.targetX -= 150; }; self.moveRight = function () { self.targetX += 150; }; self.activateTurbo = function () { if (!self.isTurbo) { self.isTurbo = true; self.speed = self.turboSpeed; self.turboEndTime = LK.ticks + 180; // 3 seconds at 60fps LK.getSound('turbo').play(); // Visual effect tween(carGraphics, { tint: 0x00ff00 }, { duration: 100 }); // Reset color after turbo LK.setTimeout(function () { tween(carGraphics, { tint: 0xffffff }, { duration: 200 }); }, 3000); } }; return self; }); var Leaderboard = Container.expand(function () { var self = Container.call(this); // Background var background = LK.getAsset('speechBubble', { width: 600, height: 800, color: 0x000000, shape: 'box', anchorX: 0.5, anchorY: 0.5 }); background.alpha = 0.8; self.addChild(background); // Title var title = new Text2('LİDERLİK TABLOSU', { size: 50, fill: 0xFFFFFF }); title.anchor.set(0.5, 0.5); title.x = 0; title.y = -350; self.addChild(title); // Get leaderboard data var scores = storage.leaderboard || []; // Display top 10 scores for (var i = 0; i < Math.min(10, scores.length); i++) { var scoreText = new Text2(i + 1 + '. ' + scores[i] + ' puan', { size: 40, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0.5); scoreText.x = 0; scoreText.y = -250 + i * 60; self.addChild(scoreText); } // Close button text var closeText = new Text2('Devam etmek için dokunun', { size: 35, fill: 0x00FF00 }); closeText.anchor.set(0.5, 0.5); closeText.x = 0; closeText.y = 350; self.addChild(closeText); // Make clickable to close self.down = function (x, y, obj) { self.destroy(); }; return self; }); var Rock = Container.expand(function () { var self = Container.call(this); var rockGraphics = self.attachAsset('rock', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4 + Math.random() * 3; self.update = function () { self.y += self.speed; }; return self; }); var SpeechBubble = Container.expand(function (message) { var self = Container.call(this); // Create bubble background var bubble = LK.getAsset('speechBubble', { width: 400, height: 120, color: 0xffffff, shape: 'box', anchorX: 0.5, anchorY: 0.5 }); self.addChild(bubble); // Create text var text = new Text2(message, { size: 40, fill: 0x000000 }); text.anchor.set(0.5, 0.5); self.addChild(text); // Animation self.alpha = 0; self.scaleX = 0.5; self.scaleY = 0.5; // Animate in tween(self, { alpha: 1, scaleX: 1, scaleY: 1 }, { duration: 300, easing: tween.easeOut }); // Auto remove after delay LK.setTimeout(function () { tween(self, { alpha: 0, scaleX: 0.8, scaleY: 0.8 }, { duration: 200, onFinish: function onFinish() { if (self.parent) { self.destroy(); } } }); }, 2000); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game variables var car; var rocks = []; var gameRunning = true; var rockSpawnTimer = 0; var rockSpawnRate = 60; // frames between spawns var pointsTimer = 0; var lastEncouragementScore = 0; var currentLevel = 1; var maxLevel = 10; var scoreToWin = 1000; // Leaderboard functions function saveScore(score) { var leaderboard = storage.leaderboard || []; leaderboard.push(score); leaderboard.sort(function (a, b) { return b - a; }); // Sort descending leaderboard = leaderboard.slice(0, 10); // Keep only top 10 storage.leaderboard = leaderboard; } function showLeaderboard() { var leaderboard = new Leaderboard(); leaderboard.x = 2048 / 2; leaderboard.y = 2732 / 2; game.addChild(leaderboard); } // Initialize car car = game.addChild(new Car()); car.x = 2048 / 2; car.y = 2732 - 150; // Score display var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); scoreTxt.y = 100; // Level display var levelTxt = new Text2('Level 1', { size: 60, fill: 0xFFFFFF }); levelTxt.anchor.set(0.5, 0); LK.gui.top.addChild(levelTxt); levelTxt.y = 200; // Turbo button var turboButton = LK.getAsset('turboButton', { anchorX: 0.5, anchorY: 0.5 }); var turboButtonText = new Text2('TURBO', { size: 36, fill: 0xffffff }); turboButtonText.anchor.set(0.5, 0.5); turboButton.addChild(turboButtonText); turboButton.x = 2048 - 100; turboButton.y = 2732 - 100; game.addChild(turboButton); // Leaderboard button var leaderboardButton = LK.getAsset('turboButton', { width: 200, height: 80, color: 0x0066CC, anchorX: 0.5, anchorY: 0.5 }); var leaderboardButtonText = new Text2('LİDER TABLOSU', { size: 28, fill: 0xffffff }); leaderboardButtonText.anchor.set(0.5, 0.5); leaderboardButton.addChild(leaderboardButtonText); leaderboardButton.x = 2048 / 2; leaderboardButton.y = 2732 - 100; game.addChild(leaderboardButton); // Turbo button interaction turboButton.down = function (x, y, obj) { if (gameRunning) { car.activateTurbo(); } }; // Leaderboard button interaction leaderboardButton.down = function (x, y, obj) { showLeaderboard(); }; // Touch controls var lastTouchX = 0; game.down = function (x, y, obj) { lastTouchX = x; }; game.move = function (x, y, obj) { if (!gameRunning) return; var deltaX = x - lastTouchX; if (Math.abs(deltaX) > 10) { if (deltaX > 0) { car.moveRight(); } else { car.moveLeft(); } lastTouchX = x; } }; // Console controls for debugging/testing var consoleControls = { left: function left() { if (gameRunning) { car.moveLeft(); console.log("Car moved left"); } }, right: function right() { if (gameRunning) { car.moveRight(); console.log("Car moved right"); } }, reset: function reset() { console.log("Game reset - reload page to restart"); }, turbo: function turbo() { if (gameRunning) { car.activateTurbo(); console.log("Turbo activated!"); } }, help: function help() { console.log("Console Commands:"); console.log("consoleControls.left() - Move car left"); console.log("consoleControls.right() - Move car right"); console.log("consoleControls.turbo() - Activate turbo boost"); console.log("consoleControls.reset() - Reset game info"); console.log("consoleControls.help() - Show this help"); } }; // Make console controls globally available window.controls = consoleControls; // Show help message on game start console.log("Rock Dodge Racing - Console Controls Available!"); console.log("Type 'controls.help()' for commands or use 'controls.left()' and 'controls.right()' to move the car"); // Spawn rock function function spawnRock() { var rock = new Rock(); rock.x = Math.random() * (2048 - 160) + 80; // Keep rocks within screen bounds rock.y = -50; rocks.push(rock); game.addChild(rock); } // Show speech bubble function showSpeechBubble(message, x, y) { var bubble = new SpeechBubble(message); bubble.x = 2048 / 2; // Center horizontally bubble.y = 2732 / 2; // Center vertically game.addChild(bubble); } // Show encouragement message function showEncouragement() { var encouragement = new SpeechBubble("Bravo, böyle devam!"); encouragement.x = 2048 / 2; encouragement.y = 2732 / 2; game.addChild(encouragement); } game.update = function () { if (!gameRunning) return; // Update score based on time survived pointsTimer++; if (pointsTimer >= 10) { // Add point every 10 frames (roughly 6 points per second) LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); pointsTimer = 0; // Check for encouragement message if (LK.getScore() > 0 && LK.getScore() % 100 === 0 && LK.getScore() > lastEncouragementScore) { showEncouragement(); lastEncouragementScore = LK.getScore(); // Level progression every 100 points if (currentLevel < maxLevel) { currentLevel++; levelTxt.setText('Level ' + currentLevel); // Show level up message var levelUpMsg = new SpeechBubble("Level " + currentLevel + "!"); levelUpMsg.x = 2048 / 2; levelUpMsg.y = 2732 / 2 - 150; game.addChild(levelUpMsg); // Increase difficulty with each level rockSpawnRate = Math.max(20, rockSpawnRate - 5); } } // Check win condition if (LK.getScore() >= scoreToWin) { gameRunning = false; saveScore(LK.getScore()); var winMsg = new SpeechBubble("Tebrikler! Oyunu bitirdiniz!"); winMsg.x = 2048 / 2; winMsg.y = 2732 / 2; game.addChild(winMsg); LK.setTimeout(function () { LK.showYouWin(); }, 2500); return; } } // Spawn rocks rockSpawnTimer++; if (rockSpawnTimer >= rockSpawnRate) { spawnRock(); rockSpawnTimer = 0; // Gradually increase difficulty if (rockSpawnRate > 25) { rockSpawnRate = Math.max(25, rockSpawnRate - 0.5); } } // Update and check rocks for (var i = rocks.length - 1; i >= 0; i--) { var rock = rocks[i]; // Check if rock is off screen if (rock.y > 2732 + 50) { rock.destroy(); rocks.splice(i, 1); continue; } // Check collision with car if (rock.intersects(car)) { // Game over gameRunning = false; saveScore(LK.getScore()); // Show speech bubble showSpeechBubble("Araba sürmeyi bilmiyor musun?", car.x, car.y - 100); // Play crash sound LK.getSound('crash').play(); // Flash effect LK.effects.flashScreen(0xff0000, 500); // Show game over after delay LK.setTimeout(function () { LK.showGameOver(); }, 2500); break; } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.normalSpeed = 8;
self.turboSpeed = 16;
self.targetX = self.x;
self.isTurbo = false;
self.turboEndTime = 0;
self.update = function () {
// Check if turbo should end
if (self.isTurbo && LK.ticks > self.turboEndTime) {
self.isTurbo = false;
self.speed = self.normalSpeed;
}
// Smooth movement towards target position
var diff = self.targetX - self.x;
var moveSpeed = self.isTurbo ? 0.25 : 0.15; // Faster movement during turbo
if (Math.abs(diff) > 1) {
self.x += diff * moveSpeed;
}
// Keep car within screen bounds
var halfWidth = carGraphics.width / 2;
if (self.x < halfWidth) {
self.x = halfWidth;
self.targetX = halfWidth;
}
if (self.x > 2048 - halfWidth) {
self.x = 2048 - halfWidth;
self.targetX = 2048 - halfWidth;
}
};
self.moveLeft = function () {
self.targetX -= 150;
};
self.moveRight = function () {
self.targetX += 150;
};
self.activateTurbo = function () {
if (!self.isTurbo) {
self.isTurbo = true;
self.speed = self.turboSpeed;
self.turboEndTime = LK.ticks + 180; // 3 seconds at 60fps
LK.getSound('turbo').play();
// Visual effect
tween(carGraphics, {
tint: 0x00ff00
}, {
duration: 100
});
// Reset color after turbo
LK.setTimeout(function () {
tween(carGraphics, {
tint: 0xffffff
}, {
duration: 200
});
}, 3000);
}
};
return self;
});
var Leaderboard = Container.expand(function () {
var self = Container.call(this);
// Background
var background = LK.getAsset('speechBubble', {
width: 600,
height: 800,
color: 0x000000,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
background.alpha = 0.8;
self.addChild(background);
// Title
var title = new Text2('LİDERLİK TABLOSU', {
size: 50,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 0;
title.y = -350;
self.addChild(title);
// Get leaderboard data
var scores = storage.leaderboard || [];
// Display top 10 scores
for (var i = 0; i < Math.min(10, scores.length); i++) {
var scoreText = new Text2(i + 1 + '. ' + scores[i] + ' puan', {
size: 40,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0.5);
scoreText.x = 0;
scoreText.y = -250 + i * 60;
self.addChild(scoreText);
}
// Close button text
var closeText = new Text2('Devam etmek için dokunun', {
size: 35,
fill: 0x00FF00
});
closeText.anchor.set(0.5, 0.5);
closeText.x = 0;
closeText.y = 350;
self.addChild(closeText);
// Make clickable to close
self.down = function (x, y, obj) {
self.destroy();
};
return self;
});
var Rock = Container.expand(function () {
var self = Container.call(this);
var rockGraphics = self.attachAsset('rock', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4 + Math.random() * 3;
self.update = function () {
self.y += self.speed;
};
return self;
});
var SpeechBubble = Container.expand(function (message) {
var self = Container.call(this);
// Create bubble background
var bubble = LK.getAsset('speechBubble', {
width: 400,
height: 120,
color: 0xffffff,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(bubble);
// Create text
var text = new Text2(message, {
size: 40,
fill: 0x000000
});
text.anchor.set(0.5, 0.5);
self.addChild(text);
// Animation
self.alpha = 0;
self.scaleX = 0.5;
self.scaleY = 0.5;
// Animate in
tween(self, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeOut
});
// Auto remove after delay
LK.setTimeout(function () {
tween(self, {
alpha: 0,
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 200,
onFinish: function onFinish() {
if (self.parent) {
self.destroy();
}
}
});
}, 2000);
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var car;
var rocks = [];
var gameRunning = true;
var rockSpawnTimer = 0;
var rockSpawnRate = 60; // frames between spawns
var pointsTimer = 0;
var lastEncouragementScore = 0;
var currentLevel = 1;
var maxLevel = 10;
var scoreToWin = 1000;
// Leaderboard functions
function saveScore(score) {
var leaderboard = storage.leaderboard || [];
leaderboard.push(score);
leaderboard.sort(function (a, b) {
return b - a;
}); // Sort descending
leaderboard = leaderboard.slice(0, 10); // Keep only top 10
storage.leaderboard = leaderboard;
}
function showLeaderboard() {
var leaderboard = new Leaderboard();
leaderboard.x = 2048 / 2;
leaderboard.y = 2732 / 2;
game.addChild(leaderboard);
}
// Initialize car
car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2732 - 150;
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Level display
var levelTxt = new Text2('Level 1', {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 200;
// Turbo button
var turboButton = LK.getAsset('turboButton', {
anchorX: 0.5,
anchorY: 0.5
});
var turboButtonText = new Text2('TURBO', {
size: 36,
fill: 0xffffff
});
turboButtonText.anchor.set(0.5, 0.5);
turboButton.addChild(turboButtonText);
turboButton.x = 2048 - 100;
turboButton.y = 2732 - 100;
game.addChild(turboButton);
// Leaderboard button
var leaderboardButton = LK.getAsset('turboButton', {
width: 200,
height: 80,
color: 0x0066CC,
anchorX: 0.5,
anchorY: 0.5
});
var leaderboardButtonText = new Text2('LİDER TABLOSU', {
size: 28,
fill: 0xffffff
});
leaderboardButtonText.anchor.set(0.5, 0.5);
leaderboardButton.addChild(leaderboardButtonText);
leaderboardButton.x = 2048 / 2;
leaderboardButton.y = 2732 - 100;
game.addChild(leaderboardButton);
// Turbo button interaction
turboButton.down = function (x, y, obj) {
if (gameRunning) {
car.activateTurbo();
}
};
// Leaderboard button interaction
leaderboardButton.down = function (x, y, obj) {
showLeaderboard();
};
// Touch controls
var lastTouchX = 0;
game.down = function (x, y, obj) {
lastTouchX = x;
};
game.move = function (x, y, obj) {
if (!gameRunning) return;
var deltaX = x - lastTouchX;
if (Math.abs(deltaX) > 10) {
if (deltaX > 0) {
car.moveRight();
} else {
car.moveLeft();
}
lastTouchX = x;
}
};
// Console controls for debugging/testing
var consoleControls = {
left: function left() {
if (gameRunning) {
car.moveLeft();
console.log("Car moved left");
}
},
right: function right() {
if (gameRunning) {
car.moveRight();
console.log("Car moved right");
}
},
reset: function reset() {
console.log("Game reset - reload page to restart");
},
turbo: function turbo() {
if (gameRunning) {
car.activateTurbo();
console.log("Turbo activated!");
}
},
help: function help() {
console.log("Console Commands:");
console.log("consoleControls.left() - Move car left");
console.log("consoleControls.right() - Move car right");
console.log("consoleControls.turbo() - Activate turbo boost");
console.log("consoleControls.reset() - Reset game info");
console.log("consoleControls.help() - Show this help");
}
};
// Make console controls globally available
window.controls = consoleControls;
// Show help message on game start
console.log("Rock Dodge Racing - Console Controls Available!");
console.log("Type 'controls.help()' for commands or use 'controls.left()' and 'controls.right()' to move the car");
// Spawn rock function
function spawnRock() {
var rock = new Rock();
rock.x = Math.random() * (2048 - 160) + 80; // Keep rocks within screen bounds
rock.y = -50;
rocks.push(rock);
game.addChild(rock);
}
// Show speech bubble
function showSpeechBubble(message, x, y) {
var bubble = new SpeechBubble(message);
bubble.x = 2048 / 2; // Center horizontally
bubble.y = 2732 / 2; // Center vertically
game.addChild(bubble);
}
// Show encouragement message
function showEncouragement() {
var encouragement = new SpeechBubble("Bravo, böyle devam!");
encouragement.x = 2048 / 2;
encouragement.y = 2732 / 2;
game.addChild(encouragement);
}
game.update = function () {
if (!gameRunning) return;
// Update score based on time survived
pointsTimer++;
if (pointsTimer >= 10) {
// Add point every 10 frames (roughly 6 points per second)
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
pointsTimer = 0;
// Check for encouragement message
if (LK.getScore() > 0 && LK.getScore() % 100 === 0 && LK.getScore() > lastEncouragementScore) {
showEncouragement();
lastEncouragementScore = LK.getScore();
// Level progression every 100 points
if (currentLevel < maxLevel) {
currentLevel++;
levelTxt.setText('Level ' + currentLevel);
// Show level up message
var levelUpMsg = new SpeechBubble("Level " + currentLevel + "!");
levelUpMsg.x = 2048 / 2;
levelUpMsg.y = 2732 / 2 - 150;
game.addChild(levelUpMsg);
// Increase difficulty with each level
rockSpawnRate = Math.max(20, rockSpawnRate - 5);
}
}
// Check win condition
if (LK.getScore() >= scoreToWin) {
gameRunning = false;
saveScore(LK.getScore());
var winMsg = new SpeechBubble("Tebrikler! Oyunu bitirdiniz!");
winMsg.x = 2048 / 2;
winMsg.y = 2732 / 2;
game.addChild(winMsg);
LK.setTimeout(function () {
LK.showYouWin();
}, 2500);
return;
}
}
// Spawn rocks
rockSpawnTimer++;
if (rockSpawnTimer >= rockSpawnRate) {
spawnRock();
rockSpawnTimer = 0;
// Gradually increase difficulty
if (rockSpawnRate > 25) {
rockSpawnRate = Math.max(25, rockSpawnRate - 0.5);
}
}
// Update and check rocks
for (var i = rocks.length - 1; i >= 0; i--) {
var rock = rocks[i];
// Check if rock is off screen
if (rock.y > 2732 + 50) {
rock.destroy();
rocks.splice(i, 1);
continue;
}
// Check collision with car
if (rock.intersects(car)) {
// Game over
gameRunning = false;
saveScore(LK.getScore());
// Show speech bubble
showSpeechBubble("Araba sürmeyi bilmiyor musun?", car.x, car.y - 100);
// Play crash sound
LK.getSound('crash').play();
// Flash effect
LK.effects.flashScreen(0xff0000, 500);
// Show game over after delay
LK.setTimeout(function () {
LK.showGameOver();
}, 2500);
break;
}
}
};