/**** * Classes ****/ // Food class (değişiklik yok) var Food = Container.expand(function () { var self = Container.call(this); self.attachAsset('food', { anchorX: 0.5, anchorY: 0.5, tint: function () { var color; do { color = Math.random() * 0xFFFFFF; } while (color === 0x000000 || color === 0x008000); return color; }() }); self.randomizePosition = function () { self.x = Math.floor(Math.random() * (2048 - self.width)); self.y = Math.floor(Math.random() * (2732 - self.height)); }; }); // Snake class (değişiklik yok) var Snake = Container.expand(function () { var self = Container.call(this); self.segments = []; self.direction = { x: 1, y: 0 }; self.speed = 5; self.grow = function () { var newSegment = self.attachAsset('snakeSegment', { anchorX: 0.5, anchorY: 0.5, tint: 0x000000, shape: 'ellipse' }); self.segments.push(newSegment); }; self.changeDirection = function (newDirection) { self.direction = newDirection; }; self.update = function () { for (var i = self.segments.length - 1; i > 0; i--) { self.segments[i].x = self.segments[i - 1].x; self.segments[i].y = self.segments[i - 1].y; } if (self.segments.length > 0) { self.segments[0].x += self.direction.x * self.speed; self.segments[0].y += self.direction.y * self.speed; // Check if the snake has hit the edge of the game area if (self.segments[0].x < 0 || self.segments[0].x > 2048 || self.segments[0].y < 0 || self.segments[0].y > 2732) { LK.showGameOver(); } } }; self.grow(); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x008000 }); /**** * Game Code ****/ // Create a text object for the score display var scoreText = new Text2('0', { size: 50, fill: 0xFFFFFF }); // Create a red border for the game area var border; game.addChild(border); // Set the anchor point to the top right corner scoreText.anchor.set(1, 0); // Add the score text to the GUI overlay at the top right corner LK.gui.topRight.addChild(scoreText); // Update the score display every game tick game.update = function () { scoreText.setText('Score: ' + LK.getScore()); }; //snake.segments.push(newSegment); // Snake class (değişiklik yok) var snake = game.addChild(new Snake()); var food = game.addChild(new Food()); food.randomizePosition(); var gameStarted = false; // Oyunun başlayıp başlamadığını kontrol etmek için değişken var gameInterval; // Oyun döngüsünü tutacak değişken // Oyun döngüsünü başlatacak fonksiyon function startGameLoop() { gameInterval = LK.setInterval(function () { snake.update(); if (snake.segments && snake.segments.length > 0 && snake.segments[0].intersects(food)) { snake.grow(); food.destroy(); // Destroy the current food food = game.addChild(new Food()); // Add a new food asset food.randomizePosition(); // Randomize the position of the new food LK.setScore(LK.getScore() + 5); // Increase the score by 5 if (LK.getScore() % 5 === 0 && snake.speed < 15) { snake.speed *= 1.1; } } // Removed game over condition when the snake intersects with itself }, 100); // 100 milisaniyede bir güncelleme (hızı kontrol eder) } // Oyun başladığında 1 saniye sonra startGameLoop fonksiyonunu çağır LK.setTimeout(function () { startGameLoop(); gameStarted = true; }, 1000); // 1000 milisaniye = 1 saniye game.down = function (x, y, obj) { if (gameStarted) { // Oyun başladıysa yön değiştir var direction = { x: 0, y: 0 }; if (Math.abs(x - snake.segments[0].x) > Math.abs(y - snake.segments[0].y)) { if (x < snake.segments[0].x) { direction.x = -1; direction.y = 0; } else if (x > snake.segments[0].x) { direction.x = 1; direction.y = 0; } } else { if (y < snake.segments[0].y) { direction.x = 0; direction.y = -1; } else if (y > snake.segments[0].y) { direction.x = 0; direction.y = 1; } } snake.changeDirection(direction); } }; //showGameOver fonksiyonuna oyunun yeniden başlaması için bir kod ekleyelim LK.showGameOver = function () { if (confirm("Oyun Bitti! Tekrar oynamak ister misiniz?")) { LK.setScore(0); // skoru sıfırla snake = game.addChild(new Snake()); // yeni bir yılan oluştur food = game.addChild(new Food()); // yeni bir yem oluştur food.randomizePosition(); //yemi rastgele konuma yerleştir snake.speed = 5; setTimeout(function () { startGameLoop(); gameStarted = true; }, 1000); } };
/****
* Classes
****/
// Food class (değişiklik yok)
var Food = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5,
tint: function () {
var color;
do {
color = Math.random() * 0xFFFFFF;
} while (color === 0x000000 || color === 0x008000);
return color;
}()
});
self.randomizePosition = function () {
self.x = Math.floor(Math.random() * (2048 - self.width));
self.y = Math.floor(Math.random() * (2732 - self.height));
};
});
// Snake class (değişiklik yok)
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 1,
y: 0
};
self.speed = 5;
self.grow = function () {
var newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
tint: 0x000000,
shape: 'ellipse'
});
self.segments.push(newSegment);
};
self.changeDirection = function (newDirection) {
self.direction = newDirection;
};
self.update = function () {
for (var i = self.segments.length - 1; i > 0; i--) {
self.segments[i].x = self.segments[i - 1].x;
self.segments[i].y = self.segments[i - 1].y;
}
if (self.segments.length > 0) {
self.segments[0].x += self.direction.x * self.speed;
self.segments[0].y += self.direction.y * self.speed;
// Check if the snake has hit the edge of the game area
if (self.segments[0].x < 0 || self.segments[0].x > 2048 || self.segments[0].y < 0 || self.segments[0].y > 2732) {
LK.showGameOver();
}
}
};
self.grow();
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000
});
/****
* Game Code
****/
// Create a text object for the score display
var scoreText = new Text2('0', {
size: 50,
fill: 0xFFFFFF
});
// Create a red border for the game area
var border;
game.addChild(border);
// Set the anchor point to the top right corner
scoreText.anchor.set(1, 0);
// Add the score text to the GUI overlay at the top right corner
LK.gui.topRight.addChild(scoreText);
// Update the score display every game tick
game.update = function () {
scoreText.setText('Score: ' + LK.getScore());
};
//snake.segments.push(newSegment);
// Snake class (değişiklik yok)
var snake = game.addChild(new Snake());
var food = game.addChild(new Food());
food.randomizePosition();
var gameStarted = false; // Oyunun başlayıp başlamadığını kontrol etmek için değişken
var gameInterval; // Oyun döngüsünü tutacak değişken
// Oyun döngüsünü başlatacak fonksiyon
function startGameLoop() {
gameInterval = LK.setInterval(function () {
snake.update();
if (snake.segments && snake.segments.length > 0 && snake.segments[0].intersects(food)) {
snake.grow();
food.destroy(); // Destroy the current food
food = game.addChild(new Food()); // Add a new food asset
food.randomizePosition(); // Randomize the position of the new food
LK.setScore(LK.getScore() + 5); // Increase the score by 5
if (LK.getScore() % 5 === 0 && snake.speed < 15) {
snake.speed *= 1.1;
}
}
// Removed game over condition when the snake intersects with itself
}, 100); // 100 milisaniyede bir güncelleme (hızı kontrol eder)
}
// Oyun başladığında 1 saniye sonra startGameLoop fonksiyonunu çağır
LK.setTimeout(function () {
startGameLoop();
gameStarted = true;
}, 1000); // 1000 milisaniye = 1 saniye
game.down = function (x, y, obj) {
if (gameStarted) {
// Oyun başladıysa yön değiştir
var direction = {
x: 0,
y: 0
};
if (Math.abs(x - snake.segments[0].x) > Math.abs(y - snake.segments[0].y)) {
if (x < snake.segments[0].x) {
direction.x = -1;
direction.y = 0;
} else if (x > snake.segments[0].x) {
direction.x = 1;
direction.y = 0;
}
} else {
if (y < snake.segments[0].y) {
direction.x = 0;
direction.y = -1;
} else if (y > snake.segments[0].y) {
direction.x = 0;
direction.y = 1;
}
}
snake.changeDirection(direction);
}
};
//showGameOver fonksiyonuna oyunun yeniden başlaması için bir kod ekleyelim
LK.showGameOver = function () {
if (confirm("Oyun Bitti! Tekrar oynamak ister misiniz?")) {
LK.setScore(0); // skoru sıfırla
snake = game.addChild(new Snake()); // yeni bir yılan oluştur
food = game.addChild(new Food()); // yeni bir yem oluştur
food.randomizePosition(); //yemi rastgele konuma yerleştir
snake.speed = 5;
setTimeout(function () {
startGameLoop();
gameStarted = true;
}, 1000);
}
};