נבנה על ידי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).

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

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

2. במחשב איטי יש 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?

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

תרגיליםExercises

תרגילExercise 1

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

עם Input ו-Ship מהתזכורת: שלושה טיקים של חצי שנייה שמאלה, ואז טיק אחד למטה, במהירות 120 מ-(100, 100). הדפיסו -80.0, 160.0.

With Input and Ship from the refresher: three half-second ticks to the left, then one tick down, at speed 120 from (100, 100). Print -80.0, 160.0.

איך הפלט אמור להיראות?What should the output look like?
-80.0, 160.0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: in.left = true; ואחר כך in.left = false; in.down = true;in.left = true; then in.left = false; in.down = true;
לפתרוןSolution
תרגילExercise 2

אותו מרחקThe same distance

travel(fps, speed, seconds) מזיזה עם speed * dt בכל פריים. שתי שניות במהירות 200: ב-60 fps וב-20 fps. הדפיסו 60 fps: 400.0, 20 fps: 400.0.

travel(fps, speed, seconds) moves by speed * dt each frame. Two seconds at speed 200: at 60 fps and at 20 fps. Print 60 fps: 400.0, 20 fps: 400.0.

איך הפלט אמור להיראות?What should the output look like?
60 fps: 400.0, 20 fps: 400.0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: dt = 1.0 / fpsdt = 1.0 / fps
לפתרוןSolution
תרגילExercise 3

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

clamp(v, min, max) משאירה ערך בתוך טווח. חללית ב-380 זזה ימינה במהירות 200, טיקים של 0.1. אחרי כל טיק מגבילים ל-0..400. הדפיסו 400 400 400 400 400.

clamp(v, min, max) keeps a value within a range. A ship at 380 moves right at speed 200, ticks of 0.1. After each tick clamp to 0..400. Print 400 400 400 400 400.

איך הפלט אמור להיראות?What should the output look like?
400 400 400 400 400
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: Math.max(min, Math.min(max, v))Math.max(min, Math.min(max, v))
לפתרוןSolution
תרגילExercise 4

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

התוכנית מזיזה רק ברגע הלחיצה, ולכן שלושה טיקים עם מקש לחוץ נותנים רק 10. תקנו עם מצב מקש (right): לחיצה ועזיבה רק משנות את המצב, וכל tick זז אם המקש לחוץ. הפלט: x = 30.0.

The program moves only at the moment of the press, so three ticks with the key down give only 10. Fix it with key state (right): press and release only change the state, and each tick moves if the key is down. The output: x = 30.0.

איך הפלט אמור להיראות?What should the output look like?
x = 30.0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: שלושה טיקים בזמן שהמקש לחוץ = 3 צעדים.Three ticks while the key is down = 3 steps.
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

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.

איך הפלט אמור להיראות?What should the output look like?
ship x=350, bullets=4
first bullet y=-80
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: סופרים את wait אחורה עם dt, ויורים כש-wait <= 0.Count wait down with dt, and shoot when wait <= 0.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

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

הקלקה ב-(30, 40). בכל טיק זזים 10 לכיוונה (וקטור מנורמל), ועוצרים כשמגיעים. הדפיסו 6 מיקומים (6,8, 12,16... עד 30,40) ואת הזווית angle 53 (atan2).

A click at (30, 40). Each tick move 10 toward it (a normalised vector), stopping on arrival. Print 6 positions (6,8, 12,16... up to 30,40) and the angle angle 53 (atan2).

איך הפלט אמור להיראות?What should the output look like?
6,8
12,16
18,24
24,32
30,40
30,40
angle 53
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: dx / dist * speeddx / dist * speed
לפתרוןSolution
אתגרChallenge 2

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

בלי נרמול, ימינה ולמטה יחד זז 141 במקום 100. move(right, down, speed) מנרמלת את הכיוון. הדפיסו 100 100.

Without normalising, right and down together move 141 instead of 100. move(right, down, speed) normalises the direction. Print 100 100.

איך הפלט אמור להיראות?What should the output look like?
100 100
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: מחלקים באורך הווקטור Math.hypot(dx, dy).Divide by the vector's length Math.hypot(dx, dy).
לפתרוןSolution

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