מפגש 40: רשת משבצות, deque, טיימר צעדים, תור קלט ושלבים.Session 40: A grid, deque, a step timer, an input queue and levels.
רשת משבצות, deque, טיימר צעדים, תור קלט ושלבים. בסוף המפגש מוסיפים צעד לפרויקט המלווה.A grid, deque, a step timer, an input queue and levels. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
רשתGrid
נחשSnake
גדילהGrowing
התנגשותCollision
קלטInput
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
בנחש, העולם הוא רשת של משבצות. במקום לחשוב בפיקסלים, חושבים ב"עמודה 5, שורה 3", וממירים לפיקסלים רק בשביל הציור: כפול גודל המשבצת. זה מפשט הכול: התנגשות היא פשוט "אותה משבצת". רקע של משבצות לסירוגין עוזר לעין לראות את הרשת:In Snake the world is a grid of cells. Instead of thinking in pixels, think "column 5, row 3", and convert to pixels only for drawing: times the cell size. That simplifies everything: a collision is just "the same cell". A checkerboard background helps the eye see the grid:
אנלוגיהAnalogy
רשת היא כמו לוח שחמט: לא אומרים "הפרש 3.7 סנטימטר משמאל", אלא "הפרש ב-ג5".A grid is like a chessboard: you don't say "the knight is 3.7 cm from the left", you say "the knight is on c5".
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
נחשSnake
הנחש כרשימהThe snake as a list
רשימת טאפלים, הראש ראשוןA list of tuples, head first
תנועה: ראש חדש בהתחלה, מוחקים את הזנבMove: a new head in front, remove the tail
לא מזיזים כל חוליה!Don't move every segment!
הנחש הוא רשימה של משבצות, הראש בהתחלה. הטריק היפה: כדי לזוז לא צריך להזיז כל חוליה. מוסיפים ראש חדש במשבצת הבאה (insert(0, head)), ומוחקים את הזנב (pop()). כל השאר נשאר במקום, וזה נראה כמו זחילה. הסימולציה מראה את הרשימה משתנה:The snake is a list of cells, head first. The lovely trick: to move, you don't move every segment. Add a new head in the next cell (insert(0, head)), and remove the tail (pop()). Everything else stays put, and it looks like crawling. The simulation shows the list changing:
אנלוגיהAnalogy
הנחש הוא כמו רכבת של אנשים שהולכים בתור: הראשון זז קדימה וכל אחד נכנס למקום של זה שלפניו, אז מספיק להוסיף מקום מקדימה ולמחוק מאחורה.The snake is like a queue of people walking in line: the first steps forward and each takes the spot of the one ahead, so it's enough to add a spot in front and remove one behind.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
גדילהGrowing
לאכול ולגדולEating and growing
ראש על האוכל → לא מוחקים זנבHead on the food → don't remove the tail
אוכל חדש רק במשבצת פנויהNew food only on a free cell
random.choice מרשימת פנויותrandom.choice from free cells
גדילה היא הטריק הקודם הפוך: כשהראש נכנס למשבצת של האוכל, פשוט לא מוחקים את הזנב, והנחש מתארך בחוליה. את האוכל החדש שמים רק במשבצת שאין בה נחש: בונים רשימה של משבצות פנויות ובוחרים אחת באקראי:Growing is the previous trick in reverse: when the head enters the food's cell, simply don't remove the tail, and the snake gets one segment longer. Put the new food only on a cell without snake: build a list of free cells and choose one at random:
אנלוגיהAnalogy
זה כמו לסחוב חבל מהסליל: בכל צעד שולפים עוד קצת, ורק כשרוצים להאריך, לא מגלגלים בחזרה.It's like pulling rope from a reel: each step pulls a bit more out, and only when you want it longer do you skip rolling it back.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
התנגשותCollision
התנגשויותCollisions
ראש בתוך הגוף = סוףHead inside the body = game over
ברשת, התנגשות היא שאלה אחת: האם הראש החדש כבר ברשימה? head in snake. לקירות שתי אפשרויות: לסיים את המשחק, או לעבור לצד השני בעזרת %: (x + 1) % COLS הופך את עמודה 30 ל-0. שימו לב: בודקים התנגשות לפני שמוחקים את הזנב? לא! הזנב זז, אז מותר להיכנס למשבצת שהזנב עוזב:On a grid, a collision is one question: is the new head already in the list? head in snake. For walls there are two options: end the game, or wrap-around with %: (x + 1) % COLS turns column 30 into 0. Note: should you check before removing the tail? No! The tail moves, so entering the cell the tail is leaving is allowed:
אנלוגיהAnalogy
מעבר מקצה לקצה הוא כמו מסלול ריצה עגול: יוצאים מהקו ומגיעים לאותו קו מההתחלה.Wrap-around is like a circular running track: you cross the line and arrive back at the start.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
קלטInput
טיקים וקלטTicks and input
טיק כל 120ms, ציור כל פרייםA tick every 120ms, drawing every frame
הנחש לא זז בכל פריים: הוא זז בכל טיק, למשל כל 120 אלפיות שנייה, וזה הקצב שמרגיש "רטרו". שני כללי קלט: אסור לפנות 180 מעלות (הנחש היה נכנס בעצמו). ואם השחקן לוחץ למעלה ואז שמאלה מהר מאוד, בתוך טיק אחד, שומרים את הלחיצות בתור ומבצעים אחת בכל טיק:The snake doesn't move every frame: it moves every tick, say every 120 milliseconds, the pace that feels "retro". Two input rules: no 180-degree turns (the snake would run into itself). And if the player presses up then left very fast, within one tick, keep the presses in a queue and apply one per tick:
אנלוגיהAnalogy
תור קלט הוא כמו פתקים שמשאירים לנהג: "ימינה, ואז שמאלה". גם אם כתבתם את שניהם בבת אחת, הוא מבצע אותם לפי הסדר בצמתים הבאים.An input queue is like notes left for a driver: "right, then left". Even if you wrote both at once, they're carried out in order at the next junctions.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Retro Snake: המשחקRetro Snake: the game
רשת, נחש, אוכל, ניקודGrid, snake, food, score
מעבר מקצה לקצהWrap-around
צבע הולך ונחלש לאורך הגוףA colour fading along the body
המשחק השלם: נחש שזוחל ברשת, אוכל אדום שמופיע במשבצת פנויה, גדילה, התנגשות עצמית, מעבר מקצה לקצה, וניקוד. הגוף נצבע בירוק שהולך ונחלש לכיוון הזנב. כאן "שחקן אוטומטי" פונה לעבר האוכל; במחשב שלכם החיצים מכניסים פניות לתור:The full game: a snake crawling on a grid, red food appearing on a free cell, growing, self-collision, wrap-around, and score. The body is coloured a green that fades toward the tail. Here an "auto-player" turns toward the food; on your computer the arrows add turns to the queue:
אנלוגיהAnalogy
הנחש מראה כמה רחוק אפשר להגיע עם רשימה פשוטה: כל המשחק הוא insert, pop ו-in.Snake shows how far a simple list can take you: the whole game is insert, pop and in.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
retro_snake.py
import random
free = []
random.choice(free)
מה Python עונהWhat Python says
IndexError: Cannot choose from an empty sequence
IndexError: Cannot choose from an empty sequence: אם אין משבצת פנויה, השחקן ניצח! בודקים if not free: ומציגים מסך ניצחון.IndexError: Cannot choose from an empty sequence: if there's no free cell, the player won! Check if not free: and show a victory screen.
רשימת המשבצות הפנויות ריקה כשהנחש ממלא את כל הלוח.The free-cell list is empty when the snake fills the board.