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

JavaJava
44
חלק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).

או Ctrl+Por Ctrl+P

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

  • KeyListener: מקבל אירועים:KeyListener: Receives events:
  • מצב מקשים: במקום להזיז בתוך keyPressed (שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם.key state: Instead of moving inside keyPressed (which repeats in jumps), keep which keys are down, and move by them on every tick.
  • keyPressed ו-keyReleased רק מעדכנים את קבוצת המקשים הלחוצים.keyPressed and keyReleased only 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: Input is the key state, and Ship.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: with speed * dt you end up in the same place.
  • משחק 2: חללית שיורהGame 2: a ship that shoots

מונחיםTerms

  • KeyListenerKeyListener: מקבל אירועים: keyPressed כשלוחצים ו-keyReleased כשעוזבים. ה-JPanel צריך setFocusable(true) כדי לקבל אותם.Receives events: keyPressed on press and keyReleased on release. The JPanel needs setFocusable(true) to get them.
  • מצב מקשיםkey state: במקום להזיז בתוך keyPressed (שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם. התנועה חלקה ואפשר כמה מקשים יחד.Instead of moving inside keyPressed (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. Move speed * 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 with dt.
  • KeyListenerKeyListener: מקבל אירועים: keyPressed כשלוחצים ו-keyReleased כשעוזבים. ה-JPanel צריך setFocusable(true) כדי לקבל אותם.Receives events: keyPressed on press and keyReleased on release. The JPanel needs setFocusable(true) to get them.
  • מצב מקשיםkey state: במקום להזיז בתוך keyPressed (שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם. התנועה חלקה ואפשר כמה מקשים יחד.Instead of moving inside keyPressed (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. Move speed * 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 with dt.

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

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