将图像放在Pygame网格上

发布于 2025-02-01 00:34:48 字数 1810 浏览 1 评论 0原文

红色和蓝色的侧面每个都将其瓷砖替换为图像(没有一个图像将它们全部重叠,而是一个图像粘贴到每个瓷砖上)。我不太确定该如何解决,任何帮助都将不胜感激!

import pygame

TILESIZE = 22
margin = 1
BOARD_POS = (10, 10)


def create_board_surf():
    board_surf = pygame.Surface((TILESIZE * 50 + margin * 51, TILESIZE * 26 + margin * 27))
    board_surf.set_colorkey((0, 0, 0))
    board_surf.set_alpha(210)
    for row in range(26):
        for column in range(0, 25):
            color = (170, 70, 70)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)
        for column in range(25, 50):
            color = (70, 95, 195)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)
    return board_surf


def create_board():
    board = []
    for row in range(26):
        board.append([])
        for column in range(50):
            board[row].append(None)
    return board


def main():
    global screen
    pygame.init()
    screen = pygame.display.set_mode((1200, 650))
    clock = pygame.time.Clock()

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        create_board()
        board_surf = create_board_surf()
        screen.fill(pygame.Color((200, 200, 200)))
        screen.blit(board_surf, BOARD_POS)
        pygame.display.flip()
        clock.tick(60)


main()

The red and blue sides would each have their tiles replaced by an image (not one image overlapping them all, but one image pasted onto each tile). I'm not really sure how to go about it, and any help would be appreciated!

enter image description here

import pygame

TILESIZE = 22
margin = 1
BOARD_POS = (10, 10)


def create_board_surf():
    board_surf = pygame.Surface((TILESIZE * 50 + margin * 51, TILESIZE * 26 + margin * 27))
    board_surf.set_colorkey((0, 0, 0))
    board_surf.set_alpha(210)
    for row in range(26):
        for column in range(0, 25):
            color = (170, 70, 70)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)
        for column in range(25, 50):
            color = (70, 95, 195)
            rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
            pygame.draw.rect(board_surf, pygame.Color(color), rect)
    return board_surf


def create_board():
    board = []
    for row in range(26):
        board.append([])
        for column in range(50):
            board[row].append(None)
    return board


def main():
    global screen
    pygame.init()
    screen = pygame.display.set_mode((1200, 650))
    clock = pygame.time.Clock()

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        create_board()
        board_surf = create_board_surf()
        screen.fill(pygame.Color((200, 200, 200)))
        screen.blit(board_surf, BOARD_POS)
        pygame.display.flip()
        clock.tick(60)


main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2025-02-08 00:34:48

def create_board_surf()下:在每个瓷砖绘制矩形的位置,替换pygame.draw.Rect board_surf.blit(image_surface(image_surface), ) *列 +边距,(保证金 +图块) *行 +边距)将图像放置在瓷砖矩形所在的位置。

您可以制作image_surface by img = pygame.image.image.load(“ imagename.png”)>
这些图像必须缩放到瓷砖尺寸,以使它们正确拟合:
img = pygame.transform.scale(img,(tilesize,tilesize)))

如果某些图块应具有不同的图像,则可以使用列和行位置来确定要使用的图像,以相同的方式使用有些瓷砖是红色的,而另一些则根据其列为蓝色。

Under def create_board_surf(): where you draw rectangles for each tile, replace the pygame.draw.rect with board_surf.blit(IMAGE_SURFACE, ((margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin)) to put an image where the tile rectangle would of been.

You can make the IMAGE_SURFACE by img = pygame.image.load("IMAGENAME.png")
These images would have to be scaled to the tile size so they fit properly:
img = pygame.transform.scale(img, (TILESIZE, TILESIZE))

If some tiles should have different images, you could maybe use the column and row location to determine which image to use, the same way some tiles are red while others are blue based on their column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文