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

חזרה למפגשBack
JavaJava

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

מפגש 46: מכונת מצבים עם enum, מסכים, שמירת שיא, וקול.Session 46: A state machine with an enum, screens, a saved high score, and sound.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • מכונת מצביםA state machine
  • מסך לפי מצבA screen per state
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • מכונת מצבים: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (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:
  • שיא: הניקוד הגבוה ביותר, שנשמר בקובץ כדי שיישאר אחרי שסוגרים את המשחק.high score: The highest score, saved in a file so it stays after the game closes.
נושאTopic 2

מכונת מצביםA state machine

מכונת מצביםA state machine

  • 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).
נושאTopic 3

מסך לפי מצבA screen per state

מסך לפי מצבA screen per state

  • 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.
נושאTopic 4

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • the switch expression does not cover all possible input values:the switch expression does not cover all possible input values:
נושאTopic 5

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score

מכונת מצבים: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (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:

שיא: הניקוד הגבוה ביותר, שנשמר בקובץ כדי שיישאר אחרי שסוגרים את המשחק.high score: The highest score, saved in a file so it stays after the game closes.

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

השחקן לחץ HIT בזמן שהוא בתפריט. מה קורה?The player pressed HIT while in the menu. What happens?

  • כלום: האירוע לא שייך למצב הזהNothing: the event doesn't belong to this state
  • מאבד חייםLoses a life
  • Game OverGame over
  • שגיאהAn error

כלום: האירוע לא שייך למצב הזהNothing: the event doesn't belong to this state

למה enum ולא מחרוזות כמו "menu"?Why an enum and not strings like "menu"?

  • javac בודק שאין שגיאת כתיב ושה-switch מכסה הכולjavac checks there's no typo and the switch covers everything
  • זה מהיר יותרIt's faster
  • מחרוזות לא עובדות ב-switchStrings don't work in a switch
  • אין הבדלNo difference

javac בודק שאין שגיאת כתיב ושה-switch מכסה הכולjavac checks there's no typo and the switch covers everything

מתי שומרים שיא לקובץ?When do you save a high score to the file?

  • רק כשהתוצאה החדשה גבוהה מהשמורהOnly when the new result beats the saved one
  • אחרי כל משחקAfter every game
  • לעולם לאNever
  • בכל טיקEvery tick

רק כשהתוצאה החדשה גבוהה מהשמורהOnly when the new result beats the saved one

מעבריםTransitions

  • עם Game מהתזכורת הריצו את האירועים HIT, ENTER, HIT, ENTER, HIT, HIT והדפיסו את המצב והחיים אחרי כל אחד.With Game from the refresher run the events HIT, ENTER, HIT, ENTER, HIT, HIT and print the state and lives after each.

מסכיםScreens

  • screen(state, score) עם switch בחצים מחזיר את הטקסט של כל מסך.screen(state, score) with an arrow switch returns each screen's text.

שיא בקובץA high score in a file

  • loadBest() קוראת את best.txt (או 0), ו-saveBest(score) כותבת רק אם יותר גבוה.loadBest() reads best.txt (or 0), and saveBest(score) writes only if it's higher.

צייד באגים: מצב שנשכחBug hunter: a forgotten state

  • music(state) לא מכסה את GAME_OVER, ו-javac עוצר.music(state) doesn't cover GAME_OVER, and javac stops.

משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score

  • הוסיפו ניקוד:Add scoring:

מחסנית מסכיםA stack of screens

  • משחק → השהיה → הגדרות:Game → pause → settings:

צלילים לפי אירועיםSounds by events

  • SoundQueue.on(event) מוסיפה צליל לכל אירוע (ding, bump, game-over), ואחרי Game Over מתעלמת מהכול.SoundQueue.on(event) adds a sound per event (ding, bump, game-over), and after game over ignores everything.

עקבו בסימולציה אחרי state.Follow state in the simulation.

State.values() עובר על כל המצבים.State.values() goes over every state.

מתחילים מ-deleteIfExists כדי שכל הרצה תתחיל נקי.Start with deleteIfExists so every run starts clean.

case GAME_OVER -> "sad.wav";case GAME_OVER -> "sad.wav";

בכניסה ל-PLAYING מאפסים חיים וניקוד, אבל לא את best.On entering PLAYING reset lives and score, but not best.

ArrayDeque עם push, pop, peek.An ArrayDeque with push, pop, peek.

מה למדנוWhat we learned

  • מכונת מצבים: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (enum State).state machine: The game is always in one state from a closed list (enum State).
  • 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.
  • switch שמחזיר ערך על enum חייב לכסות כל מצב.A value-returning switch on an enum must cover every state.
  • 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

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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