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

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

Pixel PlatformerPixel Platformer

Tilemap, מצלמה, Coyote Time, Jump Buffer ומצבי אנימציה.

Tilemaps, a camera, coyote time, jump buffering and animation states.

או Ctrl+Por Ctrl+P

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

  • מפה: # קרקע, c מטבע, e אויבA map: # ground, c coin, e enemy
  • אריח = Rect בגודל TA tile = a Rect of size T
  • על הקרקע רק אחרי נחיתהGrounded only after landing
  • קפיצה = vy = -6 רק אם groundedA jump = vy = -6 only if grounded
  • הפרדת צירים: x ואז yAxis separation: x then y
  • ב-x: עוצרים בקירOn x: stop at the wall
  • מצלמה = היסט אופקיA camera = a horizontal offset
  • מציירים ב-rect.move(-camera, 0)Draw at rect.move(-camera, 0)
  • מטבע: נגיעה = נקודה ומחיקהA coin: a touch = a point and removal

מונחיםTerms

  • מפת אריחיםtilemap: עולם בנוי מאריחים קטנים זהים בגודלם, שמתוארים במפה כמו טקסט. כל תו הוא סוג אריח.A world built from small equal-size tiles, described in a text-like map. Each character is a tile type.
  • מצלמהcamera: היסט שמחסירים מכל מה שמציירים, כדי שהמסך יראה חלק מעולם גדול ויעקוב אחרי השחקן.An offset subtracted from everything you draw, so the screen shows part of a large world and follows the player.
  • על הקרקעgrounded: דגל שאומר שהשחקן עומד על משהו. מותר לקפוץ רק כשהוא אמת.A flag saying the player stands on something. Jumping is allowed only when it's true.
  • זמן קויוטהcoyote time: כמה פריימים אחרי שיורדים מקצה שבהם עדיין מותר לקפוץ. גורם למשחק להרגיש הוגן.A few frames after stepping off an edge during which jumping is still allowed. Makes the game feel fair.
  • סיורpatrol: אויב שהולך הלוך ושוב ומסתובב בקצה פלטפורמה או בקיר.An enemy walking back and forth, turning at a platform's edge or a wall.

Cheatsheet

אריחיםTiles
import pygame

LEVEL = [
    "..............................................",
    "..............................................",
    "......................c.c.....................",
    ".............c.c.....#####..........c.c.c.....",
קפיצהJump
def jump_height(hold_frames, gravity=0.35, jump=-6.2, cut=0.5):
    y, vy, top = 0.0, jump, 0.0
    for frame in range(200):
        if frame == hold_frames and vy < 0:
            vy *= cut
        vy += gravity
        y += vy
התנגשותCollision
import pygame

def move(player, dx, vy, solids):
    player.x += dx
    for s in solids:
        if player.colliderect(s):
            player.right = s.left if dx > 0 else player.right
מצלמהCamera
def camera_x(player_x, screen_w, world_w):
    return max(0, min(world_w - screen_w, player_x - screen_w // 3))

world_w, screen_w = 736, 480
for x in (20, 200, 400, 700):
    cam = camera_x(x, screen_w, world_w)
    print(f"player at {x:>3} -> camera {cam:>3} -> drawn at {x - cam}")
אויביםEnemies
import pygame

def stomp_or_hurt(player, vy, enemy):
    if not player.colliderect(enemy):
        return None
    if vy > 0 and player.bottom - vy <= enemy.centery:
        return "stomp"
משחקGame
import pygame

LEVEL = [
    "..............................................",
    "..............................................",
    "......................c.c.....................",
    ".............c.c.....#####..........c.c.c.....",

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

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
IndexErrorגישה למשבצת מחוץ למפה.Reading a tile outside the map.IndexError: list index out of range: יש רק 8 שורות (0 עד 7). כשבודקים אריחים ליד השחקן, מוודאים שהשורה והעמודה בתוך המפה.IndexError: list index out of range: there are only 8 rows (0 to 7). When checking tiles near the player, make sure the row and column are inside the map.
AssertionErrorמאפשרים קפיצה באוויר.Allowing a jump in mid-air.AssertionError: 6 jumps without landing: ...: השחקן עף. בודקים if jump_pressed and on_ground:.AssertionError: 6 jumps without landing: ...: the player flies. Check if jump_pressed and on_ground:.
AssertionErrorבודקים את שני הצירים יחד, והשחקן "נתפס" בפינות.Checking both axes together, so the player "snags" on corners.AssertionError: with x and y moved together ...: כשמזיזים בשני הצירים יחד, אי אפשר לדעת אם פגעתם בקיר או ברצפה. מזיזים ובודקים ציר אחרי ציר.AssertionError: with x and y moved together ...: moving on both axes at once, you can't tell whether you hit a wall or a floor. Move and check one axis at a time.
AssertionErrorמחסירים את המצלמה גם בלוגיקה, ולא רק בציור.Subtracting the camera in the logic too, not just when drawing.AssertionError: the player and the coin are 10px apart ...: התנגשויות ותנועה בקואורדינטות של העולם; המצלמה רק לציור.AssertionError: the player and the coin are 10px apart ...: collisions and movement in world coordinates; the camera only for drawing.
AssertionErrorאויב שנופל מהפלטפורמה כי לא בודק קרקע לפניו.An enemy walking off the platform because it doesn't check the ground ahead.AssertionError: the enemy walked to x=220, off the platform: ...: בודקים אריח מתחת לנקודה שלפני האויב, והופכים כיוון אם אין.AssertionError: the enemy walked to x=220, off the platform: ...: check the tile under the point ahead of the enemy, and turn if there isn't one.
AssertionErrorשחקן שנופל דרך הקרקע במהירות גבוהה.A player falling through the ground at high speed.AssertionError: falling at 42px a frame through 16px tiles: ...: מנהור! מגבילים מהירות נפילה (כאן 8) כך שתהיה קטנה מגודל אריח.AssertionError: falling at 42px a frame through 16px tiles: ...: tunneling! Cap the fall speed (here 8) below the tile size.

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