2012-01-07 166 views
1

我想要做的是(0,390),(0,450),(610,390),(610,450) - 覆盖在名为(brick_tile)的平铺图像中。到目前为止,所有我必须是这样的:如何在python/pygame中覆盖瓷砖(图片)的区域?

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() 

回答

2

用砖头瓦你就必须做块,块传输,块传输在一个循环:

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() 
+0

OK我补充说,该计划的结束。并且砖块不会变成 – enrique2334 2012-01-07 15:29:27

+0

您需要调用函数:'setup_background()'。我编辑了代码以显示我的意思。 – unutbu 2012-01-07 15:37:44

+0

'回溯(最近通话最后一个): 文件 “C:\用户\恩里克\ Dropbox的\ GAMEZ_PYGAME \ gamez.py”,14号线在 setup_background() NameError:名字 'setup_background' 不是defined' – enrique2334 2012-01-07 15:43:00