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

JavaJava
43
חלקPart 8: משחקים ב-JavaGames in Java כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

חלון ראשון: Swing, ציור ו-game loopFirst window: Swing, drawing & the game loop

JFrame ו-JPanel, ציור עם Graphics2D, וה-game loop: עדכון, ציור, המתנה.

JFrame and JPanel, drawing with Graphics2D, and the game loop: update, draw, wait.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. באיזה כיוון y גדל בציור של Java?Which way does y grow in Java drawing?

2. מה שני השלבים שחוזרים בכל סיבוב של לולאת משחק?Which two steps repeat in every round of a game loop?

3. למה להפריד את הלוגיקה מהציור?Why separate the logic from the drawing?

תרגיליםExercises

תרגילExercise 1

שלושה טיקיםThree ticks

כדור ב-x=0 עם מהירות 5. בלולאה של 3 טיקים הוסיפו את המהירות ל-x והדפיסו tick 1 x=5, tick 2 x=10, tick 3 x=15.

A ball at x=0 with speed 5. In a loop of 3 ticks add the speed to x and print tick 1 x=5, tick 2 x=10, tick 3 x=15.

איך הפלט אמור להיראות?What should the output look like?
tick 1 x=5
tick 2 x=10
tick 3 x=15
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: x += vx;x += vx;
לפתרוןSolution
תרגילExercise 2

קירA wall

Ball.update(w, h) מזיזה, ומהפכת את vx כש-x מגיע ל-0 או ל-w (אותו דבר ל-y). כדור ב-80 עם מהירות 15 במשטח 100: הדפיסו 95 110 95 80.

Ball.update(w, h) moves, and flips vx when x reaches 0 or w (the same for y). A ball at 80 with speed 15 on a 100-wide field: print 95 110 95 80.

איך הפלט אמור להיראות?What should the output look like?
95 110 95 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: הכדור עובר מעט את הקיר (110) ורק אז חוזר: כך עובדת בדיקה אחרי הזזה.The ball passes the wall a little (110) and only then returns: that's how checking after moving works.
לפתרוןSolution
תרגילExercise 3

מסך של תוויםA screen of characters

render(x, width) מחזירה שורה כמו |.o..|. כדור על 4 משבצות זז ימינה ושמאלה 6 טיקים. הדפיסו את 6 ה"פריימים": |o...|, |.o..|, |..o.|, |...o|, |..o.|, |.o..|.

render(x, width) returns a line like |.o..|. A ball on 4 cells moves right and left for 6 ticks. Print the 6 "frames": |o...|, |.o..|, |..o.|, |...o|, |..o.|, |.o..|.

איך הפלט אמור להיראות?What should the output look like?
|o...|
|.o..|
|..o.|
|...o|
|..o.|
|.o..|
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: כל טיק: מציירים, מזיזים, בודקים קיר.Each tick: draw, move, check the wall.
לפתרוןSolution
תרגילExercise 4

צייד באגים: כדור בורחBug hunter: an escaping ball

הכדור מתחיל ב-90 עם מהירות 4, והבדיקה היא x == 100. הוא קופץ מ-98 ל-102 ולא נוגע בקיר לעולם, ובורח מהמסך. תקנו כך שיודפס 94 98 102 98 94 90.

The ball starts at 90 with speed 4, and the check is x == 100. It jumps from 98 to 102 and never touches the wall, escaping the screen. Fix it so it prints 94 98 102 98 94 90.

איך הפלט אמור להיראות?What should the output look like?
94 98 102 98 94 90
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: >= במקום ==.>= instead of ==.
לפתרוןSolution
תרגילExercise 5

משחק 1: כדורים קופציםGame 1: bouncing balls

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

שלושה כדורים במשטח 100x100. update מחזירה true כשהכדור נגע בקיר. אחרי 10 טיקים הדפיסו את מספר ההתנגשויות ואת המיקום של כל כדור. זו הלוגיקה של המשחק; במחשב שלכם חברו אותה לחלון מהתזכורת הראשונה.

Three balls on a 100x100 field. update returns true when the ball hit a wall. After 10 ticks print the number of bounces and each ball's position. This is the game's logic; on your computer, connect it to the window from the first refresher.

איך הפלט אמור להיראות?What should the output look like?
after 10 ticks: 2 bounces
50,40
0,80
48,70
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: for (Ball b : balls) if (b.update(100, 100)) bounces++;for (Ball b : balls) if (b.update(100, 100)) bounces++;
לפתרוןSolution

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

אתגרChallenge 1

כוח משיכהGravity

כדור נופל: בכל טיק vy += 2, y += vy. כשהוא פוגע ברצפה (100), שמים אותו על הרצפה ומחזירים אותו למעלה עם 80% מהמהירות. הדפיסו את שלוש הקפיצות הראשונות (bounce 1 at tick 9, up speed 16...).

A falling ball: each tick vy += 2, y += vy. When it hits the floor (100), put it on the floor and send it back up with 80% of the speed. Print the first three bounces (bounce 1 at tick 9, up speed 16...).

איך הפלט אמור להיראות?What should the output look like?
bounce 1 at tick 9, up speed 16
bounce 2 at tick 24, up speed 11
bounce 3 at tick 35, up speed 9
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: vy = -vy * 0.8;vy = -vy * 0.8;
לפתרוןSolution
אתגרChallenge 2

מסך דו-ממדיA 2D screen

מסך 8x4 של char, שני כדורים שזזים באלכסון וקופצים מהקירות. בכל טיק: ממלאים בנקודות, מציירים o לכל כדור, מזיזים, ומדפיסים את המסך ואחריו --. שני טיקים.

An 8x4 char screen, two balls moving diagonally and bouncing off the walls. Each tick: fill with dots, draw an o per ball, move, and print the screen followed by --. Two ticks.

איך הפלט אמור להיראות?What should the output look like?
........
.o......
......o.
........
--
........
........
..o.....
.....o..
--
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: java.util.Arrays.fill(row, '.')java.util.Arrays.fill(row, '.')
לפתרוןSolution

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