נבנה על ידיBuilt by Ariel Yohimovich
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).
KeyListener, מצב מקשים לתנועה חלקה, עכבר, ומהירות שלא תלויה במחשב (delta time). בסוף המפגש מוסיפים צעד לפרויקט המלווה.KeyListener, key state for smooth movement, the mouse, and speed that doesn't depend on the computer (delta time). At the end we add a step to the companion project.מה נעשה היום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
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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.
keyPressed ו-keyReleased רק מעדכנים את קבוצת המקשים הלחוצים. ה-Timer מחשב dt ומזיז לפי המקשים. נסו שני חצים יחד: אלכסון.keyPressed and keyReleased only update the set of keys that are down. 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.
הלוגיקה לבד: Input הוא מצב המקשים, ו-Ship.update(in, dt) זזה לפיו. שלושה טיקים ימינה של חצי שנייה במהירות 120 הם 180 פיקסלים.The logic alone: Input is the key state, and Ship.update(in, dt) moves by it. 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.
שנייה אחת ב-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
Gun עם cooldown של 0.3 שניות: כל עוד fire לחוץ, יורה כדור מהמיקום של החללית כשה-cooldown נגמר, וכל כדור עולה 400 לשנייה. החללית זזה ימינה ב-150. אחרי 10 פריימים של 0.1: ship x=350, bullets=4 ו-first bullet y=-80.Gun with a 0.3-second cooldown: while fire is down, it shoots a bullet from the ship's position when the cooldown is over, and each bullet rises 400 per second. The ship moves right at 150. After 10 frames of 0.1: ship x=350, bullets=4 and first bullet y=-80.KeyListener: מקבל אירועים:KeyListener: Receives events:
המונחים של המפגשThis session's termsמצב מקשים: במקום להזיז בתוך keyPressed (שחוזר בקפיצות), שומרים אילו מקשים לחוצים, ובכל טיק זזים לפיהם.key state: Instead of moving inside keyPressed (which repeats in jumps), keep which keys are down, and move by them on every tick.
המונחים של המפגשThis session's termsdelta time: הזמן (בשניות) מאז הפריים הקודם.delta time: The time (in seconds) since the previous frame.
המונחים של המפגשThis session's termscooldown: זמן המתנה אחרי פעולה (כמו ירייה) לפני שמותר שוב.cooldown: A wait after an action (like a shot) before it's allowed again.
המונחים של המפגשThis session's termskeyPressed ו-keyReleased רק מעדכנים את קבוצת המקשים הלחוצים.keyPressed and keyReleased only update the set of keys that are down.
חללית שזזה עם המקלדתA spaceship moved by the keyboardה-Timer מחשב dt ומזיז לפי המקשים.The Timer computes dt and moves by the keys.
חללית שזזה עם המקלדתA spaceship moved by the keyboardנסו שני חצים יחד: אלכסון.Try two arrows together: a diagonal.
חללית שזזה עם המקלדתA spaceship moved by the keyboardהלוגיקה לבד: Input הוא מצב המקשים, ו-Ship.update(in, dt) זזה לפיו.The logic alone: Input is the key state, and Ship.update(in, dt) moves by it.
מצב מקשים בלי חלוןKey state without a windowשלושה טיקים ימינה של חצי שנייה במהירות 120 הם 180 פיקסלים.Three half-second ticks to the right at speed 120 are 180 pixels.
מצב מקשים בלי חלוןKey state without a windowשנייה אחת ב-30 פריימים או ב-144: עם speed * dt מגיעים לאותו מקום.One second at 30 frames or at 144: with speed * dt you end up in the same place.
delta time: אותו מרחק בכל מחשבdelta time: the same distance on every computerמשחק 2: חללית שיורהGame 2: a ship that shoots
הצעד של היום בפרויקט מדףToday's step in the Shelf projectלמה לא להזיז את השחקן ישר בתוך 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כי האירוע חוזר בקפיצות ולא בכל פריים, והתנועה לא חלקהBecause the event repeats in jumps, not every frame, so movement isn't smooth
נכון.Right.במחשב איטי יש 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.שניהם אותו מרחקBoth the same distance
נכון.Right.מה צריך כדי ש-JPanel יקבל אירועי מקלדת?What does a JPanel need to get keyboard events?
setFocusable(true) ו-KeyListenersetFocusable(true) and a KeyListener- רק
repaint()Only repaint() - TimerA Timer
- שום דברNothing
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.setFocusable(true) ו-KeyListenersetFocusable(true) and a KeyListener
נכון.Right.שמאלה ולמטהLeft and down
- עם
Input ו-Ship מהתזכורת:With Input and Ship from the refresher:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.אותו מרחקThe same distance
travel(fps, speed, seconds) מזיזה עם speed * dt בכל פריים.travel(fps, speed, seconds) moves by speed * dt each frame.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.לא לצאת מהמסךDon't leave the screen
clamp(v, min, max) משאירה ערך בתוך טווח.clamp(v, min, max) keeps a value within a range.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: תנועה רק בלחיצה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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משחק 2: חללית שיורהGame 2: a ship that shoots
Gun עם cooldown של 0.3 שניות:Gun with a 0.3-second cooldown:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.לנוע לעבר הקליקMoving toward the click
- הקלקה ב-(30, 40).A click at (30, 40).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.אלכסון לא מהיר יותרA diagonal isn't faster
- בלי נרמול, ימינה ולמטה יחד זז 141 במקום 100.Without normalising, right and down together move 141 instead of 100.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.in.left = true; ואחר כך in.left = false; in.down = true;in.left = true; then in.left = false; in.down = true;
שמאלה ולמטהLeft and downdt = 1.0 / fpsdt = 1.0 / fps
אותו מרחקThe same distanceMath.max(min, Math.min(max, v))Math.max(min, Math.min(max, v))
לא לצאת מהמסךDon't leave the screenשלושה טיקים בזמן שהמקש לחוץ = 3 צעדים.Three ticks while the key is down = 3 steps.
צייד באגים: תנועה רק בלחיצהBug hunter: moving only on pressמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.