2012-01-07 156 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