התנגשויות ו-spritesCollisions & sprites
מלבנים ועיגולים שמתנגשים, sprites מתמונות, אנימציה מפריימים וניקוד.
Rectangles and circles that collide, sprites from images, frame animation and scoring.
מלבניםRectangles
תזכורת: מה ביקשוReminder: the task
עם 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.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Box player = new Box(10, 10, 20, 20);
Box star = new Box(29, 5, 10, 10);
Box far = new Box(30, 30, 5, 5);
System.out.println(player.intersects(star) + " " + player.intersects(far));
}
}
record Box(int x, int y, int w, int h) {
boolean intersects(Box o) {
return x < o.x + o.w && o.x < x + w && y < o.y + o.h && o.y < y + h;
}
}true false
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
עיגוליםCircles
תזכורת: מה ביקשוReminder: the task
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.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
static boolean circles(double x1, double y1, double r1, double x2, double y2, double r2) {
double dx = x1 - x2, dy = y1 - y2;
double rr = r1 + r2;
return dx * dx + dy * dy <= rr * rr;
}
public static void main(String[] args) {
System.out.println(circles(0, 0, 5, 6, 8, 5) + " " + circles(0, 0, 5, 6, 8, 4));
}
}true false
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
פריים לפי זמןA frame by time
תזכורת: מה ביקשוReminder: the task
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.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
static int frame(double time, double frameSeconds, int frames) {
return (int) (time / frameSeconds) % frames;
}
public static void main(String[] args) {
for (double t = 0; t < 1.0; t += 0.25) {
System.out.print(frame(t, 0.2, 3) + " ");
}
System.out.println();
}
}0 1 2 0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: נגיעה בקצהBug hunter: touching at the edge
תזכורת: מה ביקשוReminder: the task
שני מלבנים שרק נוגעים בקצה (אחד נגמר ב-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.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Box a = new Box(0, 0, 10, 10);
Box touching = new Box(10, 0, 10, 10);
Box overlap = new Box(9, 0, 10, 10);
System.out.println(a.intersects(touching) + " " + a.intersects(overlap));
}
}
record Box(int x, int y, int w, int h) {
boolean intersects(Box o) {
return x < o.x + o.w && o.x < x + w && y < o.y + o.h && o.y < y + h;
}
}false true
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
משחק 3: לתפוס כוכביםGame 3: catch the stars
תזכורת: מה ביקשוReminder: the task
שלושה כוכבים נופלים 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.
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Box paddle = new Box(40, 90, 30, 5);
List<int[]> stars = new ArrayList<>(List.of(new int[]{50, 0}, new int[]{10, 20}, new int[]{60, 40}));
int score = 0;
int missed = 0;
for (int tick = 0; tick < 12; tick++) {
for (int i = stars.size() - 1; i >= 0; i--) {
int[] s = stars.get(i);
s[1] += 10;
if (new Box(s[0], s[1], 5, 5).intersects(paddle)) {
score += 10;
stars.remove(i);
} else if (s[1] > 100) {
missed++;
stars.remove(i);
}
}
}
System.out.println("score " + score + ", missed " + missed + ", left " + stars.size());
}
}
record Box(int x, int y, int w, int h) {
boolean intersects(Box o) {
return x < o.x + o.w && o.x < x + w && y < o.y + o.h && o.y < y + h;
}
}score 20, missed 1, left 0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
תא ב-sprite sheetA cell in a sprite sheet
תזכורת: מה ביקשוReminder: the task
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, ...].
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
record Cell(int x, int y, int w, int h) {
}
static Cell cell(int index, int columns, int size) {
return new Cell(index % columns * size, index / columns * size, size, size);
}
public static void main(String[] args) {
for (int i : new int[]{0, 3, 5}) {
System.out.println(cell(i, 4, 32));
}
}
}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]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
רשת חוסכת בדיקותA grid saves checks
תזכורת: מה ביקשוReminder: the task
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.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
int[][] things = {{5, 5}, {12, 8}, {150, 150}, {155, 152}, {300, 20}};
int naive = things.length * (things.length - 1) / 2;
Map<String, List<Integer>> grid = new HashMap<>();
for (int i = 0; i < things.length; i++) {
String key = (things[i][0] / 50) + "," + (things[i][1] / 50);
grid.computeIfAbsent(key, k -> new ArrayList<>()).add(i);
}
int checks = 0;
for (List<Integer> cellItems : grid.values()) {
int n = cellItems.size();
checks += n * (n - 1) / 2;
}
System.out.println("naive " + naive + " checks, grid " + checks + " checks");
}
}naive 10 checks, grid 2 checks
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.