חלקPart 7: משחקים עם PygameGames with Pygame
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
Brick Breaker: שובר לבניםBrick Breaker
מחלקות במשחק, שלבים מנתוני טקסט, power-ups ומטמון תמונות.
Classes in a game, levels from text data, power-ups and an image cache.
הנקודות החשובותKey points
- מפת שלב: כל תו הוא לבנהA level map: each character is a brick
.= ריק.= empty- הפרדת צירים: קודם x, אז yAxis separation: x first, then y
- פגיעה ב-x →
vx = -vxA hit on x →vx = -vx - כדור מהיר יכול "לדלג" מעל לבנהA fast ball can "skip" a brick
- צעדים קטנים: מחלקים את התנועהSmall steps: split the motion
- לבנה שנשברת מפילה חיזוק לפעמיםA broken brick sometimes drops a power-up
- תופסים במחבט → אפקטCatch it with the paddle → an effect
- כדור מתחת למחבט = חיים פחותBall below the paddle = a life lost
מונחיםTerms
- מפת שלבlevel map: תיאור של שלב כטקסט: כל תו הוא משבצת (לבנה, ריק, אויב). קל לערוך ולהוסיף שלבים.A level described as text: each character is a cell (brick, empty, enemy). Easy to edit and add levels.
- הפרדת ציריםaxis separation: מזיזים קודם בציר x ובודקים התנגשות, ואז בציר y ובודקים שוב. כך יודעים מאיזה צד הייתה הפגיעה.Move along x first and check collisions, then along y and check again. That tells you which side was hit.
- חיזוק (power-up)power-up: פריט שנופל ונותן יכולת זמנית, כמו מחבט רחב יותר או כדור נוסף.An item that falls and gives a temporary ability, like a wider paddle or an extra ball.
- מנהורtunneling: באג שבו עצם מהיר "מדלג" מעל עצם דק בין שני פריימים ולא מתנגש בו.A bug where a fast object "skips over" a thin one between two frames and never collides with it.
Cheatsheet
שלבLevel
import pygame
LEVEL = [
"RRRRRRRRRR",
"OOOO..OOOO",
"YYYYYYYYYY",
"GG.GGGG.GG",צדדיםSides
import pygame
def move_ball(ball, vx, vy, bricks):
ball.x += vx
i = ball.collidelist(bricks)
if i >= 0:
bricks.pop(i)מנהורTunneling
import pygame
brick = pygame.Rect(100, 50, 40, 14)
def crosses(y, vy, steps):
ball = pygame.Rect(110, y, 10, 10)
for _ in range(steps):חיזוקיםPower-ups
import random
random.seed(6)
wide_timer = 0
paddle_w = 80
drops = 0
for frame in range(600):שלביםLevels
LEVELS = [["RR", ".."], ["RRR", "OOO"], ["Y.Y"]]
level, lives, state = 0, 3, "playing"
bricks = sum(row.count("R") + row.count("O") + row.count("Y") for row in LEVELS[level])
for event in ["break", "break", "fall", "break"] + ["break"] * 6 + ["break", "break"]:
if state != "playing":
break
if event == "fall":משחקGame
import pygame
W, H = 480, 320
LEVEL = [
"RRRRRRRRRR",
"OOOO..OOOO",
"YYYYYYYYYY",שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
AssertionError | שורות באורך שונה במפה.Rows of different lengths in the map. | AssertionError: row lengths [2, 4]: ...: מוסיפים נקודות לשורה הקצרה. בדיקה כזו בתחילת הטעינה חוסכת באגים מוזרים.AssertionError: row lengths [2, 4]: ...: pad the short row with dots. A check like this when loading saves odd bugs. |
AssertionError | הופכים את שני הכיוונים בכל פגיעה.Flipping both directions on every hit. | AssertionError: vx became -3: ...: הכדור חוזר בדיוק במסלול שבא, וזה נראה לא טבעי. הופכים רק את הציר שבו הייתה הפגיעה.AssertionError: vx became -3: ...: the ball goes straight back the way it came, which looks unnatural. Flip only the axis that was hit. |
AssertionError | מגבירים מהירות בלי לחשוב על עובי הלבנים.Raising the speed without thinking about brick thickness. | AssertionError: speed 30 > 24: ...: מגבילים מהירות, או מחלקים לצעדים כמו בדוגמה.AssertionError: speed 30 > 24: ...: cap the speed, or split into sub-steps as in the example. |
AssertionError | מגדילים את הרוחב בכל איסוף, בלי גבול.Widening on every pickup, without limit. | AssertionError: paddle is 480px wide ...: קובעים רוחב (paddle_w = 130) ומאפסים את הטיימר, במקום להוסיף שוב ושוב.AssertionError: paddle is 480px wide ...: set the width (paddle_w = 130) and reset the timer, instead of adding again and again. |
AssertionError | עוברים שלב בלי לטעון את הלבנים החדשות.Advancing a level without loading the new bricks. | AssertionError: level 2 started with 0 bricks: ...: אחרי level += 1 בונים מחדש את רשימת הלבנים מהמפה החדשה.AssertionError: level 2 started with 0 bricks: ...: after level += 1 rebuild the brick list from the new map. |
AssertionError | שוברים כמה לבנים בפגיעה אחת ומהפכים את הכיוון כמה פעמים.Breaking several bricks in one hit and flipping the direction several times. | AssertionError: two bricks hit in the same frame flipped vy twice, back to -4: ...: גם אם נשברו שתי לבנים, הופכים את הציר פעם אחת בלבד.AssertionError: two bricks hit in the same frame flipped vy twice, back to -4: ...: even if two bricks broke, flip the axis only once. |