מפגש 45: Tilemap, מצלמה, Coyote Time, Jump Buffer ומצבי אנימציה.Session 45: Tilemaps, a camera, coyote time, jump buffering and animation states.
Tilemap, מצלמה, Coyote Time, Jump Buffer ומצבי אנימציה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Tilemaps, a camera, coyote time, jump buffering and animation states. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
אריחיםTiles
קפיצהJump
התנגשותCollision
מצלמהCamera
אויביםEnemies
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
אריחיםTiles
מפת אריחיםA tilemap
מפה: # קרקע, c מטבע, e אויבA map: # ground, c coin, e enemy
אריח = Rect בגודל TA tile = a Rect of size T
מפה גדולה מהמסךA map bigger than the screen
פלטפורמר נבנה ממפת אריחים, בדיוק כמו שלבי Brick Breaker: כל תו במחרוזת הוא אריח בגודל 16×16. # הוא קרקע, c מטבע, e אויב ו-F הדגל בסוף. המפה רחבה בהרבה מהמסך, ובהמשך המצלמה תגלה אותה בהדרגה. כאן מציירים את כל העולם מוקטן כדי לראות אותו בבת אחת:A platformer is built from a tilemap, just like Brick Breaker's levels: each character is a 16×16 tile. # is ground, c a coin, e an enemy and F the flag at the end. The map is much wider than the screen, and later the camera will reveal it gradually. Here we draw the whole world scaled down to see it at once:
אנלוגיהAnalogy
מפת אריחים היא כמו לגו: כמה סוגי לבנים בגודל אחיד, ומהם בונים כל עולם שרוצים.A tilemap is like Lego: a few kinds of same-size bricks, and from them you build any world you like.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
קפיצה = vy = -6 רק אם groundedA jump = vy = -6 only if grounded
שחרור מוקדם = קפיצה נמוכהAn early release = a short jump
הכבידה מוכרת מ-Flappy. ההבדל: מותר לקפוץ רק כשעומדים על הקרקע, אחרת השחקן היה "עף". הדגל on_ground מתאפס בכל פריים ונדלק רק כשמתנגשים במשהו מלמטה. טריק של משחקים טובים: אם משחררים את מקש הקפיצה מוקדם, חותכים את המהירות כלפי מעלה, וכך יש שליטה בגובה:Gravity is familiar from Flappy. The difference: jumping is allowed only when standing on the ground, otherwise the player would "fly". The on_ground flag resets every frame and turns on only on a collision from below. A good-game trick: if the jump key is released early, cut the upward speed, giving control over the height:
אנלוגיהAnalogy
זה כמו לזרוק כדור למעלה: את הגובה קובעת הזריקה, והכבידה מחליטה מתי הוא חוזר. שחרור מוקדם הוא כמו לזרוק חלש יותר.It's like throwing a ball up: the throw sets the height, and gravity decides when it returns. Releasing early is like throwing softer.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
התנגשותCollision
התנגשות באריחיםColliding with tiles
הפרדת צירים: x ואז yAxis separation: x then y
ב-x: עוצרים בקירOn x: stop at the wall
ב-y: נוחתים או נתקלים בתקרהOn y: land or bump the ceiling
כמו בלבני Brick Breaker, הסוד הוא הפרדת צירים. מזיזים ב-x ובודקים: אם נכנסנו לאריח, מצמידים את הצד לקיר. אחר כך מזיזים ב-y ובודקים: אם נכנסנו לאריח בזמן נפילה, נוחתים עליו (ו-on_ground נדלק); בזמן עלייה, הראש נתקל בתקרה. הנה פונקציה שעושה בדיוק את זה:As with Brick Breaker's bricks, the secret is axis separation. Move on x and check: if we entered a tile, snap the side to the wall. Then move on y and check: if we entered a tile while falling, land on it (and on_ground turns on); while rising, the head bumps the ceiling. Here's a function doing exactly that:
אנלוגיהAnalogy
התנגשות באריחים היא כמו ללכת בחדר חשוך עם יד מושטת: כשנוגעים בקיר, עוצרים בדיוק לידו, לא בתוכו.Tile collision is like walking in a dark room with your hand out: touch a wall and you stop right next to it, not inside it.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
מציירים ב-rect.move(-camera, 0)Draw at rect.move(-camera, 0)
מגבילים לגבולות העולםClamp to the world's edges
העולם רחב מהמסך, אז צריך מצלמה: מספר שאומר כמה פיקסלים לגלול. הלוגיקה של המשחק נשארת בקואורדינטות של העולם, ורק בציור מחסירים את המצלמה. המצלמה ממקמת את השחקן בשליש השמאלי של המסך (כדי לראות קדימה), ולא יוצאת מגבולות העולם:The world is wider than the screen, so you need a camera: a number saying how many pixels to scroll. Game logic stays in world coordinates, and only when drawing do you subtract the camera. The camera keeps the player in the left third of the screen (to see ahead), and never goes past the world's edges:
אנלוגיהAnalogy
מצלמה היא כמו חלון ברכבת: הנוף עצמו לא זז, אתם זזים לאורכו ורואים בכל רגע רק את החלק שמול החלון.A camera is like a train window: the landscape itself doesn't move, you move along it and see only the part in front of the window.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
אויביםEnemies
מטבעות ואויביםCoins and enemies
מטבע: נגיעה = נקודה ומחיקהA coin: a touch = a point and removal
סיור: מסתובב בקצה הפלטפורמהA patrol: turns at the platform's edge
קפיצה על אויב מלמעלה = הוא מתJumping on an enemy from above = it dies
מטבעות הם Rects שנעלמים במגע. אויב מסייר הולך עד שאין קרקע לפניו (בודקים אריח מתחת לנקודה שלפניו) ואז מסתובב. והחוק הקלאסי: נגיעה מהצד פוגעת בשחקן, אבל נחיתה עליו מלמעלה (השחקן נופל, והתחתית שלו מעל אמצע האויב) מחסלת אותו ומקפיצה מעט:Coins are Rects that vanish on touch. A patrolling enemy walks until there's no ground ahead (check the tile below the point in front of it) and then turns. And the classic rule: touching from the side hurts the player, but landing on it from above (the player falling, their bottom above the enemy's middle) defeats it, with a little bounce:
אנלוגיהAnalogy
אויב מסייר הוא כמו שומר שהולך לאורך חומה: מגיע לפינה, מסתובב, וחוזר, בלי לחשוב יותר מזה.A patrolling enemy is like a guard walking a wall: reaches the corner, turns, comes back, and thinks no further.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Pixel Platformer: המשחקPixel Platformer: the game
עולם מאריחים, מצלמה שעוקבתA tile world, a following camera
קפיצות, מטבעות ואויביםJumps, coins and enemies
מגיעים לדגלReach the flag
המשחק כולו: עולם ממפת אריחים, שחקן עם כבידה וקפיצה, התנגשות בהפרדת צירים, מצלמה שעוקבת, מטבעות, אויבים מסיירים, ודגל בסוף. כאן השחקן רץ ימינה וקופץ לבד מול בורות, קירות ואויבים; במחשב שלכם החיצים ורווח:The whole game: a world from a tilemap, a player with gravity and jumping, axis-separated collision, a following camera, coins, patrolling enemies, and a flag at the end. Here the player runs right and jumps by itself at gaps, walls and enemies; on your computer, arrows and space:
אנלוגיהAnalogy
פלטפורמר הוא אוסף של חוקים קטנים שכל אחד מהם פשוט: כבידה, מגע בקרקע, מצלמה. החוויה נוצרת מהשילוב.A platformer is a collection of small rules, each simple: gravity, ground contact, a camera. The experience comes from combining them.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
מפה: # קרקע, c מטבע, e אויבA map: # ground, c coin, e enemy