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

חזרה למפגשBack
JavaJava

משחק שלם ואריזה ל-jar ו-jpackageA full game, packed with jar & jpackage

מפגש 48: מחברים הכול למשחק שלם, אורזים ל-jar שרץ בלחיצה כפולה, ויוצרים מתקין עם jpackage.Session 48: Putting it all into a full game, packing a jar that runs on double-click, and making an installer with jpackage.

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

  • מונחיםTerms
  • המשחק השלם, בלי מסךThe full game, with no screen
  • הגדרות מקובץSettings from a file
  • גרסה של המשחקThe game's version
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

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

  • seed: new Random(42) מייצר תמיד את אותה סדרה "אקראית".seed: new Random(42) always produces the same "random" sequence.
  • jar: קובץ zip עם המחלקות המהודרות וקבצי המשאבים (תמונות, צלילים).jar: A zip file with the compiled classes and resource files (images, sounds).
  • manifest: קובץ META-INF/MANIFEST.MF בתוך ה-jar.manifest: The META-INF/MANIFEST.MF file inside the jar.
  • jpackage: כלי ב-JDK שאורז את ה-jar יחד עם Java עצמה לתוכנה רגילה (app ב-Mac, exe או msi ב-Windows).jpackage: A JDK tool that packs the jar together with Java itself into a regular app (an app on Mac, an exe or msi on Windows).
נושאTopic 2

המשחק השלם, בלי מסךThe full game, with no screen

המשחק השלם, בלי מסךThe full game, with no screen

  • כל מה שבנינו: State, Input, משוט שזז עם dt ונשאר במסך, כוכבים שנופלים, התנגשות, ניקוד וחיים.Everything we built: State, Input, a paddle moving with dt and staying on screen, falling stars, collisions, score and lives.
  • עם seed קבוע (42) ו"שחקן" אוטומטי שזז הלוך ושוב, התוצאה זהה בכל הרצה, ולכן אפשר לבדוק אותה.With a fixed seed (42) and an automatic "player" moving back and forth, the result is the same every run, so it can be tested.
נושאTopic 3

הגדרות מקובץSettings from a file

הגדרות מקובץSettings from a file

  • Properties קורא קובץ key=value ומדלג על הערות.Properties reads a key=value file and skips comments.
  • getProperty(key, default) נותן ברירת מחדל כשהמפתח חסר, כך שהמשחק עובד גם בלי קובץ.getProperty(key, default) gives a default when the key is missing, so the game works even without a file.
נושאTopic 4

גרסה של המשחקThe game's version

גרסה של המשחקThe game's version

  • גרסאות נכתבות major.minor.patch ומשווים אותן כמספרים, לא כטקסט: 1.10.0 חדשה מ-1.2.0.Versions are written major.minor.patch and compared as numbers, not text: 1.10.0 is newer than 1.2.0.
  • את הגרסה שמים ב-manifest כ-Implementation-Version.The version goes in the manifest as Implementation-Version.
נושאTopic 5

פרויקט מדףShelf project

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

  • משחק 6: המשחק השלםGame 6: the full game

seed: new Random(42) מייצר תמיד את אותה סדרה "אקראית".seed: new Random(42) always produces the same "random" sequence.

jar: קובץ zip עם המחלקות המהודרות וקבצי המשאבים (תמונות, צלילים).jar: A zip file with the compiled classes and resource files (images, sounds).

manifest: קובץ META-INF/MANIFEST.MF בתוך ה-jar.manifest: The META-INF/MANIFEST.MF file inside the jar.

jpackage: כלי ב-JDK שאורז את ה-jar יחד עם Java עצמה לתוכנה רגילה (app ב-Mac, exe או msi ב-Windows).jpackage: A JDK tool that packs the jar together with Java itself into a regular app (an app on Mac, an exe or msi on Windows).

כל מה שבנינו: State, Input, משוט שזז עם dt ונשאר במסך, כוכבים שנופלים, התנגשות, ניקוד וחיים.Everything we built: State, Input, a paddle moving with dt and staying on screen, falling stars, collisions, score and lives.

עם seed קבוע (42) ו"שחקן" אוטומטי שזז הלוך ושוב, התוצאה זהה בכל הרצה, ולכן אפשר לבדוק אותה.With a fixed seed (42) and an automatic "player" moving back and forth, the result is the same every run, so it can be tested.

Properties קורא קובץ key=value ומדלג על הערות.Properties reads a key=value file and skips comments.

getProperty(key, default) נותן ברירת מחדל כשהמפתח חסר, כך שהמשחק עובד גם בלי קובץ.getProperty(key, default) gives a default when the key is missing, so the game works even without a file.

גרסאות נכתבות major.minor.patch ומשווים אותן כמספרים, לא כטקסט: 1.10.0 חדשה מ-1.2.0.Versions are written major.minor.patch and compared as numbers, not text: 1.10.0 is newer than 1.2.0.

את הגרסה שמים ב-manifest כ-Implementation-Version.The version goes in the manifest as Implementation-Version.

משחק 6: המשחק השלםGame 6: the full game

מה עושה Main-Class ב-manifest?What does Main-Class in the manifest do?

  • אומר ל-java -jar איזו מחלקה להריץTells java -jar which class to run
  • מכווץ את ה-jarCompresses the jar
  • מתקין JavaInstalls Java
  • לא עושה כלוםNothing

אומר ל-java -jar איזו מחלקה להריץTells java -jar which class to run

למה new Random(42) בבדיקות?Why new Random(42) in tests?

  • כדי שהאקראיות תחזור על עצמה והתוצאה תהיה קבועהSo the randomness repeats and the result is fixed
  • כי 42 מזלBecause 42 is lucky
  • זה מהיר יותרIt's faster
  • אסור Random בלי seedRandom without a seed is forbidden

כדי שהאקראיות תחזור על עצמה והתוצאה תהיה קבועהSo the randomness repeats and the result is fixed

מה היתרון של jpackage על פני jar?What does jpackage give over a jar?

  • Java ארוזה בפנים: לא צריך להתקין JavaJava is packed inside: no need to install Java
  • הקוד מהיר יותרThe code is faster
  • אין צורך ב-mainNo need for main
  • אין הבדלNo difference

Java ארוזה בפנים: לא צריך להתקין JavaJava is packed inside: no need to install Java

manifestA manifest

  • manifest(mainClass, version) בונה את הטקסט של MANIFEST.MF:manifest(mainClass, version) builds the text of MANIFEST.MF:

הגדרות מקובץSettings from a file

  • Properties קורא קובץ key=value (ומדלג על הערות #).Properties reads a key=value file (skipping # comments).

משחק 6: המשחק השלםGame 6: the full game

  • כתבו את Game מהתזכורת:Write Game from the refresher:

השוואת גרסאותComparing versions

  • compare(a, b) משווה גרסאות major.minor.patch כמספרים (לא כטקסט:compare(a, b) compares major.minor.patch versions as numbers (not as text:

לארוז ולהריץPack and run

  • במחשב שלכם, בתיקיית המשחק:On your computer, in the game folder:

seed אחר, משחק אחרAnother seed, another game

  • הריצו את Game עם seed 7 ועם seed 42 (אותו שחקן אוטומטי), והדפיסו את שתי התוצאות.Run Game with seed 7 and with seed 42 (the same automatic player), and print both results.

ה-jar מבפניםInside the jar

  • הסתכלו בתוך ה-jar:Look inside the jar:

כל שורה מסתיימת ב-\n.Each line ends with \n.

p.getProperty("fps", "60")p.getProperty("fps", "60")

עם seed קבוע התוצאה זהה בכל הרצה.With a fixed seed the result is the same every run.

split("\\."): הנקודה מיוחדת ב-regex.split("\\."): the dot is special in a regex.

מה למדנוWhat we learned

  • seed: new Random(42) מייצר תמיד את אותה סדרה "אקראית".seed: new Random(42) always produces the same "random" sequence.
  • כל מה שבנינו: State, Input, משוט שזז עם dt ונשאר במסך, כוכבים שנופלים, התנגשות, ניקוד וחיים.Everything we built: State, Input, a paddle moving with dt and staying on screen, falling stars, collisions, score and lives.
  • Properties קורא קובץ key=value ומדלג על הערות.Properties reads a key=value file and skips comments.
  • גרסאות נכתבות major.minor.patch ומשווים אותן כמספרים, לא כטקסט: 1.10.0 חדשה מ-1.2.0.Versions are written major.minor.patch and compared as numbers, not text: 1.10.0 is newer than 1.2.0.
  • משחק 6: המשחק השלםGame 6: the full game

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

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

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