משחק שלם ואריזה ל-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.
01המונחים של המפגשThis session's terms
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.
Before the code, the words. Every term here is also linked in the course glossary.
- seed:
new Random(42)מייצר תמיד את אותה סדרה "אקראית". במשחק אמיתי בלי seed; בבדיקות עם 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. - jar: קובץ zip עם המחלקות המהודרות וקבצי המשאבים (תמונות, צלילים).
jar --create --file game.jar --main-class Main -C out .ו-java -jar game.jar.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. - manifest: קובץ
META-INF/MANIFEST.MFבתוך ה-jar. השורהMain-Class: Mainאומרת ל-java -jarמאיפה להתחיל.manifest: TheMETA-INF/MANIFEST.MFfile inside the jar. The lineMain-Class: Maintellsjava -jarwhere to start. - jpackage: כלי ב-JDK שאורז את ה-jar יחד עם Java עצמה לתוכנה רגילה (app ב-Mac, exe או msi ב-Windows). מי שמקבל לא צריך להתקין Java.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). Whoever gets it doesn't need to install Java.
02המשחק השלם, בלי מסךThe full game, with no screen
כל מה שבנינו: State, Input, משוט שזז עם dt ונשאר במסך, כוכבים שנופלים, התנגשות, ניקוד וחיים. עם seed קבוע (42) ו"שחקן" אוטומטי שזז הלוך ושוב, התוצאה זהה בכל הרצה, ולכן אפשר לבדוק אותה.
Everything we built: State, Input, a paddle moving with dt and staying on screen, falling stars, collisions, score and lives. 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.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
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);
}
}
enum State {
MENU, PLAYING, GAME_OVER
}
class Input {
boolean left, right;
}
class Game {
final Input input = new Input();
final java.util.Random rnd;
State state = State.MENU;
double paddleX = 200;
final List<double[]> stars = new ArrayList<>();
int score, lives;
double spawn = 0;
Game(long seed) {
rnd = new java.util.Random(seed);
}
void start() {
state = State.PLAYING;
score = 0;
lives = 3;
stars.clear();
}
void update(double dt) {
if (state != State.PLAYING) {
return;
}
if (input.left) {
paddleX -= 200 * dt;
}
if (input.right) {
paddleX += 200 * dt;
}
paddleX = Math.max(0, Math.min(360, paddleX));
spawn -= dt;
if (spawn <= 0) {
stars.add(new double[]{rnd.nextInt(390), 0});
spawn = 0.6;
}
for (int i = stars.size() - 1; i >= 0; i--) {
double[] s = stars.get(i);
s[1] += 120 * dt;
boolean caught = s[1] >= 270 && s[1] <= 290 && s[0] + 10 > paddleX && s[0] < paddleX + 40;
if (caught) {
score += 10;
stars.remove(i);
} else if (s[1] > 300) {
stars.remove(i);
if (--lives == 0) {
state = State.GAME_OVER;
}
}
}
}
}GAME_OVER score=30 lives=0
03הגדרות מקובץSettings from a file
Properties קורא קובץ key=value ומדלג על הערות. getProperty(key, default) נותן ברירת מחדל כשהמפתח חסר, כך שהמשחק עובד גם בלי קובץ.
Properties reads a key=value file and skips comments. getProperty(key, default) gives a default when the key is missing, so the game works even without a file.
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException {
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"));
}
}Star Catcher 400x300 sound=on fps=60
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
04גרסה של המשחקThe game's version
גרסאות נכתבות major.minor.patch ומשווים אותן כמספרים, לא כטקסט: 1.10.0 חדשה מ-1.2.0. את הגרסה שמים ב-manifest כ-Implementation-Version.
Versions are written major.minor.patch and compared as numbers, not text: 1.10.0 is newer than 1.2.0. The version goes in the manifest as Implementation-Version.
public class Main {
static int compare(String a, String b) {
String[] x = a.split("\\.");
String[] y = b.split("\\.");
for (int i = 0; i < 3; i++) {
int c = Integer.compare(Integer.parseInt(x[i]), Integer.parseInt(y[i]));
if (c != 0) {
return c;
}
}
return 0;
}
public static void main(String[] args) {
System.out.println(compare("1.2.0", "1.10.0") + " " + compare("2.0.0", "1.9.9") + " " + compare("1.0.1", "1.0.1"));
}
}-1 1 0
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
05הצעד של היום בפרויקט מדףToday's step in the Shelf project
כתבו את Game מהתזכורת: מצבים, קלט, משוט מוגבל למסך, כוכב חדש כל 0.6 שנייה (עם Random(seed)), נפילה, תפיסה (10 נקודות) ופספוס (חיים). הריצו עם seed 42 ושחקן אוטומטי עד Game Over, והדפיסו את התוצאה. בבית: חברו ל-JPanel מהמפגשים הקודמים.
Write Game from the refresher: states, input, a paddle kept on screen, a new star every 0.6 seconds (with Random(seed)), falling, catching (10 points) and missing (lives). Run with seed 42 and an automatic player until game over, and print the result. At home: connect it to the JPanel from earlier sessions.
את הצעד הזה כותבים בעמוד התרגול (התרגיל המסומן כצעד בפרויקט). בעמוד הפתרונות יש פתרון מלא עם סימולציה שמריצה אותו.
You write this step on the practice page (the exercise marked as a project step). The solutions page has a full solution with a simulation that runs it.
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.