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

חזרה למפגשBack
JavaJava

קלט מקלדת ועכברKeyboard & mouse input

מפגש 44: KeyListener, מצב מקשים לתנועה חלקה, עכבר, ומהירות שלא תלויה במחשב (delta time).Session 44: KeyListener, key state for smooth movement, the mouse, and speed that doesn't depend on the computer (delta time).

מה נעשה היוםToday's plan

  • מונחיםTerms
  • חללית שזזה עם המקלדתA spaceship moved by the keyboard
  • מצב מקשים בלי חלוןKey state without a window
  • delta time: אותו מרחק בכל מחשבdelta time: the same distance on every computer
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • 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.
  • delta time: הזמן (בשניות) מאז הפריים הקודם.delta time: The time (in seconds) since the previous frame.
  • cooldown: זמן המתנה אחרי פעולה (כמו ירייה) לפני שמותר שוב.cooldown: A wait after an action (like a shot) before it's allowed again.
נושאTopic 2

חללית שזזה עם המקלדתA spaceship moved by the keyboard

חללית שזזה עם המקלדתA spaceship moved by the keyboard

  • 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.
  • נסו שני חצים יחד: אלכסון.Try two arrows together: a diagonal.
נושאTopic 3

מצב מקשים בלי חלוןKey state without a window

מצב מקשים בלי חלוןKey state without a window

  • הלוגיקה לבד: 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.
נושאTopic 4

delta time: אותו מרחק בכל מחשבdelta time: the same distance on every computer

delta time: אותו מרחק בכל מחשבdelta time: the same distance on every computer

  • שנייה אחת ב-30 פריימים או ב-144: עם speed * dt מגיעים לאותו מקום.One second at 30 frames or at 144: with speed * dt you end up in the same place.
נושאTopic 5

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • משחק 2: חללית שיורהGame 2: a ship that shoots

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.

delta time: הזמן (בשניות) מאז הפריים הקודם.delta time: The time (in seconds) since the previous frame.

cooldown: זמן המתנה אחרי פעולה (כמו ירייה) לפני שמותר שוב.cooldown: A wait after an action (like a shot) before it's allowed again.

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.

נסו שני חצים יחד: אלכסון.Try two arrows together: a diagonal.

הלוגיקה לבד: 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

למה לא להזיז את השחקן ישר בתוך keyPressed?Why not move the player right inside keyPressed?

  • כי האירוע חוזר בקפיצות ולא בכל פריים, והתנועה לא חלקהBecause the event repeats in jumps, not every frame, so movement isn't smooth
  • כי זה אסורBecause it's forbidden
  • כי זה מהיר מדיBecause it's too fast
  • אין בעיהNo problem

כי האירוע חוזר בקפיצות ולא בכל פריים, והתנועה לא חלקהBecause the event repeats in jumps, not every frame, so movement isn't smooth

במחשב איטי יש 30 פריימים בשנייה, ובמהיר 120. עם delta time, מי מגיע רחוק יותר בשנייה?A slow computer gets 30 frames a second, a fast one 120. With delta time, who goes further in a second?

  • שניהם אותו מרחקBoth the same distance
  • המהירThe fast one
  • האיטיThe slow one
  • תלוי במקלדתIt depends on the keyboard

שניהם אותו מרחקBoth the same distance

מה צריך כדי ש-JPanel יקבל אירועי מקלדת?What does a JPanel need to get keyboard events?

  • setFocusable(true) ו-KeyListenersetFocusable(true) and a KeyListener
  • רק repaint()Only repaint()
  • TimerA Timer
  • שום דברNothing

setFocusable(true) ו-KeyListenersetFocusable(true) and a KeyListener

שמאלה ולמטהLeft and down

  • עם Input ו-Ship מהתזכורת:With Input and Ship from the refresher:

אותו מרחקThe same distance

  • travel(fps, speed, seconds) מזיזה עם speed * dt בכל פריים.travel(fps, speed, seconds) moves by speed * dt each frame.

לא לצאת מהמסךDon't leave the screen

  • clamp(v, min, max) משאירה ערך בתוך טווח.clamp(v, min, max) keeps a value within a range.

צייד באגים: תנועה רק בלחיצהBug hunter: moving only on press

  • התוכנית מזיזה רק ברגע הלחיצה, ולכן שלושה טיקים עם מקש לחוץ נותנים רק 10.The program moves only at the moment of the press, so three ticks with the key down give only 10.

משחק 2: חללית שיורהGame 2: a ship that shoots

  • Gun עם cooldown של 0.3 שניות:Gun with a 0.3-second cooldown:

לנוע לעבר הקליקMoving toward the click

  • הקלקה ב-(30, 40).A click at (30, 40).

אלכסון לא מהיר יותרA diagonal isn't faster

  • בלי נרמול, ימינה ולמטה יחד זז 141 במקום 100.Without normalising, right and down together move 141 instead of 100.

in.left = true; ואחר כך in.left = false; in.down = true;in.left = true; then in.left = false; in.down = true;

dt = 1.0 / fpsdt = 1.0 / fps

Math.max(min, Math.min(max, v))Math.max(min, Math.min(max, v))

שלושה טיקים בזמן שהמקש לחוץ = 3 צעדים.Three ticks while the key is down = 3 steps.

מה למדנוWhat we learned

  • KeyListener: מקבל אירועים:KeyListener: Receives events:
  • keyPressed ו-keyReleased רק מעדכנים את קבוצת המקשים הלחוצים.keyPressed and keyReleased only update the set of keys that are down.
  • הלוגיקה לבד: Input הוא מצב המקשים, ו-Ship.update(in, dt) זזה לפיו.The logic alone: Input is the key state, and Ship.update(in, dt) moves by it.
  • שנייה אחת ב-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

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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