如何在python/pygame中用瓦片(图片)覆盖区域?

1 投票
1 回答
3503 浏览
提问于 2025-04-17 09:44

我想做的是在坐标(0,390)、(0,450)、(610,390)和(610,450)这几个点上,覆盖一张叫做(砖块图案)的图片。到目前为止,我只有这些代码:

import pygame
from pygame.locals import*

cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')

pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1

while running:
    screen.fill((pink))
    screen.blit(cloud_background,(0,0))
    screen.blit(brick_tile,(0,450))
    pygame.display.flip()

    event = pygame.event.poll()
    if event.type == pygame.QUIT: sys.exit()

1 个回答

2

要用砖块铺砖,你只需要在一个循环里不停地绘制(blit),就是这样简单:

import pygame
import sys
import itertools

cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')

pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1

def setup_background():
    screen.fill((pink))
    screen.blit(cloud_background,(0,0))
    brick_width, brick_height = brick_tile.get_width(), brick_tile.get_height()
    for x,y in itertools.product(range(0,610+1,brick_width),
                                 range(390,450+1,brick_height)):
        # print(x,y)
        screen.blit(brick_tile, (x, y))
    pygame.display.flip()

while running:
    setup_background()    
    event = pygame.event.poll()
    if event.type == pygame.QUIT: sys.exit()

撰写回答