Menu
CoddyTech

רקע מרוצף

חלק מהיחידה עולמות במסלול ה-Pygame של Coddy. שיעור 9 מתוך 35.

רקע בצבע אחיד מאחורי השלב נראה ריק. assets/backgrounds/clouds.png היא תמונה בגודל 72 על 72 פיקסלים, שנועדה לחזור על עצמה: הקצה השמאלי שלה מתאים לקצה הימני שלה, והקצה העליון מתאים לקצה התחתון שלה. צייר אותה ברשת שמכסה את החלון:

for y in range(0, 360, 72):
    for x in range(0, 576, 72):
        screen.blit(background, (x, y))

range(0, 576, 72) נותן את הערכים 0, 72, 144 וכן הלאה עד 504, נקודת ההתחלה האחרונה שעדיין מוצגת. 576 הוא כפולה של 72, ולכן העותק האחרון מסתיים בקצה החלון. הרקע מחליף את screen.fill: הוא מכסה כל פיקסל.

לסדר יש חשיבות. כל מה שתצייר מאוחר יותר יכסה את מה שכבר נמצא שם, לכן צייר קודם את הרקע, ואז את האריחים מעליו.

challenge icon

אתגר

קל

קוד ההתחלה טוען את assets/backgrounds/clouds.png לתוך background.

  1. בלולאת המשחק, במקום screen.fill, צייר את background בכל ערך של x מ־0 עד 576, בקפיצות של 72, עבור כל ערך של y מ־0 עד 360, בקפיצות של 72.
  2. צייר את האריחים אחרי הרקע.

נסו בעצמכם

import pygame

TILE = 36

LEVEL = [
    "###.............",
    "###.............",
    "..........C.....",
    ".........####...",
    "..C.............",
    ".####......C....",
    "..........###...",
    "#....C.........#",
    "###..##...###.##",
    "######..########",
]

pygame.init()
screen = pygame.display.set_mode((576, 360))
pygame.display.set_caption("Tiles")
clock = pygame.time.Clock()

grass_img = pygame.image.load("assets/images/grass.png").convert_alpha()
grass_left_img = pygame.image.load("assets/images/grass_left.png").convert_alpha()
grass_right_img = pygame.image.load("assets/images/grass_right.png").convert_alpha()
dirt_img = pygame.image.load("assets/images/dirt.png").convert_alpha()
coin_img = pygame.image.load("assets/images/coin.png").convert_alpha()
background = pygame.image.load("assets/backgrounds/clouds.png").convert_alpha()


def solid(row, col):
    if row < 0 or row >= len(LEVEL) or col < 0 or col >= len(LEVEL[0]):
        return False
    return LEVEL[row][col] == "#"


tiles = []
for row, line in enumerate(LEVEL):
    for col, char in enumerate(line):
        x, y = col * TILE, row * TILE
        if char == "#":
            if solid(row - 1, col):
                image = dirt_img
            elif not solid(row, col - 1) and solid(row, col + 1):
                image = grass_left_img
            elif solid(row, col - 1) and not solid(row, col + 1):
                image = grass_right_img
            else:
                image = grass_img
            tiles.append((image, pygame.Rect(x, y, TILE, TILE)))
        elif char == "C":
            tiles.append((coin_img, pygame.Rect(x, y, TILE, TILE)))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((140, 200, 240))
    for image, rect in tiles:
        screen.blit(image, rect)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

כל השיעורים ביחידה עולמות

תרגלו בעצמכם: Pygame אונליין