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

חזרה למפגשBack
PythonPython

Star Catcher: תופסים כוכביםStar Catcher

מפגש 38: Rect, מקלדת, התנגשויות, טקסט, מצבי משחק ושיא בקובץ.Session 38: Rect, keyboard, collisions, text, game states and a saved high score.

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

  • מקלדתKeyboard
  • RectRect
  • הופעהSpawning
  • התנגשותCollision
  • מצבState
  • משחקGame
נושאTopic 1

מקלדתKeyboard

מקלדתThe keyboard

  • `KEYDOWN`: פעם אחת בלחיצה`KEYDOWN`: once per press
  • get_pressed(): כל עוד מחזיקיםget_pressed(): as long as held
  • pygame.K_LEFT, K_SPACE...pygame.K_LEFT, K_SPACE...

אנלוגיהAnalogy

KEYDOWN הוא כמו פעמון בדלת: מצלצל פעם אחת כשלוחצים. get_pressed הוא כמו להסתכל אם מישהו עדיין עומד ליד הדלת.KEYDOWN is like a doorbell: it rings once when pressed. get_pressed is like looking whether someone is still standing at the door.
נושאTopic 2

RectRect

Rect ותנועה בגבולותRect and bounded movement

אנלוגיהAnalogy

להגביל לטווח זה כמו גדר במגרש משחקים: הילדים רצים חופשי, אבל לא יוצאים לכביש.Clamping is like a playground fence: kids run freely, but never out into the road.
נושאTopic 3

הופעהSpawning

כוכבים נופליםFalling stars

  • הופעה כל N פריימיםspawn every N frames
  • random.randint למיקוםrandom.randint for position
  • מוחקים מה שיצא מהמסךRemove what leaves the screen

אנלוגיהAnalogy

הופעה היא כמו מכונת כדורי טניס: כל כמה שניות יוצא כדור, והכדורים שנפלו מהמגרש נאספים, כדי שהמגרש לא יתמלא.Spawning is like a tennis ball machine: every few seconds a ball comes out, and balls off the court are collected so the court doesn't fill up.
נושאTopic 4

התנגשותCollision

התנגשותCollision

  • a.colliderect(b) → True/Falsea.colliderect(b) → True/False
  • collidelist / collidepointcollidelist / collidepoint
  • מלבנים חופפים = התנגשותOverlapping rects = a collision

אנלוגיהAnalogy

התנגשות היא כמו לבדוק אם שני שטיחים על הרצפה חופפים: אם אחד נגמר לפני שהשני מתחיל, בכל כיוון, הם לא נוגעים.Collision is like checking whether two rugs on the floor overlap: if one ends before the other starts, in any direction, they don't touch.
נושאTopic 5

מצבState

ניקוד, חיים וסיוםScore, lives and game over

  • ניקוד עולה בתפיסהScore goes up on a catch
  • חיים יורדים בפספוסLives go down on a miss
  • מצב משחק: "playing" / "over"Game state: "playing" / "over"

אנלוגיהAnalogy

מצב משחק הוא כמו רמזור: ירוק = משחקים, אדום = נגמר. הלולאה מסתכלת ברמזור לפני שהיא מחליטה מה לעשות.Game state is like a traffic light: green = playing, red = over. The loop looks at the light before deciding what to do.
נושאTopic 6

משחקGame

Star Catcher: המשחקStar Catcher: the game

  • סל שזז עם החיציםA basket moving with the arrows
  • כוכבים נופלים ונתפסיםStars fall and get caught
  • ניקוד וחיים בפינהScore and lives in the corner

אנלוגיהAnalogy

המשחק כולו הוא שלושה רעיונות שכבר הכרתם: תנועה, הופעה והתנגשות. כמו מתכון שמחבר שלושה מצרכים פשוטים למנה.The whole game is three ideas you already know: movement, spawning and collision. Like a recipe combining three simple ingredients into a dish.

`KEYDOWN`: פעם אחת בלחיצה`KEYDOWN`: once per press

get_pressed(): כל עוד מחזיקיםget_pressed(): as long as held

pygame.K_LEFT, K_SPACE...pygame.K_LEFT, K_SPACE...

`Rect` יודע left, right, centerxA `Rect` knows left, right, centerx

הגבלה לטווח שומרת בתוך המסךClamping keeps it on screen

rect.clamp_ip(screen_rect)rect.clamp_ip(screen_rect)

הופעה כל N פריימיםspawn every N frames

random.randint למיקוםrandom.randint for position

מוחקים מה שיצא מהמסךRemove what leaves the screen

a.colliderect(b) → True/Falsea.colliderect(b) → True/False

collidelist / collidepointcollidelist / collidepoint

מלבנים חופפים = התנגשותOverlapping rects = a collision

ניקוד עולה בתפיסהScore goes up on a catch

חיים יורדים בפספוסLives go down on a miss

מצב משחק: "playing" / "over"Game state: "playing" / "over"

סל שזז עם החיציםA basket moving with the arrows

כוכבים נופלים ונתפסיםStars fall and get caught

ניקוד וחיים בפינהScore and lives in the corner

מה למדנוWhat we learned

  • `KEYDOWN`: פעם אחת בלחיצה`KEYDOWN`: once per press
  • `Rect` יודע left, right, centerxA `Rect` knows left, right, centerx
  • הופעה כל N פריימיםspawn every N frames
  • a.colliderect(b) → True/Falsea.colliderect(b) → True/False
  • ניקוד עולה בתפיסהScore goes up on a catch
  • סל שזז עם החיציםA basket moving with the arrows

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

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

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