Menu
CoddyTech

خلفية مبلطة

جزء من قسم العوالم في رحلة Pygame على Coddy. الدرس 9 من 36.

يبدو المستوى فارغًا عندما يكون خلفيته بلون سادة. الصورة 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

التحدي

سهل

املأ السماء بخلفية سحابية متكررة.

  1. بدلًا من screen.fill، ارسم background (المحمّلة في المشروع المبدئي) عند كل قيمة x من 0 إلى 576 وكل قيمة 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()
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس العوالم

تدرّب بنفسك: Playground لـ Pygame