Yazılan yılan oyunundaki kod hatası nerede?

cCanbaz

Centipat
Katılım
23 Ağustos 2021
Mesajlar
39
Daha fazla  
Cinsiyet
Erkek
<canvas id="game" width="400" height="400"></canvas>

<script>
class SnakeGame {
constructor() {
this.canvas = document.getElementById('game');
this.context = this.canvas.getContext('2d')
document.addEventListener('keydown', this.onKeyPress.bind(this));
}

init() {
this.positionX = this.positionY = 10;
this.appleX = this.appleY = 5;
this.tailSize = 1;
this.trail = [];
this.gridSize = this.tileCount = 20;
this.velocityX = this.velocityY = 0;

this.timer = setInterval(this.loop.bind(this), 1000/15)
}

reset() {
clearInterval(this.timer);
this.init();
}

loop() {
this.update();
this.draw();
}

update() {
this.positionX += this.velocityX;
this.positionY += this.velocityY;

if (this.positionX < 0) {
this.positionX = this.tileCount - 1;
}
if (this.positionY < 0) {
this.positionY = this.tileCount - 1;
}
if (this.positionX > this.tileCount - 1) {
this.positionX = 0;
}
if (this.positionY > this.tileCount - 1) {
this.positionY = 0;
}

this.trail.forEach(t => {
if (this.positionX === t.positionX && this.positionY === t.positionY) {
this.reset();
}
});

this.trail.push({positionX: this.positionX, positionY: this.positionY});

while (this.trail.length > this.tailSize) {
this.trail.shift();
}

if (this.appleX === this.positionX && this.appleY === positionY) {
this.tailSize++;
this.appleX = Math.floor(Math.random() * this.tileCount);
this.appleY = Math.floor(Math.random() * this.tileCount);
}
}

draw() {
this.context.fillStyle = 'green';
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);

this.context.fillStyle = 'white';
this.context.font = '20px Arial';
this.context.fillText(this.tailSize, 5, 20, 40);

this.context.fillStyle = 'black';
this.trail.forEach(t => {
this.context.fillRect(t.positionX * this.gridSize, t.positionY * this.gridSize, this.gridSize - 5, this.gridSize - 5);
});

this.context.fillStyle = 'black';
this.context.fillRect(this.appleX * this.gridSize, this.appleY * this.gridSize, this.gridSize - 5, this.gridSize - 5);

}

onKeyPress(e) {
if (e.keyCode === 37 && this.velocityX !== 1) {
this.velocityX = -1;
this.velocityY = 0;
}
if (e.keyCode === 38 && this.velocityY !== 1) {
this.velocityX = 0;
this.velocityY = -1;
}
if (e.keyCode === 39 && this.velocityX !== -1) {
this.velocityX = 1;
this.velocityY = 0;
}
if (e.keyCode === 40 && this.velocityY !== -1) {
this.velocityX = 0;
this.velocityY = 1;
}
}
}

const game = new SnakeGame();
window.onload = () => game.init();
</script>

Bir yılan oyunu yapıyorum, yılan çalışıyor ama elmayı yemiyor, içinden geçiyor. Nasıl çözebilirim?
 
Son düzenleyen: Moderatör:
<Canvas ID="Game" width="400" height="400"></canvas>

<Script>
Class SnakeGame {
constructor() {
This. Canvas = document. GetElementById('Game');
This. Context = this. Canvas. GetContext('2D')
Document. AddEventListener('keydown', this. OnKeyPress. Bind(this));
}

init() {
This. PositionX = this. PositionY = 10;
This. AppleX = this. AppleY = 5;
This. TailSize = 1;
This. Trail = [];
This. GridSize = this. TileCount = 20;
This. VelocityX = this. VelocityY = 0;

This. Timer = setInterval(this. Loop. Bind(this), 1000/15)
}

reset() {
ClearInterval(this. Timer);
this.init();
}

loop() {
this.update();
this.draw();
}

update() {
This. PositionX += this. VelocityX;
This. PositionY += this. VelocityY;

İf (this. PositionX < 0) {
This. PositionX = this. TileCount - 1;
}
İf (this. PositionY < 0) {
This. PositionY = this. TileCount - 1;
}
İf (this. PositionX > this. TileCount - 1) {
This. PositionX = 0;
}
İf (this. PositionY > this. TileCount - 1) {
This. PositionY = 0;
}

This. Trail. ForEach(t => {
İf (this. PositionX === t. PositionX && this. PositionY === t. PositionY) {
this.reset();
}
});

This. Trail. Push({positionX: This. PositionX, positionY: This. PositionY});

While (this. Trail. Length > this. TailSize) {
this.trail.shift();
}

İf (this. AppleX === this. PositionX && this. AppleY === positionY) {
This. TailSize++;
This. AppleX = Math.floor(Math.random() * this.tileCount);
This. AppleY = Math.floor(Math.random() * this.tileCount);
}
}

draw() {
This. Context. FillStyle = 'Green';
This. Context. FillRect(0, 0, this. Canvas. Width, this. Canvas. Height);

This. Context. FillStyle = 'white';
This. Context. Font = '20px Arial';
This. Context. FillText(this. TailSize, 5, 20, 40);

This. Context. FillStyle = 'Black';
This. Trail. ForEach(t => {
This. Context. FillRect(t. PositionX * this. GridSize, t. PositionY * this. GridSize, this. GridSize - 5, this. GridSize - 5);
});

This. Context. FillStyle = 'Black';
This. Context. FillRect(this. AppleX * this. GridSize, this. AppleY * this. GridSize, this. GridSize - 5, this. GridSize - 5);

}

OnKeyPress(e) {
İf (e. KeyCode === 37 && this. VelocityX!== 1) {
This. VelocityX = -1;
This. VelocityY = 0;
}
İf (e. KeyCode === 38 && this. VelocityY!== 1) {
This. VelocityX = 0;
This. VelocityY = -1;
}
İf (e. KeyCode === 39 && this. VelocityX!== -1) {
This. VelocityX = 1;
This. VelocityY = 0;
}
İf (e. KeyCode === 40 && this. VelocityY!== -1) {
This. VelocityX = 0;
This. VelocityY = 1;
}
}
}

Const Game = New SnakeGame();
Window. Onload = () => game.init();
</Script>

Bir yılan oyunu yapıyorum, yılan çalışıyor ama elmayı yemiyor, içinden geçiyor. Nasıl çözebilirim?

JavaScript kullanmışsın ben backend geliştiriciyim dili bilmiyorum ama galiba; koymayı unutmuşsun this. Context bölümüne bak bir de IDE üzerinden hata alıyorsan hatayı yazki ne olduğunu anlayalım.
 
Son düzenleme:
JavaScript kullanmışsın ben backend geliştiriciyim dili bilmiyorum ama galiba; koymayı unutmuşsun this. Context bölümüne bak bir de IDE üzerinden hata alıyorsan hatayı yazki ne olduğunu anlayalım.
kanka ben yaptım olmadı ve IDE dediğin ne benim pek bir bilgim yok ben sadece videodan bakıyorum istersen linki
Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.
 
kanka ben yaptım olmadı ve IDE dediğin ne benim pek bir bilgim yok ben sadece videodan bakıyorum istersen linki
Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.
this.context = this.canvas.getContext('2d') sonuna noktalı virgül koymayı dener misin ide kodları yazdığın program .Eğer yazılım öğrenmeye çalışıyorsan ekrandaki yazıları kendi dosyalarına yazarak pek bir şey öğrenemezsin neden yazdığını kavramaya çalış izlemeden yaz.
 
kanka işte dediğin şeyi yaptım zaten kendim de kavramaya çalışıyorum ama bunu değil bu bana fazla, ayrıca sublime text kullanıyorum ve hala aynı hata var ama bence onda sıkıntı olsaydı zaten ekrana bir görüntü gelmezdi çünkü o 2d mi 3d mi onu kontrol ediyor ve ben oraya 3d yazdığımda ekrana görüntü gelmiyor
 
Kanka işte dediğin şeyi yaptım zaten kendim de kavramaya çalışıyorum ama bunu değil bu bana fazla ayrıca sublime text kullanıyorum ve hala aynı hata var ama bence onda sıkıntı olsaydı zaten ekrana bir görüntü gelmezdi çünkü o 2D mi 3D mi onu kontrol ediyor ve ben oraya 3D yazdığımda ekrana görüntü gelmiyor

Syntax yani söz dizimi(yazım yanlışı gibi düşünebilirsin) hatası aldığını düşünüyorum tam olarak ne hatası alıyorsun sadece sitede mi gözükmüyor başka bir şey mi yoksa kopyalayıp atar mısın?
 
kanka yani dediğin syntax console gibi mi? çünkü öyleyse bende hiçbir sorun yok gözükmüyo hata sadece oyunu denediğimde var ama elmayı almadığı için elmayı alınca kuyruğu uzayacak mı bilmiyorum orada sıkıntı var mı merak ediyorum istesem oyunun linkini atardım ama html olduğu için senin sublime text (veya başaka yazılım programlırı vb.) indirip kodu yazıp html olarak açman gerekir yani uğraşırsan hatanın nerede olduğunu bulursun herhalde
 
kanka yani dediğin syntax console gibi mi? çünkü öyleyse bende hiçbir sorun yok gözükmüyo hata sadece oyunu denediğimde var ama elmayı almadığı için elmayı alınca kuyruğu uzayacak mı bilmiyorum orada sıkıntı var mı merak ediyorum
Knk ben javaScript bilmiyorum fakat eğer kuyruk da uzamıyor ise komple yazdığın elma kodlarında hata var.Eğer kuyruk uzuyor ise deyince elmaya o zaman sadece elmanın yok olma kısmında hata vardır.Hiç olmadı internetden bak ben daha çok c# ve python kullanıyorum.O neden le haliyle bu kodlara aşina değilim.
 
Kanka yani dediğin syntax Console gibi mi? Çünkü öyleyse bende hiçbir sorun yok gözükmüyor hata sadece oyunu denediğimde var ama elmayı almadığı için elmayı alınca kuyruğu uzayacak mı bilmiyorum orada sıkıntı var mı merak ediyorum

Adamla birebir aynı yazmayı deneyebilir misin? Birkaç yeri kaçırmış olabileceğine inanıyorum JavaScript hakkında bilgim olduğu söylenemez ama hatalarını gösterecek bir IDE seçmen senin yararına olur İnternete javaScript İDE yazarsan çıkar.
 

Yeni konular

Geri
Yukarı