חלקPart 8: משחקים ב-JavaGames in Java
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
קלט מקלדת ועכברKeyboard & mouse input
KeyListener, מצב מקשים לתנועה חלקה, עכבר, ומהירות שלא תלויה במחשב (delta time).
KeyListener, key state for smooth movement, the mouse, and speed that doesn't depend on the computer (delta time).
הנקודות החשובותKey points
- KeyListener: מקבל אירועים:KeyListener: Receives events:
- מצב מקשים: במקום להזיז בתוך
keyPressed(שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם.key state: Instead of moving insidekeyPressed(which repeats in jumps), keep which keys are down, and move by them on every tick. keyPressedו-keyReleasedרק מעדכנים את קבוצת המקשים הלחוצים.keyPressedandkeyReleasedonly update the set of keys that are down.- ה-Timer מחשב dt ומזיז לפי המקשים.The Timer computes dt and moves by the keys.
- הלוגיקה לבד:
Inputהוא מצב המקשים, ו-Ship.update(in, dt)זזה לפיו.The logic alone:Inputis the key state, andShip.update(in, dt)moves by it. - שלושה טיקים ימינה של חצי שנייה במהירות 120 הם 180 פיקסלים.Three half-second ticks to the right at speed 120 are 180 pixels.
- שנייה אחת ב-30 פריימים או ב-144: עם
speed * dtמגיעים לאותו מקום.One second at 30 frames or at 144: withspeed * dtyou end up in the same place. - משחק 2: חללית שיורהGame 2: a ship that shoots
מונחיםTerms
- KeyListenerKeyListener: מקבל אירועים:
keyPressedכשלוחצים ו-keyReleasedכשעוזבים. ה-JPanel צריךsetFocusable(true)כדי לקבל אותם.Receives events:keyPressedon press andkeyReleasedon release. The JPanel needssetFocusable(true)to get them. - מצב מקשיםkey state: במקום להזיז בתוך
keyPressed(שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם. התנועה חלקה ואפשר כמה מקשים יחד.Instead of moving insidekeyPressed(which repeats in jumps), keep which keys are down, and move by them on every tick. Movement is smooth and several keys work together. - delta timedelta time: הזמן (בשניות) מאז הפריים הקודם. זזים
speed * dt, כך שבמחשב איטי (פחות פריימים) כל צעד גדול יותר, והמרחק בשנייה זהה.The time (in seconds) since the previous frame. Movespeed * dt, so on a slow computer (fewer frames) each step is bigger, and the distance per second is the same. - cooldowncooldown: זמן המתנה אחרי פעולה (כמו ירייה) לפני שמותר שוב. סופרים אותו אחורה עם
dt.A wait after an action (like a shot) before it's allowed again. Count it down withdt. - KeyListenerKeyListener: מקבל אירועים:
keyPressedכשלוחצים ו-keyReleasedכשעוזבים. ה-JPanel צריךsetFocusable(true)כדי לקבל אותם.Receives events:keyPressedon press andkeyReleasedon release. The JPanel needssetFocusable(true)to get them. - מצב מקשיםkey state: במקום להזיז בתוך
keyPressed(שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם. התנועה חלקה ואפשר כמה מקשים יחד.Instead of moving insidekeyPressed(which repeats in jumps), keep which keys are down, and move by them on every tick. Movement is smooth and several keys work together. - delta timedelta time: הזמן (בשניות) מאז הפריים הקודם. זזים
speed * dt, כך שבמחשב איטי (פחות פריימים) כל צעד גדול יותר, והמרחק בשנייה זהה.The time (in seconds) since the previous frame. Movespeed * dt, so on a slow computer (fewer frames) each step is bigger, and the distance per second is the same. - cooldowncooldown: זמן המתנה אחרי פעולה (כמו ירייה) לפני שמותר שוב. סופרים אותו אחורה עם
dt.A wait after an action (like a shot) before it's allowed again. Count it down withdt.
Cheatsheet
חללית שזזה עם המקלדתA spaceship moved by the keyboard
JFrame f = new JFrame("Spaceship");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Main());
f.pack();
f.setVisible(true);מצב מקשים בלי חלוןKey state without a window
Input in = new Input();
Ship s = new Ship(100, 100, 120);
in.right = true;
for (int i = 0; i < 3; i++) {
s.update(in, 0.5);
}
in.right = false;
in.up = true;
s.update(in, 0.5);
System.out.println(s.x + ", " + s.y);delta time: אותו מרחק בכל מחשבdelta time: the same distance on every computer
System.out.println("30 fps: " + travel(30, 120, 1) + ", 144 fps: " + travel(144, 120, 1));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|