Pygame地图编辑器 - 正确显示图像

时间:2015-11-10 22:02:55

标签: python image pygame

我一直在为我的游戏创建地图编辑器。基本上我使用矩阵建立一个网格,然后用20x20的瓷砖填充它,这些瓷砖具有指定的数值,可以根据鼠标位置和键盘按钮的点击进行更改。如果我想让左上方的图块颜色不同,我会将其分配给1:

[
 [1,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0],
]

然而,我现在遇到的问题是尝试blit大于瓷砖的图像,而不仅仅是改变瓷砖的颜色。由于瓷砖仅为20x20,因此仅显示20x20的图像。即使我将整个图像设置为blit,碰撞检测也无法正常工作,因为图像仍然只会更改一个选定的索引,而不是图像实际覆盖的所有索引? 换句话说,假设我的图像是40x40。如果我成功地将整个图像blit到左上角索引,对象仍然只会与那个20x20空间碰撞吗?

如果这是真的,那么当我将40x40图像blit时,是否会有一种方法可以使所有索引发生变化,所以它看起来像这样?

[
 [1,1,0,0,0,0,0,0,0,0],
 [1,1,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0],
]

以下是目前的完整代码:

import pygame

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
LEAST_DISPLAY_WIDTH = 0
MAX_DISPLAY_WIDTH = 800
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PURPLE = (255, 0, 255)
DIRT = (0, 0, 16, 16)
GREEN = (34,139,34)
YELLOW = (255,255,51)
BROWN = (210,180,140)
BLUE = (0, 255, 255)

pygame.init()
size = (DISPLAY_WIDTH, DISPLAY_WIDTH)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
game_exit = False

class Graph(object):
    def __init__(self, rows=None, columns=None):
        self.rows = rows
        self.columns = columns
        self.grid = []

    def make_graph(self):
        for rows in range(self.rows):
            self.grid.append([])
            for columns in range(self.columns):
                self.grid[rows].append(0)
        return self.grid

class Tile(object):
    def __init__(self, width=None, height=None, color=None, loadedimage=None):
        self.width = width
        self.height = height
        self.color = color
        self.image = pygame.Surface((self.width, self.height))
        if self.color != None:
            self.image.fill(self.color)
        self.loadedimage = loadedimage
        if self.loadedimage != None:
            self.image = pygame.image.load(str(loadedimage))
            self.rect = self.image.get_rect()
            self.width = self.rect.x
            self.height = self.rect.y
        self.image.convert()
        self.rect = self.image.get_rect()


graph = Graph(rows=60, columns=80)
graph.make_graph()

while not game_exit:
    for row in range(graph.rows):
        for column in range(graph.columns):
            if graph.grid[row][column] == 0:
                color = WHITE
                tile = Tile(width=20, height=20, color=color)
            elif graph.grid[row][column] == 1:
                color = None
                #testing for blitting images
                tile = Tile(width=20, height=20, color=color, loadedimage="poro.png")
                # print tile.rect.x, tile.rect.y
            elif graph.grid[row][column] == 2:
                color = BLUE
                tile = Tile(width=20, height=20, color=color)
            elif graph.grid[row][column] == 3:
                color = GREEN
                tile = Tile(width=20, height=20, color=color)
            elif graph.grid[row][column] == 4:
                color = BROWN
                tile = Tile(width=20, height=20, color=color)
            elif graph.grid[row][column] == 5:
                color = YELLOW
                tile = Tile(width=20, height=20, color=color)
            elif graph.grid[row][column] == 6:
                color = BLACK
                tile = Tile(width=20, height=20, color=color)
            screen.blit(tile.image, ((tile.width*column),(tile.height*row)))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_exit = True

        if event.type == pygame.KEYDOWN:
            pos = pygame.mouse.get_pos()
            column = pos[0] // tile.width
            row = pos[1] // tile.height
            if event.key == pygame.K_a:
                graph.grid[row][column] = 1
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)
            if event.key == pygame.K_s:
                graph.grid[row][column] = 2
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)
            if event.key == pygame.K_d:
                graph.grid[row][column] = 3
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)
            if event.key == pygame.K_f:
                graph.grid[row][column] = 4
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)
            if event.key == pygame.K_g:
                graph.grid[row][column] = 5
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)
            if event.key == pygame.K_h:
                graph.grid[row][column] = 6
                print "Position: {0}, Grid Coordinates: {1}, {2}".format(pos, row, column)

    clock.tick(60)
    pygame.display.flip()
print graph.grid
pygame.quit()

为方便起见,这是源和图片的链接:https://github.com/tear727/PygameMapEditor

1 个答案:

答案 0 :(得分:0)

解决图片问题的最简单方法是使用pygame.transform.scale(Surface, (width, height), DestSurface = None)将图片转换为图块大小,您可以阅读有关here的更多信息。

class Tile(object):
    def __init__(self, width=None, height=None, color=None, loadedimage=None):
        self.width = width
        self.height = height
        self.color = color
        self.image = pygame.Surface((self.width, self.height))
        if self.color != None:
            self.image.fill(self.color)
        self.loadedimage = loadedimage
        if self.loadedimage != None:
            self.image = pygame.image.load(str(loadedimage))

            # Scale image to tilesize
            self.image = pygame.transform.scale(self.image,(self.width,self.height))

            self.rect = self.image.get_rect()
            self.width = self.rect.width
            self.height = self.rect.height
        self.image.convert()
        self.rect = self.image.get_rect()

如果您确定图像的当前大小是合适的,那么碰撞只会落在一个原始图块下面会出现问题。然而,它也会;创建该图像的问题,覆盖它溢出的图块,反之亦然,使图像溢出到其他图块是一个坏主意。

如果绝对无法缩放图像,则应将图像分割为多个部分,然后将特定部分添加到各自的图块中。

您可以通过blitting图像的特定区域而不是整个图像来完成此操作。

screen.blit(tile.image,((tile.width*column),(tile.height*row)), (pos_on_image,tile_size) )

在他的方式中,整个图像可以在不缩放的情况下显示而不会使碰撞关闭或图像与其他图块重叠。

相关问题