חלון ראשון: 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.
שלושה טיקיםThree ticks
תזכורת: מה ביקשוReminder: the task
כדור ב-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.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
int x = 0;
int vx = 5;
for (int tick = 1; tick <= 3; tick++) {
x += vx;
System.out.println("tick " + tick + " x=" + x);
}
}
}tick 1 x=5 tick 2 x=10 tick 3 x=15
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
קירA wall
תזכורת: מה ביקשוReminder: the task
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.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
Ball b = new Ball(80, 50, 15, 0);
for (int t = 1; t <= 4; t++) {
b.update(100, 100);
System.out.print(b.x + " ");
}
System.out.println();
}
}
class Ball {
int x, y, vx, vy;
Ball(int x, int y, int vx, int vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
}
boolean update(int w, int h) {
x += vx;
y += vy;
boolean bounced = false;
if (x <= 0 || x >= w) {
vx = -vx;
bounced = true;
}
if (y <= 0 || y >= h) {
vy = -vy;
bounced = true;
}
return bounced;
}
}95 110 95 80
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מסך של תוויםA screen of characters
תזכורת: מה ביקשוReminder: the task
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..|.
סימולציה: הפתרון רץ שורה אחרי שורה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 String render(int x, int width) {
StringBuilder row = new StringBuilder();
for (int c = 0; c < width; c++) {
row.append(c == x ? 'o' : '.');
}
return "|" + row + "|";
}
public static void main(String[] args) {
int x = 0;
int vx = 1;
for (int tick = 0; tick < 6; tick++) {
System.out.println(render(x, 4));
x += vx;
if (x == 0 || x == 3) {
vx = -vx;
}
}
}
}|o...| |.o..| |..o.| |...o| |..o.| |.o..|
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: כדור בורחBug hunter: an escaping ball
תזכורת: מה ביקשוReminder: the task
הכדור מתחיל ב-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.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
int x = 90;
int vx = 4;
for (int t = 1; t <= 6; t++) {
x += vx;
if (x >= 100) {
vx = -vx;
}
System.out.print(x + " ");
}
System.out.println();
}
}94 98 102 98 94 90
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
משחק 1: כדורים קופציםGame 1: bouncing balls
תזכורת: מה ביקשוReminder: the task
שלושה כדורים במשטח 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.
הפתרון המלא והפלט שלוFull solution and its output
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Ball> balls = List.of(new Ball(10, 10, 4, 3), new Ball(50, 20, -5, 6), new Ball(90, 90, 7, -2));
int bounces = 0;
for (int tick = 0; tick < 10; tick++) {
for (Ball b : balls) {
if (b.update(100, 100)) {
bounces++;
}
}
}
System.out.println("after 10 ticks: " + bounces + " bounces");
for (Ball b : balls) {
System.out.println(b.x + "," + b.y);
}
}
}
class Ball {
int x, y, vx, vy;
Ball(int x, int y, int vx, int vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
}
boolean update(int w, int h) {
x += vx;
y += vy;
boolean bounced = false;
if (x <= 0 || x >= w) {
vx = -vx;
bounced = true;
}
if (y <= 0 || y >= h) {
vy = -vy;
bounced = true;
}
return bounced;
}
}after 10 ticks: 2 bounces 50,40 0,80 48,70
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
כוח משיכהGravity
תזכורת: מה ביקשוReminder: the task
כדור נופל: בכל טיק 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...).
סימולציה: הפתרון רץ שורה אחרי שורה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) {
double y = 0;
double vy = 0;
double floor = 100;
int bounces = 0;
for (int tick = 0; tick < 60 && bounces < 3; tick++) {
vy += 2;
y += vy;
if (y >= floor) {
y = floor;
vy = -vy * 0.8;
bounces++;
System.out.println("bounce " + bounces + " at tick " + tick + ", up speed " + Math.round(-vy));
}
}
}
}bounce 1 at tick 9, up speed 16 bounce 2 at tick 24, up speed 11 bounce 3 at tick 35, up speed 9
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מסך דו-ממדיA 2D screen
תזכורת: מה ביקשוReminder: the task
מסך 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.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
int w = 8, h = 4;
int[][] balls = {{1, 1, 1, 1}, {6, 2, -1, 1}};
for (int tick = 0; tick < 2; tick++) {
char[][] screen = new char[h][w];
for (char[] row : screen) {
java.util.Arrays.fill(row, '.');
}
for (int[] b : balls) {
screen[b[1]][b[0]] = 'o';
b[0] += b[2];
b[1] += b[3];
if (b[0] <= 0 || b[0] >= w - 1) {
b[2] = -b[2];
}
if (b[1] <= 0 || b[1] >= h - 1) {
b[3] = -b[3];
}
}
for (char[] row : screen) {
System.out.println(new String(row));
}
System.out.println("--");
}
}
}........ .o...... ......o. ........ -- ........ ........ ..o..... .....o.. --
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.