我对 Python/Pygame 完全陌生,我正在尝试将图像分割成次表面,然后将它们 blit 以显示在屏幕上(以便稍后我可以修改它以调用某个图块,然后 blit 它) .
E1:我想我终于设法以正确的方式对其进行切片,但是在 blitting 中出现了一点问题。
E2:更新了代码和错误(在我之前尝试过的大多数代码中都得到了这个)。
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
def load_tileset(filename, width, height):
image = pygame.image.load(filename).convert()
image_width, image_height = image.get_size()
tileset = []
for tile_x in range(0, image_width//width):
line = []
tileset.append(line)
for tile_y in range(0, image_height//height):
rect = (tile_x*width, tile_y*height, width, height)
line.append(image.subsurface(rect))
return tileset
def draw_background(screen, tile_img, field_rect):
img_rect = tile_img.get_rect()
for y in range(0,400,32):
for x in range(0,600,32):
screen.blit(tile_img, (x,y))
Field_Rect = Rect(50,50, 300,300)
tileset = load_tileset("platform1.bmp", 17, 17)
bg_tile_img = tileset[0]
draw_background(screen, bg_tile_img, Field_Rect)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
pygame.display.flip()
它给 atm 的错误是:
img_rect = tile_img.get_rect()
AttributeError: 'list' object has no attribute 'get_rect'