נבנה על ידיBuilt by Ariel Yohimovich
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.
מכונת מצבים עם enum, מסכים, שמירת שיא, וקול. בסוף המפגש מוסיפים צעד לפרויקט המלווה.A state machine with an enum, screens, a saved high score, and sound. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- מכונת מצביםA state machine
- מסך לפי מצבA screen per state
- טעויות נפוצותCommon mistakes
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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).
Game.on(key) מחליט לפי state: בתפריט ENTER מתחיל, במשחק HIT מוריד חיים, וב-Game Over ENTER חוזר לתפריט. מקש שלא מתאים למצב פשוט לא עושה כלום (ESC כאן).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. 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.
switch שמחזיר ערך על enum חייב לכסות כל מצב. אם תוסיפו PAUSED ל-enum, javac יראה לכם כל מקום שצריך לעדכן.A value-returning switch on an enum must cover every state. 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:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 5
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score
הוסיפו ניקוד: STAR נותן 10, ושלוש פגיעות מסיימות את המשחק ומעדכנות את best. אחרי התסריט (שני משחקים: 30 נקודות ואז 10) הדפיסו GAME_OVER score=10 best=30.Add scoring: STAR gives 10, and three hits end the game and update best. After the script (two games: 30 points then 10) print GAME_OVER score=10 best=30.מכונת מצבים: המשחק תמיד נמצא במצב אחד מתוך רשימה סגורה (enum State).state machine: The game is always in one state from a closed list (enum State).
המונחים של המפגשThis session's termsמעבר: שינוי ממצב למצב בעקבות אירוע:transition: A change from state to state after an event:
המונחים של המפגשThis session's termsשיא: הניקוד הגבוה ביותר, שנשמר בקובץ כדי שיישאר אחרי שסוגרים את המשחק.high score: The highest score, saved in a file so it stays after the game closes.
המונחים של המפגשThis session's termsGame.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.
מכונת מצביםA state machineמקש שלא מתאים למצב פשוט לא עושה כלום (ESC כאן).A key that doesn't fit the state just does nothing (ESC here).
מכונת מצביםA state machineswitch שמחזיר ערך על enum חייב לכסות כל מצב.A value-returning switch on an enum must cover every state.
מסך לפי מצבA screen per stateאם תוסיפו PAUSED ל-enum, javac יראה לכם כל מקום שצריך לעדכן.If you add PAUSED to the enum, javac shows you every place to update.
מסך לפי מצבA screen per statethe switch expression does not cover all possible input values:the switch expression does not cover all possible input values:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמשחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score
הצעד של היום בפרויקט מדףToday's step in the Shelf projectהשחקן לחץ 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כלום: האירוע לא שייך למצב הזהNothing: the event doesn't belong to this state
נכון.Right.למה 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.javac בודק שאין שגיאת כתיב ושה-switch מכסה הכולjavac checks there's no typo and the switch covers everything
נכון.Right.מתי שומרים שיא לקובץ?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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.רק כשהתוצאה החדשה גבוהה מהשמורהOnly when the new result beats the saved one
נכון.Right.מעברים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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מסכיםScreens
screen(state, score) עם switch בחצים מחזיר את הטקסט של כל מסך.screen(state, score) with an arrow switch returns each screen's text.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שיא בקובץ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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: מצב שנשכחBug hunter: a forgotten state
music(state) לא מכסה את GAME_OVER, ו-javac עוצר.music(state) doesn't cover GAME_OVER, and javac stops.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score
- הוסיפו ניקוד:Add scoring:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחסנית מסכיםA stack of screens
- משחק → השהיה → הגדרות:Game → pause → settings:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צלילים לפי אירועים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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.עקבו בסימולציה אחרי state.Follow state in the simulation.
מעבריםTransitionsState.values() עובר על כל המצבים.State.values() goes over every state.
מסכיםScreensמתחילים מ-deleteIfExists כדי שכל הרצה תתחיל נקי.Start with deleteIfExists so every run starts clean.
שיא בקובץA high score in a filecase GAME_OVER -> "sad.wav";case GAME_OVER -> "sad.wav";
צייד באגים: מצב שנשכחBug hunter: a forgotten stateבכניסה ל-PLAYING מאפסים חיים וניקוד, אבל לא את best.On entering PLAYING reset lives and score, but not best.
משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high scoreArrayDeque עם push, pop, peek.An ArrayDeque with push, pop, peek.
מחסנית מסכיםA stack of screensמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.