נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
46
חלקPart 8: משחקים ב-JavaGames in Java כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מצבי משחק: תפריט, משחק ו-Game OverGame states: menu, play & game over

מכונת מצבים עם enum, מסכים, שמירת שיא, וקול.

A state machine with an enum, screens, a saved high score, and sound.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • מכונת מצבים: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (enum State).state machine: The game is always in one state from a closed list (enum State).
  • מעבר: שינוי ממצב למצב בעקבות אירוע:transition: A change from state to state after an event:
  • Game.on(key) מחליט לפי state: בתפריט ENTER מתחיל, במשחק HIT מוריד חיים, וב-Game Over ENTER חוזר לתפריט.Game.on(key) decides by state: in the menu ENTER starts, in play HIT takes a life, and in game over ENTER returns to the menu.
  • מקש שלא מתאים למצב פשוט לא עושה כלום (ESC כאן).A key that doesn't fit the state just does nothing (ESC here).
  • switch שמחזיר ערך על enum חייב לכסות כל מצב.A value-returning switch on an enum must cover every state.
  • אם תוסיפו PAUSED ל-enum, javac יראה לכם כל מקום שצריך לעדכן.If you add PAUSED to the enum, javac shows you every place to update.
  • the switch expression does not cover all possible input values:the switch expression does not cover all possible input values:
  • משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score

מונחיםTerms

  • מכונת מצביםstate machine: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (enum State). כל אירוע מועבר לפי המצב, וחלק מהאירועים מעבירים למצב אחר.The game is always in one state from a closed list (enum State). Each event is handled by the state, and some events move to another state.
  • מעברtransition: שינוי ממצב למצב בעקבות אירוע: MENU + ENTER → PLAYING. כדאי לצייר אותם כחצים לפני שכותבים קוד.A change from state to state after an event: MENU + ENTER → PLAYING. Worth drawing as arrows before writing code.
  • שיאhigh score: הניקוד הגבוה ביותר, שנשמר בקובץ כדי שיישאר אחרי שסוגרים את המשחק. שומרים רק אם התוצאה החדשה גבוהה יותר.The highest score, saved in a file so it stays after the game closes. Save only if the new result is higher.
  • מכונת מצביםstate machine: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (enum State). כל אירוע מועבר לפי המצב, וחלק מהאירועים מעבירים למצב אחר.The game is always in one state from a closed list (enum State). Each event is handled by the state, and some events move to another state.
  • מעברtransition: שינוי ממצב למצב בעקבות אירוע: MENU + ENTER → PLAYING. כדאי לצייר אותם כחצים לפני שכותבים קוד.A change from state to state after an event: MENU + ENTER → PLAYING. Worth drawing as arrows before writing code.
  • שיאhigh score: הניקוד הגבוה ביותר, שנשמר בקובץ כדי שיישאר אחרי שסוגרים את המשחק. שומרים רק אם התוצאה החדשה גבוהה יותר.The highest score, saved in a file so it stays after the game closes. Save only if the new result is higher.

Cheatsheet

מכונת מצביםA state machine
Game g = new Game();
for (String key : List.of("ENTER", "HIT", "HIT", "HIT", "ENTER", "ESC")) {
    g.on(key);
    System.out.println(key + " -> " + g.state + " lives=" + g.lives);
}
מסך לפי מצבA screen per state
for (State s : State.values()) {
    System.out.println(screen(s, 120));
}

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
the switch expression does not cover all possible input valuצייד באגים: מצב שנשכחBug hunter: a forgotten statethe switch expression does not cover all possible input values: לכל מצב צריך תשובה.the switch expression does not cover all possible input values: every state needs an answer.

נבנה על ידיBuilt by Ariel Yohimovich