Game engine API · Lesson 9
Catch Game Walkthrough
Inspect the catch game setup and step rules: target placement, circle collision, 0.24 px/ms speed and the timer finish event.
Catch is a timed circle-collection game. The player radius is 14, target radii are 11, and each catch awards 10 points.
What you will learn
- yakalamaKur(config) fields
- Contact testing with daireCakisi
- dt clamping and the bitti event
API signatures
| Call | Result |
|---|---|
basla(mod, config) | {ok, hata?} · validates mode and config |
olay(ad, fn) | function that cancels the subscription |
yakalamaKur | pure helper defined in the source module |
adim | pure helper defined in the source module |
daireCakisi | pure helper defined in the source module |
sinirla | pure helper defined in the source module |
Setup fields
genislik is at least 200 and yukseklik at least 160; the defaults are 480 and 320. hedef is at least 3 and sure at least 5000 ms, defaulting to 8 and 30000.
Targets are random but are repositioned up to eight times so they do not start on top of the player. The default seed is 424242.
Create the game state
var O = window.Oyunlar2D;
var d = O.yakalamaKur({
genislik: 480,
yukseklik: 320,
hedef: 3,
sure: 5000,
tohum: 424242
});
console.log(d.hedefler.length); // 3
console.log(d.sureMs); // 5000
console.log(d.oyuncu.r, d.hedefler[0].r); // 14 11What happens in this code? The _rng field is an internal generator and is not used by game code. Seeded randomness keeps tests repeatable.
Step, collision and timer
adim(durum, girdi, dt) clamps dt to 0-60 ms. Speed is 0.24 px/ms; sinirla() keeps the player inside the field.
Circle collision compares squared centre distances. A caught target is removed and replaced; when time runs out the bitti event is published.
Move and read the events
var O = window.Oyunlar2D;
var d = O.yakalamaKur({ hedef: 3, sure: 5000 });
var sonuc = O.adim(d, { x: 1, y: 0 }, 16);
console.log(d.oyuncu.x > 240); // true
console.log(O.daireCakisi({ x: 0, y: 0, r: 1 }, { x: 1, y: 0, r: 1 })); // true
window.OyunMotoru.olay('puan', function (e) {
console.log('Gained:', e.deger, e.sebep);
});
window.OyunMotoru.basla('2d', {
kok: document.getElementById('oyunAlani2D'),
oyun: 'yakalama',
hedef: 5,
sure: 20000
});What happens in this code? The engine reads the score from puan events, not from the game state. The 2D layer adds e.deger through puanAta().
Common mistakes
| Wrong approach | Result | Correct approach |
|---|---|---|
| No dt clamp | The player teleports after a tab switch | adim() clamps to 60 ms |
| Box test for circles | False contact at the corners | Use daireCakisi() |
| Leaving a caught target in place | Score repeats every frame | The code replaces the target |
| Setting sure to 1000 | The 5000 ms floor applies | sinirla() keeps the lower bound |
Try it yourself
Set hedef to 3 and sure to 5000. Move the player onto the targets and confirm three puan events followed by a bitti event.