חלק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.
הנקודות החשובותKey points
- AABB: התנגשות בין שני מלבנים ישרים:AABB: A collision between two upright rectangles:
- sprite: תמונה קטנה של דמות או עצם במשחק.sprite: A small picture of a character or object in a game.
Rectangle.intersectsבודק חפיפה.Rectangle.intersectschecks for overlap.- הכוכב שנוגע במשוט מצויר באדום.The star touching the paddle is drawn in red.
- שני מלבנים חופפים אם כל אחד מתחיל לפני שהשני נגמר, בשני הצירים.Two rectangles overlap if each starts before the other ends, on both axes.
- בסימולציה רואים את ארבע ההשוואות.In the simulation you see the four comparisons.
- 4 פריימים, כל אחד 0.1 שנייה.4 frames, each 0.1 seconds.
- הזמן קובע את הפריים, לא מספר הסיבובים של הלולאה, כך שהאנימציה באותה מהירות בכל מחשב.The time decides the frame, not the number of loop rounds, so the animation runs at the same speed on every computer.
- משחק 3: לתפוס כוכביםGame 3: catch the stars
מונחיםTerms
- AABBAABB: התנגשות בין שני מלבנים ישרים: הם חופפים אם הם חופפים גם בציר x וגם בציר y.
Rectangle.intersectsעושה בדיוק את זה.A collision between two upright rectangles: they overlap if they overlap on both the x axis and the y axis.Rectangle.intersectsdoes exactly that. - spritesprite: תמונה קטנה של דמות או עצם במשחק. נטענת עם
ImageIO.readומצוירת עםg.drawImage. sprite sheet: הרבה פריימים בתמונה אחת, ברשת.A small picture of a character or object in a game. Loaded withImageIO.readand drawn withg.drawImage. A sprite sheet: many frames in one picture, on a grid. - אנימציה מפריימיםframe animation: מחליפים פריים כל כמה מאיות שנייה:
frame = (int) (time / frameSeconds) % frames.Switch the frame every few hundredths of a second:frame = (int) (time / frameSeconds) % frames. - broad phasebroad phase: שלב סינון לפני בדיקת התנגשות מדויקת: מחלקים את העולם לתאים ובודקים רק עצמים באותו תא, במקום כל זוג.A filtering step before exact collision checks: split the world into cells and check only objects in the same cell, instead of every pair.
- AABBAABB: התנגשות בין שני מלבנים ישרים: הם חופפים אם הם חופפים גם בציר x וגם בציר y.
Rectangle.intersectsעושה בדיוק את זה.A collision between two upright rectangles: they overlap if they overlap on both the x axis and the y axis.Rectangle.intersectsdoes exactly that. - spritesprite: תמונה קטנה של דמות או עצם במשחק. נטענת עם
ImageIO.readומצוירת עםg.drawImage. sprite sheet: הרבה פריימים בתמונה אחת, ברשת.A small picture of a character or object in a game. Loaded withImageIO.readand drawn withg.drawImage. A sprite sheet: many frames in one picture, on a grid. - אנימציה מפריימיםframe animation: מחליפים פריים כל כמה מאיות שנייה:
frame = (int) (time / frameSeconds) % frames.Switch the frame every few hundredths of a second:frame = (int) (time / frameSeconds) % frames. - broad phasebroad phase: שלב סינון לפני בדיקת התנגשות מדויקת: מחלקים את העולם לתאים ובודקים רק עצמים באותו תא, במקום כל זוג.A filtering step before exact collision checks: split the world into cells and check only objects in the same cell, instead of every pair.
Cheatsheet
התנגשות, מצוירתA collision, drawn
BufferedImage img = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0xF4F1EA));
g.fillRect(0, 0, 400, 200);
Rectangle player = new Rectangle(160, 150, 80, 20);
Rectangle[] stars = {new Rectangle(60, 40, 20, 20), new Rectangle(190, 140, 20, 20), new Rectangle(320, 90, 20, 20)};
g.setColor(new Color(0x2D5BD0));
g.fill(player);
int hits = 0;AABB בלי ספריותAABB with no libraries
Box player = new Box(10, 10, 20, 20);
Box star = new Box(25, 25, 10, 10);
Box far = new Box(50, 10, 10, 10);
System.out.println(player.intersects(star) + " " + player.intersects(far));איזה פריים עכשיו?Which frame now?
for (double t = 0; t < 1.0; t += 0.25) {
System.out.print(frame(t, 0.1, 4) + " ");
}
System.out.println();שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|