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

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

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

מחברים הכול למשחק שלם, אורזים ל-jar שרץ בלחיצה כפולה, ויוצרים מתקין עם jpackage.

Putting it all into a full game, packing a jar that runs on double-click, and making an installer with jpackage.

או Ctrl+Por Ctrl+P

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

  • 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).
  • כל מה שבנינו: 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

מונחיםTerms

  • seedseed: new Random(42) מייצר תמיד את אותה סדרה "אקראית". במשחק אמיתי בלי seed; בבדיקות עם seed, כדי שהתוצאה תהיה קבועה.new Random(42) always produces the same "random" sequence. In the real game no seed; in tests with a seed, so the result is fixed.
  • jarjar: קובץ zip עם המחלקות המהודרות וקבצי המשאבים (תמונות, צלילים). jar --create --file game.jar --main-class Main -C out . ו-java -jar game.jar.A zip file with the compiled classes and resource files (images, sounds). jar --create --file game.jar --main-class Main -C out . and java -jar game.jar.
  • manifestmanifest: קובץ META-INF/MANIFEST.MF בתוך ה-jar. השורה Main-Class: Main אומרת ל-java -jar מאיפה להתחיל.The META-INF/MANIFEST.MF file inside the jar. The line Main-Class: Main tells java -jar where to start.
  • jpackagejpackage: כלי ב-JDK שאורז את ה-jar יחד עם Java עצמה לתוכנה רגילה (app ב-Mac, exe או msi ב-Windows). מי שמקבל לא צריך להתקין Java.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). Whoever gets it doesn't need to install Java.
  • seedseed: new Random(42) מייצר תמיד את אותה סדרה "אקראית". במשחק אמיתי בלי seed; בבדיקות עם seed, כדי שהתוצאה תהיה קבועה.new Random(42) always produces the same "random" sequence. In the real game no seed; in tests with a seed, so the result is fixed.
  • jarjar: קובץ zip עם המחלקות המהודרות וקבצי המשאבים (תמונות, צלילים). jar --create --file game.jar --main-class Main -C out . ו-java -jar game.jar.A zip file with the compiled classes and resource files (images, sounds). jar --create --file game.jar --main-class Main -C out . and java -jar game.jar.
  • manifestmanifest: קובץ META-INF/MANIFEST.MF בתוך ה-jar. השורה Main-Class: Main אומרת ל-java -jar מאיפה להתחיל.The META-INF/MANIFEST.MF file inside the jar. The line Main-Class: Main tells java -jar where to start.
  • jpackagejpackage: כלי ב-JDK שאורז את ה-jar יחד עם Java עצמה לתוכנה רגילה (app ב-Mac, exe או msi ב-Windows). מי שמקבל לא צריך להתקין Java.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). Whoever gets it doesn't need to install Java.

Cheatsheet

המשחק השלם, בלי מסךThe full game, with no screen
Game g = new Game(42);
g.start();
for (int tick = 0; tick < 400 && g.state == State.PLAYING; tick++) {
    g.input.left = tick % 40 < 20;
    g.input.right = !g.input.left;
    g.update(0.05);
}
System.out.println(g.state + " score=" + g.score + " lives=" + g.lives);
הגדרות מקובץSettings from a file
String file = "title=Star Catcher\nwidth=400\nheight=300\n# comment\nsound=on\n";
Properties p = new Properties();
p.load(new StringReader(file));
int w = Integer.parseInt(p.getProperty("width"));
int h = Integer.parseInt(p.getProperty("height"));
System.out.println(p.getProperty("title") + " " + w + "x" + h + " sound=" + p.getProperty("sound") + " fps=" + p.getProperty("fps", "60"));
גרסה של המשחקThe game's version
System.out.println(compare("1.2.0", "1.10.0") + " " + compare("2.0.0", "1.9.9") + " " + compare("1.0.1", "1.0.1"));

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

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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