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

PythonPython
42
חלקPart 7: משחקים עם PygameGames with Pygame כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Flappy: ציפור מעופפתFlappy

Sprite sheet, אנימציית פריימים, Parallax ו-dt.

Sprite sheets, frame animation, parallax and dt.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • כבידה: vel += GRAVITY בכל פרייםGravity: vel += GRAVITY every frame
  • דחיפה: vel = FLAP בלחיצהImpulse: vel = FLAP on a press
  • צינור = x ומרכז רווחA pipe = x and a gap centre
  • מלבן עליון ומלבן תחתון מחושביםTop and bottom rects are computed
  • נקודה כשהצינור עובר את הציפורA point when the pipe passes the bird
  • דגל scored = פעם אחת בלבדA scored flag = only once
  • ציור על Surface שקוףDraw on a transparent Surface
  • pygame.transform.rotate(surf, angle)pygame.transform.rotate(surf, angle)
  • פרלקסה: רחוק = איטיParallax: far = slow

מונחיםTerms

  • כבידהgravity: תאוצה קבועה כלפי מטה: בכל פריים מוסיפים קבוע למהירות האנכית.A constant downward acceleration: every frame, add a constant to the vertical velocity.
  • דחיפה (impulse)impulse: שינוי מיידי במהירות, כמו קפיצה: vel = -6. אחריה הכבידה מאטה ומושכת חזרה.An instant change in velocity, like a jump: vel = -6. After it, gravity slows and pulls back.
  • גלילהscrolling: תחושת תנועה שנוצרת כשהעולם זז לכיוון אחד והשחקן נשאר במקום.A feeling of motion created when the world moves one way while the player stays in place.
  • פרלקסהparallax: שכבות רקע שזזות במהירויות שונות: רחוק = לאט. נותן עומק.Background layers moving at different speeds: far = slow. Gives depth.
  • תיבת פגיעהhitbox: המלבן שמשמש לבדיקת התנגשות. לרוב קטן מעט מהציור, כדי שהמשחק ירגיש הוגן.The rectangle used for collision checks. Usually a bit smaller than the drawing, so the game feels fair.

Cheatsheet

כבידהGravity
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

GRAVITY, FLAP = 0.35, -6.0
y, vel, heights = 160.0, 0.0, []
for frame in range(120):
צינורותPipes
import random
import pygame

H, GAP = 320, 110
random.seed(2)

def pipe_rects(x, gap_y, width=40):
ניקודScore
import pygame

BIRD_X = 90
bird_draw = pygame.Rect(0, 0, 30, 22)
bird_draw.center = (BIRD_X, 150)
hitbox = bird_draw.inflate(-8, -6)
print("drawing", bird_draw.size, "hitbox", hitbox.size)
סיבובRotation
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 160))
screen.fill((120, 200, 240))
bird = pygame.Surface((30, 22), pygame.SRCALPHA)
pygame.draw.ellipse(bird, (255, 210, 60), (0, 0, 30, 22))
פרלקסהParallax
import pygame

pygame.init()
W, H = 480, 200
screen = pygame.display.set_mode((W, H))
layers = [((180, 210, 240), 0.2, 70, 90), ((130, 190, 140), 0.5, 45, 60), ((80, 150, 90), 1.0, 25, 40)]
offset = 0.0
משחקGame
import random
import pygame

W, H = 480, 320
GRAVITY, FLAP, SPEED, GAP = 0.35, -6.0, 2.5, 110
pygame.init()
screen = pygame.display.set_mode((W, H))

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
AssertionErrorמוסיפים את הכבידה למיקום במקום למהירות.Adding gravity to the position instead of the velocity.AssertionError: falls [0.5, 0.5, 0.5]: ...: בלי מהירות אין האצה. vel += GRAVITY ואז y += vel.AssertionError: falls [0.5, 0.5, 0.5]: ...: without velocity there's no acceleration. vel += GRAVITY then y += vel.
AssertionErrorרווח אקראי שיוצא חלקית מהמסך.A random gap partly off screen.AssertionError: מגבילים את טווח ההגרלה כך שהרווח כולו בתוך המסך, עם שוליים.AssertionError: limit the random range so the whole gap is on screen, with a margin.
AssertionErrorסופרים נקודה בכל פריים שהצינור משמאל.Counting a point every frame the pipe is to the left.AssertionError: one pipe gave 12 points: add a scored flag: התנאי נשאר אמת, אז מסמנים את הצינור אחרי הנקודה הראשונה.AssertionError: one pipe gave 12 points: add a scored flag: the condition stays true, so mark the pipe after the first point.
AssertionErrorמסובבים את התמונה המסובבת שוב ושוב.Rotating the already-rotated image again and again.AssertionError: after 20 frames the image is (435, 435): always rotate the original: כל סיבוב מגדיל ומטשטש. שומרים את המקור ומסובבים אותו בכל פריים לזווית המלאה.AssertionError: after 20 frames the image is (435, 435): always rotate the original: each rotation grows and blurs it. Keep the original and rotate it each frame to the full angle.
AssertionErrorכל השכבות באותה מהירות, והרקע נראה שטוח.All layers at the same speed, so the background looks flat.AssertionError: every layer moves at 1.0: ...: נותנים לכל שכבה מהירות אחרת, הרחוקה האיטית ביותר.AssertionError: every layer moves at 1.0: ...: give each layer its own speed, the farthest slowest.
AssertionErrorמהירות נפילה בלי גבול, והציפור עוברת דרך צינורות.Unlimited falling speed, so the bird passes through pipes.AssertionError: after 200 frames vel is 70 pixels a frame: ...: מגבילים מהירות סופית (min(vel, 10)), אחרת הנפילה מהירה מדי ונוצר מנהור.AssertionError: after 200 frames vel is 70 pixels a frame: ...: cap the terminal velocity (min(vel, 10)), otherwise falling gets too fast and tunneling appears.

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