你如何用python / pygame覆盖瓷砖(图像)中的某个区域?

时间:2012-01-07 14:43:12

标签: python pygame tile

我想要做的是(0,390),(0,450),(610,390),(610,450) - 覆盖在名为(brick_tile)的图块图像中。到目前为止我只有这个:

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 个答案:

答案 0 :(得分:2)

要用砖块平铺,你只需要在一个循环中进行blit,blit,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()