חלק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.
הנקודות החשובות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 withdtand 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ומדלג על הערות.Propertiesreads akey=valuefile 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 writtenmajor.minor.patchand compared as numbers, not text:1.10.0is newer than1.2.0. - את הגרסה שמים ב-manifest כ-
Implementation-Version.The version goes in the manifest asImplementation-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 .andjava -jar game.jar. - manifestmanifest: קובץ
META-INF/MANIFEST.MFבתוך ה-jar. השורהMain-Class: Mainאומרת ל-java -jarמאיפה להתחיל.TheMETA-INF/MANIFEST.MFfile inside the jar. The lineMain-Class: Maintellsjava -jarwhere 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 .andjava -jar game.jar. - manifestmanifest: קובץ
META-INF/MANIFEST.MFבתוך ה-jar. השורהMain-Class: Mainאומרת ל-java -jarמאיפה להתחיל.TheMETA-INF/MANIFEST.MFfile inside the jar. The lineMain-Class: Maintellsjava -jarwhere 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 |
|---|