/**** * Classes ****/ // Class for obstacles var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.scored = false; // Property to track if the player has jumped over the obstacle self.update = function () { self.x += self.speed; if (self.x < -100) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.yVelocity = 0; self.isJumping = false; self.jump = function () { if (!self.isJumping) { self.yVelocity = -30; self.isJumping = true; } }; self.update = function () { self.y += self.yVelocity; self.yVelocity += 1; // Gravity effect if (self.y >= 2300) { // Ground level self.y = 2300; self.isJumping = false; scoreTxt.setText('Puan: ' + LK.getScore()); // Update the score display } // Update the player's x position based on their speed self.x += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = new Player(); player.x = 300; player.y = 2300; game.addChild(player); // Initialize score display var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(1, 0); // Sets anchor to the top right corner of the text. LK.gui.topRight.addChild(scoreTxt); // Attach the score text to the top-right of the screen. // Array to keep track of obstacles var obstacles = []; // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048; obstacle.y = 2300; obstacles.push(obstacle); game.addChild(obstacle); } // Handle touch events for jumping game.down = function (x, y, obj) { player.jump(); }; // Update game logic game.update = function () { player.update(); // Score update moved to obstacle update loop // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (obstacles[i].intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver('Oyun Bitti', 'Tekrar Oyna'); } else if (obstacles[i].x < player.x && !obstacles[i].scored) { obstacles[i].scored = true; LK.setScore(LK.getScore() + 1); // Increase score by 1 for each obstacle jumped over scoreTxt.setText('Puan: ' + LK.getScore()); // Update the score display } } // Spawn new obstacles periodically if (LK.ticks % 120 === 0) { spawnObstacle(); } };
/****
* Classes
****/
// Class for obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.scored = false; // Property to track if the player has jumped over the obstacle
self.update = function () {
self.x += self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.yVelocity = 0;
self.isJumping = false;
self.jump = function () {
if (!self.isJumping) {
self.yVelocity = -30;
self.isJumping = true;
}
};
self.update = function () {
self.y += self.yVelocity;
self.yVelocity += 1; // Gravity effect
if (self.y >= 2300) {
// Ground level
self.y = 2300;
self.isJumping = false;
scoreTxt.setText('Puan: ' + LK.getScore()); // Update the score display
}
// Update the player's x position based on their speed
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 300;
player.y = 2300;
game.addChild(player);
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0); // Sets anchor to the top right corner of the text.
LK.gui.topRight.addChild(scoreTxt); // Attach the score text to the top-right of the screen.
// Array to keep track of obstacles
var obstacles = [];
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = 2300;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle touch events for jumping
game.down = function (x, y, obj) {
player.jump();
};
// Update game logic
game.update = function () {
player.update();
// Score update moved to obstacle update loop
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver('Oyun Bitti', 'Tekrar Oyna');
} else if (obstacles[i].x < player.x && !obstacles[i].scored) {
obstacles[i].scored = true;
LK.setScore(LK.getScore() + 1); // Increase score by 1 for each obstacle jumped over
scoreTxt.setText('Puan: ' + LK.getScore()); // Update the score display
}
}
// Spawn new obstacles periodically
if (LK.ticks % 120 === 0) {
spawnObstacle();
}
};