Game engine API · Lesson 12
Build Your First Game
Add a game with Oyunlar2D.kayit. Write pure kur and adim logic, run it through the engine, verify score and finish events, and know the drawing limit.
The way to add a custom game is the registry: Oyunlar2D.kayit(ad, {kur, adim}). Your code stays pure; the engine handles DOM and canvas.
What you will learn
- Writing kur and adim functions
- Registering with Oyunlar2D.kayit
- Checking score and finish events through the engine
API signatures
| Call | Result |
|---|---|
basla(mod, config) | {ok, hata?} · validates mode and config |
durum() | state copy · mutation has no effect |
olay(ad, fn) | function that cancels the subscription |
kayit | pure helper defined in the source module |
sinirla | pure helper defined in the source module |
adim | pure helper defined in the source module |
Write the pure logic
kur(config) returns a state object. adim(durum, girdi, dt) returns an event list and mutates the state. The rule requires both to be functions.
The sinirla() helper keeps the target between 1 and 20. Instead of writing the score directly into the state, publish a puan event.
The counter game logic
var O = window.Oyunlar2D;
function kur(cfg) {
var c = cfg || {};
return {
tip: 'sayac',
hedef: O.sinirla(c.hedef || 5, 1, 20),
sayi: 0,
puan: 0,
bitti: false,
mesaj: 'Press Space to count.'
};
}
function adim(durum, girdi, dt) {
var olaylar = [];
if (durum.bitti || !girdi || girdi.sec !== true) return { olaylar: olaylar };
durum.sayi += 1;
durum.puan += 10;
olaylar.push({ ad: 'puan', deger: 10, sebep: 'sayac' });
if (durum.sayi >= durum.hedef) {
durum.bitti = true;
olaylar.push({ ad: 'bitti', puan: durum.puan });
}
return { olaylar: olaylar };
}What happens in this code? dt is unused in this game, but the signature still takes three parameters because that is how the engine calls adim().
Register and run
Registering the same id a second time updates the definition instead of duplicating it. The new game appears in oyunlar() immediately.
Important limit: the motif-2d.js drawing layer only draws known types. Without a custom painter the game still runs and the HUD and score work, but no shape is drawn.
Register and start the game
var O = window.Oyunlar2D;
var M = window.OyunMotoru;
O.kayit('sayac', {
ad: 'Counter',
aciklama: 'Reach the target with Space.',
kur: kur,
adim: adim
});
console.log(O.oyunlar().some(function (o) { return o.id === 'sayac'; })); // true
M.olay('bitti', function (e) {
console.log('Game over, score:', e.puan);
});
M.basla('2d', {
kok: document.getElementById('oyunAlani2D'),
oyun: 'sayac',
hedef: 5
});
console.log(M.durum().oyun); // 'sayac'What happens in this code? This flow does not touch existing questions; it only adds a game definition. Re-registering the same id safely updates it.
Check it yourself
Before publishing, test the pure logic directly. No DOM is needed; only the state and the event list matter.
Pure test
var O = window.Oyunlar2D;
var d = O.kur('sayac', { hedef: 2 });
var a = O.adim(d, { sec: true }, 16);
var b = O.adim(d, { sec: true }, 16);
console.log(a.olaylar[0].ad, b.olaylar[0].ad); // puan puan
console.log(d.bitti, d.puan); // true 20
console.log(O.adim(d, { sec: true }, 16).olaylar.length); // 0What happens in this code? After the game ends, adim() returns an empty list; that stops the game from producing score repeatedly.
Common mistakes
| Wrong approach | Result | Correct approach |
|---|---|---|
| kur or adim is not a function | kayit() returns false | Make both functions |
| Writing the score into the engine directly | The event chain breaks | Publish a puan event |
| Assuming a painter for a custom type | No shape is drawn | HUD and score still work |
| Calling adim after finish | An empty list is returned | Start a new game with basla() |
Try it yourself
Set the target to 3 and extend the pure test to three adim calls. Confirm the third call produces the bitti event and the fourth returns an empty list.