Pertemuan 4
Function dan Modular Gameplay Actions
Membuat gameplay actions seperti attack, heal, movePlayer, dan resetGame dengan function.
Tujuan Pembelajaran
Memahami function declaration.
Menggunakan parameter dan return.
Refactor game logic menjadi modular.
Why Function?
Kode gameplay action bisa dipakai ulang.
Logic lebih mudah dibaca dan dirawat.
Setiap action punya tanggung jawab jelas.
Function Declaration
function attack(enemyHealth, damage) { return enemyHealth - damage; } let slimeHealth = 50; slimeHealth = attack(slimeHealth, 10);
Gameplay Actions
function heal(health, amount) { return health + amount; } function movePlayer(x, speed) { return x + speed; } function resetGame() { return { score: 0, health: 100 }; }
Refactor Game Logic
Pindahkan action yang berulang ke function agar script game lebih modular.
Input
>
movePlayer()
>
attack()
>
update State
Activity
- Buat function `attack()`.
- Buat function `heal()`.
- Buat function `resetGame()`.
- Refactor logic score dan health menggunakan function.
Mini Quiz
Nilai yang dikirim ke function saat dipanggil disebut...
Output Pertemuan
Modular game script dengan functions.