User prompt
if snake eat food dont game over continue game
User prompt
yem aldıktan sonra yılan büyüsün
User prompt
kenara çarpınca oyun bitsin
User prompt
yem aldıkça 5 puan alsın ve yeni yemler çıksın ortaya
User prompt
yılan ve yemleri de ekle
User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 1; i < snake.segments.length; i++) {' Line Number: 55
User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of undefined (reading 'length')' in or related to this line: 'if (snake.segments.length > 0 && snake.segments[0].intersects(food)) {' Line Number: 47
User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of undefined (reading '0')' in or related to this line: 'if (snake.segments[0].intersects(food)) {' Line Number: 47
User prompt
Please fix the bug: 'Timeout.tick error: snake.update is not a function' in or related to this line: 'snake.update();' Line Number: 43
User prompt
Please fix the bug: 'Timeout.tick error: setInterval is not a function' in or related to this line: 'gameInterval = setInterval(function () {' Line Number: 42
User prompt
Please fix the bug: 'setTimeout is not a function' in or related to this line: 'setTimeout(function () {' Line Number: 63
User prompt
Please fix the bug: 'food.randomizePosition is not a function' in or related to this line: 'food.randomizePosition();' Line Number: 33
Code edit (1 edits merged)
Please save this source code
Initial prompt
Snakes
/**** * Classes ****/ // Food class (değişiklik yok) var Food = Container.expand(function () { var self = Container.call(this); self.randomizePosition = function () { self.x = Math.floor(Math.random() * (2048 - self.width)); self.y = Math.floor(Math.random() * (2732 - self.height)); }; } // ... (önceki kod ile aynı) ); // Snake class (değişiklik yok) var Snake = Container.expand(function () { var self = Container.call(this); } // ... (önceki kod ile aynı) ); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ 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 = setInterval(function () { snake.update(); if (snake.segments[0].intersects(food)) { snake.grow(); food.randomizePosition(); LK.setScore(LK.getScore() + 1); if (LK.getScore() % 5 === 0) { snake.speed *= 1.1; } } for (var i = 1; i < snake.segments.length; i++) { if (snake.segments[0].intersects(snake.segments[i])) { LK.showGameOver(); clearInterval(gameInterval); // Oyun bitince döngüyü durdur gameStarted = false; break; } } }, 100); // 100 milisaniyede bir güncelleme (hızı kontrol eder) } // Oyun başladığında 1 saniye sonra startGameLoop fonksiyonunu çağır 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); } };
===================================================================
--- original.js
+++ change.js
@@ -1,63 +1,21 @@
/****
* Classes
****/
-// Boyutlar küçültüldü
-// Food class
+// Food class (değişiklik yok)
var Food = Container.expand(function () {
var self = Container.call(this);
- var foodGraphics = self.attachAsset('food', {
- anchorX: 0.5,
- anchorY: 0.5
- });
self.randomizePosition = function () {
- self.x = Math.floor(Math.random() * 40) * 50 + 25; // Kenarlara yapışmayı engelle
- self.y = Math.floor(Math.random() * 54) * 50 + 25; // Kenarlara yapışmayı engelle
+ self.x = Math.floor(Math.random() * (2048 - self.width));
+ self.y = Math.floor(Math.random() * (2732 - self.height));
};
- return self;
-});
-// Snake class
+} // ... (önceki kod ile aynı)
+);
+// Snake class (değişiklik yok)
var Snake = Container.expand(function () {
var self = Container.call(this);
- self.segments = [];
- self.direction = {
- x: 1,
- y: 0
- }; // Başlangıç yönü: sağ
- self.speed = 5;
- self.growth = 0;
- for (var i = 0; i < 5; i++) {
- var segment = self.attachAsset('snakeSegment', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- segment.x = 1024 - i * 50; // Başlangıç pozisyonu sağdan
- segment.y = 1366;
- self.segments.push(segment);
- }
- 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;
- }
- self.segments[0].x += self.direction.x * self.speed;
- self.segments[0].y += self.direction.y * self.speed;
- // Ekran sınırlarını kontrol et ve oyunu bitir
- if (self.segments[0].x < 0 || self.segments[0].x > 2048 || self.segments[0].y < 0 || self.segments[0].y > 2732) {
- LK.showGameOver();
- }
- };
- self.changeDirection = function (newDirection) {
- if (newDirection.x !== -self.direction.x || newDirection.y !== -self.direction.y) {
- // Ters yöne gitmeyi engelle
- self.direction = newDirection;
- }
- };
- self.grow = function () {
- self.growth++;
- };
- return self;
-});
+} // ... (önceki kod ile aynı)
+);
/****
* Initialize Game
****/
@@ -67,50 +25,77 @@
/****
* Game Code
****/
-// Boyutlar küçültüldü
var snake = game.addChild(new Snake());
var food = game.addChild(new Food());
food.randomizePosition();
-game.update = function () {
- snake.update();
- if (snake.segments[0].intersects(food)) {
- snake.grow();
- food.randomizePosition();
- LK.setScore(LK.getScore() + 1);
- if (LK.getScore() % 5 === 0) {
- snake.speed *= 1.1;
+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 = setInterval(function () {
+ snake.update();
+ if (snake.segments[0].intersects(food)) {
+ snake.grow();
+ food.randomizePosition();
+ LK.setScore(LK.getScore() + 1);
+ if (LK.getScore() % 5 === 0) {
+ snake.speed *= 1.1;
+ }
}
- }
- // Kendine çarpma kontrolü (daha verimli)
- for (var i = 1; i < snake.segments.length; i++) {
- if (snake.segments[0].intersects(snake.segments[i])) {
- LK.showGameOver();
- break; // Çarpışma tespit edildikten sonra döngüden çık
+ for (var i = 1; i < snake.segments.length; i++) {
+ if (snake.segments[0].intersects(snake.segments[i])) {
+ LK.showGameOver();
+ clearInterval(gameInterval); // Oyun bitince döngüyü durdur
+ gameStarted = false;
+ break;
+ }
}
- }
-};
+ }, 100); // 100 milisaniyede bir güncelleme (hızı kontrol eder)
+}
+// Oyun başladığında 1 saniye sonra startGameLoop fonksiyonunu çağır
+setTimeout(function () {
+ startGameLoop();
+ gameStarted = true;
+}, 1000); // 1000 milisaniye = 1 saniye
game.down = function (x, y, obj) {
- 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;
+ 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;
+ }
}
- } 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);
}
- 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);
+ }
};
\ No newline at end of file