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

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

התנגשויות ו-spritesCollisions & sprites

מלבנים ועיגולים שמתנגשים, sprites מתמונות, אנימציה מפריימים וניקוד.

Rectangles and circles that collide, sprites from images, frame animation and scoring.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. שני מלבנים חופפים בציר x אבל לא בציר y. מתנגשים?Two rectangles overlap on x but not on y. Do they collide?

2. למה משווים dx*dx + dy*dy ל-r*r ולא מחשבים שורש?Why compare dx*dx + dy*dy with r*r instead of taking a square root?

3. מוחקים מרשימה בזמן לולאה עם אינדקס. מאיזה כיוון כדאי לרוץ?Removing from a list in an index loop. Which direction should you run?

תרגיליםExercises

תרגילExercise 1

מלבניםRectangles

עם Box.intersects מהתזכורת: השחקן (10,10,20,20) מול (29,5,10,10) ומול (30,30,5,5). הדפיסו true false.

With Box.intersects from the refresher: the player (10,10,20,20) against (29,5,10,10) and (30,30,5,5). Print true false.

איך הפלט אמור להיראות?What should the output look like?
true false
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=30; (30,30) מתחיל בדיוק שם, ולכן לא חופף.The player ends at x=30; (30,30) starts exactly there, so no overlap.
לפתרוןSolution
תרגילExercise 2

עיגוליםCircles

circles(x1, y1, r1, x2, y2, r2): מתנגשים אם המרחק בין המרכזים ≤ סכום הרדיוסים. בין (0,0) ל-(6,8) המרחק 10: עם רדיוסים 5+5 true, ועם 5+4 false.

circles(x1, y1, r1, x2, y2, r2): they collide if the distance between centres ≤ the sum of radii. Between (0,0) and (6,8) the distance is 10: with radii 5+5 true, with 5+4 false.

איך הפלט אמור להיראות?What should the output look like?
true false
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.sqrt: משווים ריבועים.No Math.sqrt: compare squares.
לפתרוןSolution
תרגילExercise 3

פריים לפי זמןA frame by time

3 פריימים של 0.2 שנייה. הדפיסו את הפריים בזמנים 0, 0.25, 0.5, 0.75: 0 1 2 0.

3 frames of 0.2 seconds. Print the frame at times 0, 0.25, 0.5, 0.75: 0 1 2 0.

איך הפלט אמור להיראות?What should the output look like?
0 1 2 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: (int) (time / frameSeconds) % frames(int) (time / frameSeconds) % frames
לפתרוןSolution
תרגילExercise 4

צייד באגים: נגיעה בקצהBug hunter: touching at the edge

שני מלבנים שרק נוגעים בקצה (אחד נגמר ב-10 והשני מתחיל ב-10) נחשבים כאן התנגשות, כי הבדיקה משתמשת ב-<=. תקנו כך שיודפס false true.

Two rectangles that only touch at the edge (one ends at 10, the other starts at 10) count as a collision here, because the check uses <=. Fix it so it prints false true.

איך הפלט אמור להיראות?What should the output look like?
false true
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 <=: touching isn't overlapping.
לפתרוןSolution
תרגילExercise 5

משחק 3: לתפוס כוכביםGame 3: catch the stars

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

שלושה כוכבים נופלים 10 בכל טיק. כוכב שנוגע במשוט (40,90,30,5) נותן 10 נקודות ונעלם; כוכב שעבר את 100 נחשב פספוס ונעלם. אחרי 12 טיקים: score 20, missed 1, left 0.

Three stars fall 10 per tick. A star touching the paddle (40,90,30,5) gives 10 points and disappears; a star past 100 counts as missed and disappears. After 12 ticks: score 20, missed 1, left 0.

איך הפלט אמור להיראות?What should the output look like?
score 20, missed 1, left 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: לולאה מהסוף להתחלה, כי מוחקים תוך כדי.Loop from the end to the start, because you remove as you go.
לפתרוןSolution

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

אתגרChallenge 1

תא ב-sprite sheetA cell in a sprite sheet

sprite sheet עם 4 עמודות של 32 פיקסלים. cell(index, columns, size) מחזירה את המלבן של הפריים. הדפיסו לפריימים 0, 3, 5: Cell[x=0, y=0, w=32, h=32], Cell[x=96, y=0, ...], Cell[x=32, y=32, ...].

A sprite sheet with 4 columns of 32 pixels. cell(index, columns, size) returns the frame's rectangle. Print for frames 0, 3, 5: Cell[x=0, y=0, w=32, h=32], Cell[x=96, y=0, ...], Cell[x=32, y=32, ...].

איך הפלט אמור להיראות?What should the output look like?
Cell[x=0, y=0, w=32, h=32]
Cell[x=96, y=0, w=32, h=32]
Cell[x=32, y=32, w=32, h=32]
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: עמודה index % columns, שורה index / columns.Column index % columns, row index / columns.
לפתרוןSolution
אתגרChallenge 2

רשת חוסכת בדיקותA grid saves checks

5 עצמים. בדיקה נאיבית של כל זוג היא 10 בדיקות. עם רשת של תאים בגודל 50, בודקים רק זוגות באותו תא. הדפיסו naive 10 checks, grid 2 checks.

5 objects. A naive check of every pair is 10 checks. With a grid of 50-sized cells, check only pairs in the same cell. Print naive 10 checks, grid 2 checks.

איך הפלט אמור להיראות?What should the output look like?
naive 10 checks, grid 2 checks
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 / 50) + "," + (y / 50).The cell key: (x / 50) + "," + (y / 50).
לפתרוןSolution

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