חלקPart 7: משחקים עם PygameGames with Pygame
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
ברוכים הבאים ל-Pygame: בועות קופצותWelcome to Pygame: Bubble Bounce
התקנת pygame-ce, לולאת המשחק, ציור, תנועה, כבידה ואירועי עכבר.
Installing pygame-ce, the game loop, drawing, movement, gravity and mouse events.
הנקודות החשובותKey points
pip install pygame-cepip install pygame-cepygame.display.set_mode((w, h))pygame.display.set_mode((w, h))- אירועים → עדכון → ציורEvents → update → draw
QUITסוגר את החלוןQUITcloses the window- (0, 0) בפינה השמאלית העליונה(0, 0) is the top-left corner
- y גדל כלפי מטה!y grows downward!
- מהירות:
vx,vyבכל פרייםvelocity:vx,vyper frame - מיקום += מהירותposition += velocity
- פגיעה בקיר = הופכים את סימן המהירותHitting a wall = flip the velocity's sign
מונחיםTerms
- pygamepygame: ספרייה לבניית משחקים דו-ממדיים בפייתון: חלון, ציור, מקלדת, עכבר וצלילים. מתקינים את הגרסה המתוחזקת:
pip install pygame-ce.A library for building 2D games in Python: a window, drawing, keyboard, mouse and sound. Install the maintained version:pip install pygame-ce. - לולאת משחקgame loop: הלולאה שרצה עשרות פעמים בשנייה: קוראת אירועים, מעדכנת את מצב המשחק, ומציירת את המסך מחדש.The loop running dozens of times a second: read events, update the game state, redraw the screen.
- פרייםframe (game): תמונה אחת של המשחק. 60 פריימים בשנייה (FPS) נראים כתנועה רציפה.One picture of the game. 60 frames per second (FPS) look like smooth motion.
- קואורדינטות מסךscreen coordinates: מיקום לפי (x, y) בפיקסלים: (0, 0) בפינה השמאלית העליונה, x גדל ימינה ו-y גדל למטה.Position as (x, y) in pixels: (0, 0) is the top-left corner, x grows to the right and y grows down.
- מהירות (וקטור)velocity: כמה פיקסלים זזים בכל פריים בכל ציר:
vx,vy. מהירות שלילית = תנועה שמאלה או למעלה.How many pixels to move per frame on each axis:vx,vy. Negative velocity = moving left or up. - אירועevent: משהו שקרה מאז הפריים הקודם: לחיצת מקש, לחיצת עכבר, סגירת חלון.
pygame.event.get().Something that happened since the last frame: a key press, a mouse click, closing the window.pygame.event.get().
Cheatsheet
חלוןWindow
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 320))
pygame.display.set_caption("Bubble Bounce")
screen.fill((18, 24, 48))
pygame.draw.circle(screen, (90, 180, 255), (240, 160), 50)לולאהLoop
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()
x = 40
running = TrueצורותShapes
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 320))
screen.fill((245, 240, 230))
ground = pygame.Rect(0, 270, 480, 50)
pygame.draw.rect(screen, (60, 160, 90), ground)תנועהMotion
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 320))
screen.fill((18, 24, 48))
x, y, vx, vy = 30, 280, 5, -3
for frame in range(80):הקפצהBounce
def bounce(pos, vel, r, size):
pos += vel
if pos - r < 0:
pos, vel = r, -vel
elif pos + r > size:
pos, vel = size - r, -vel
return pos, velמשחקGame
import random
import pygame
class Bubble:
def __init__(self, x, y, r, vx, vy, color):
self.x, self.y, self.r = x, y, rשגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
ValueError | צבע עם ערך מעל 255.A colour value above 255. | ValueError: invalid color (color sequence must have size 3 or 4, and each element must be an integer in the range [0, 255]): כל רכיב צבע הוא מ-0 עד 255.ValueError: invalid color (color sequence must have size 3 or 4, and each element must be an integer in the range [0, 255]): each colour component is 0 to 255. |
AssertionError | שוכחים למלא את המסך מחדש, וכל עיגול משאיר שובל.Forgetting to clear the screen, so every circle leaves a trail. | AssertionError: 8 circles on screen: ...: בלי screen.fill בתחילת כל פריים, הציורים הקודמים נשארים. ממלאים רקע ואז מציירים.AssertionError: 8 circles on screen: ...: without screen.fill at the start of each frame, earlier drawings stay. Fill the background, then draw. |
AssertionError | חושבים ש-y גדל כלפי מעלה.Thinking y grows upward. | AssertionError: the sky was drawn at y=300 ...: ב-pygame y גדול = נמוך יותר במסך. הקרקע בערכי y גבוהים.AssertionError: the sky was drawn at y=300 ...: in pygame a bigger y is lower on screen. The ground has high y values. |
AssertionError | משנים מיקום למספר עשרוני ומעבירים ישר לציור.Turning a position into a float and passing it straight to drawing. | AssertionError: x is 11.2...: מחזיקים מיקום עשרוני לתנועה חלקה, ומעגלים (round) רק בשורת הציור.AssertionError: x is 11.2...: keep a float position for smooth motion, and round (round) only on the drawing line. |
AssertionError | בודקים את המרכז במקום את הקצה, והבועה נכנסת חצי לתוך הקיר.Checking the centre instead of the edge, so the bubble sinks halfway into the wall. | AssertionError: centre at x=470 says no wall, but the edge is at 490: ...: הבועה כבר חצתה את הקיר. בודקים x + r > width.AssertionError: centre at x=470 says no wall, but the edge is at 490: ...: the bubble already crossed the wall. Check x + r > width. |
AssertionError | מוחקים מרשימה בזמן שעוברים עליה בלולאה.Removing from a list while looping over it. | AssertionError: left [2, 4]: removing while looping skips items: אחרי מחיקה הרשימה מתקצרת והלולאה מדלגת. לפוצץ אחת: break מיד אחרי remove (כמו בקוד), או לבנות רשימה חדשה.AssertionError: left [2, 4]: removing while looping skips items: after removing, the list shrinks and the loop skips one. To pop one: break right after remove (as in the code), or build a new list. |