נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

Retro Snake: נחש רטרוRetro Snake

מפגש 40: רשת משבצות, deque, טיימר צעדים, תור קלט ושלבים.Session 40: A grid, deque, a step timer, an input queue and levels.

מה נעשה היוםToday's plan

  • רשתGrid
  • נחשSnake
  • גדילהGrowing
  • התנגשותCollision
  • קלטInput
  • משחקGame
נושאTopic 1

רשתGrid

עולם של משבצותA world of cells

  • רשת: (עמודה, שורה)grid: (column, row)
  • פיקסלים = משבצת × CELLPixels = cell × CELL
  • לוח שחמט עדין ברקעA subtle chessboard background

אנלוגיה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".
נושא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!

אנלוגיה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.
נושא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

אנלוגיה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.
נושאTopic 4

התנגשותCollision

התנגשויותCollisions

  • ראש בתוך הגוף = סוףHead inside the body = game over
  • קיר: סוף או מעבר לצד השניWall: game over or wrap-around
  • % עוטף מספרים% wraps numbers

אנלוגיהAnalogy

מעבר מקצה לקצה הוא כמו מסלול ריצה עגול: יוצאים מהקו ומגיעים לאותו קו מההתחלה.Wrap-around is like a circular running track: you cross the line and arrive back at the start.
נושאTopic 5

קלטInput

טיקים וקלטTicks and input

  • טיק כל 120ms, ציור כל פרייםA tick every 120ms, drawing every frame
  • אסור להתהפך לאחורNo reversing
  • תור קלט לשתי לחיצות מהירותAn input queue for two quick presses

אנלוגיה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.
נושאTopic 6

משחקGame

Retro Snake: המשחקRetro Snake: the game

  • רשת, נחש, אוכל, ניקודGrid, snake, food, score
  • מעבר מקצה לקצהWrap-around
  • צבע הולך ונחלש לאורך הגוףA colour fading along the body

אנלוגיהAnalogy

הנחש מראה כמה רחוק אפשר להגיע עם רשימה פשוטה: כל המשחק הוא insert, pop ו-in.Snake shows how far a simple list can take you: the whole game is insert, pop and in.

טעות נפוצה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.

רשת: (עמודה, שורה)grid: (column, row)

פיקסלים = משבצת × CELLPixels = cell × CELL

לוח שחמט עדין ברקעA subtle chessboard background

רשימת טאפלים, הראש ראשוןA list of tuples, head first

תנועה: ראש חדש בהתחלה, מוחקים את הזנבMove: a new head in front, remove the tail

לא מזיזים כל חוליה!Don't move every segment!

ראש על האוכל → לא מוחקים זנבHead on the food → don't remove the tail

אוכל חדש רק במשבצת פנויהNew food only on a free cell

random.choice מרשימת פנויותrandom.choice from free cells

ראש בתוך הגוף = סוףHead inside the body = game over

קיר: סוף או מעבר לצד השניWall: game over or wrap-around

% עוטף מספרים% wraps numbers

טיק כל 120ms, ציור כל פרייםA tick every 120ms, drawing every frame

אסור להתהפך לאחורNo reversing

תור קלט לשתי לחיצות מהירותAn input queue for two quick presses

רשת, נחש, אוכל, ניקודGrid, snake, food, score

מעבר מקצה לקצהWrap-around

מה למדנוWhat we learned

  • רשת: (עמודה, שורה)grid: (column, row)
  • רשימת טאפלים, הראש ראשוןA list of tuples, head first
  • ראש על האוכל → לא מוחקים זנבHead on the food → don't remove the tail
  • ראש בתוך הגוף = סוףHead inside the body = game over
  • טיק כל 120ms, ציור כל פרייםA tick every 120ms, drawing every frame
  • רשת, נחש, אוכל, ניקודGrid, snake, food, score

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich