Pertemuan 12
Game State, Menu, Pause, Game Over, Restart, dan localStorage
Membuat state management dan persistent high score di browser.
Tujuan Pembelajaran
Mengelola state START, PLAYING, PAUSED, GAME_OVER.
Membuat restart dan return to menu.
Menyimpan high score dengan localStorage.
Game States
START
>
PLAYING
>
PAUSED
>
GAME_OVER
State Logic
let gameState = "START"; function update() { if (gameState === "PLAYING") { updatePlayer(); updateEnemies(); } }
Menu, Pause, Game Over
function startGame() { gameState = "PLAYING"; } function pauseGame() { gameState = "PAUSED"; } function endGame() { gameState = "GAME_OVER"; }
High Score dengan localStorage
localStorage menyimpan data sederhana di browser.
localStorage
{
highScore: "240"
}
{
highScore: "240"
}
Save High Score
const highScore = Number(localStorage.getItem("highScore")) || 0; if (score > highScore) { localStorage.setItem("highScore", score); }
Activity
- Buat variable gameState.
- Buat start, pause, restart, return to menu.
- Buat game over screen.
- Simpan high score dengan localStorage.
Mini Quiz
API browser untuk menyimpan high score sederhana adalah...
Output Pertemuan
Full game with states dan high score saving.