Atölye › Scorched Earth Tarzı Topçu › 8/23. adım
Boşluk namlunun ucundan bir mermi atsın. Merminin bir hızı var: namlunun yönü çarpı güç. state o an ne
olduğunu ('aiming' nişan alınıyor, 'flying' mermi uçuyor…), turn sıranın kimde olduğunu (0 sen) tutuyor. Mermi
küçük koyu bir top; uçması sonraki adımda.
```js
let turn // 0 or 1: whose shot it is
let shell // { x, y, vx, vy } or null
let state // 'aiming', 'flying', 'boom', 'won' or 'lost'
turn = 0
shell = null
state = 'aiming'
function fire() {
if (state !== 'aiming') return
const t = tanks[turn]
// The shell leaves from the end of the barrel.
shell = {
x: t.x + Math.cos(t.angle) * 14,
y: t.y - 8 + Math.sin(t.angle) * 14,
vx: Math.cos(t.angle) * t.power,
vy: Math.sin(t.angle) * t.power,
}
state = 'flying'
}
else if (event.key === ' ') {
fire()
} else return
if (shell) {
ctx.fillStyle = '#0f172a'
ctx.beginPath()
ctx.arc(shell.x, shell.y, 3, 0, Math.PI * 2)
ctx.fill()
}
```{ x, y, vx, vy }: konumu ve hızı (karede kaç piksel yana ve aşağı). Hız namlu yönünde, power uzunlukta.const t = tanks[turn] → sırası gelen tank. Şimdilik hep sen; ileride bilgisayar da ateş edecek.if (state !== 'aiming') return → mermi uçarken ikinci Boşluk bir şey yapmasın.ctx.arc(x, y, 3, 0, Math.PI * 2) → 3 piksel yarıçaplı tam daire.let tanks satırının altına turn, shell ve state yaz; reset'te tank döngüsünün altına üç satırı yaz.aimBy'ın üstüne fire fonksiyonunu yaz; tuş dinleyicisine else return'den önce Boşluk bloğunu ekle.draw'da tank döngüsünün altına mermi bloğunu yaz. Çalıştır ve Boşluk'a bas.Beceriler: Klavye ve fare girdisi, Hız ve yerçekimi