pygame系列教程——使用tile贴片实现游戏地图

文章目录

  • 前言
  • 本节最终实现的效果
  • 实现原理
    • 如果地图很大
  • 实现代码如下:
  • 项目结构及使用到的素材
    • 项目结构
    • 使用到的图片素材

前言

在上一节中,我们了解了图片的导入及显示。那么接下来我们就使用上一节中的图片导入知识,使用tiles贴图来实现游戏地图的构建。

本节最终实现的效果

pygame系列教程——使用tile贴片实现游戏地图_第1张图片

实现原理

实现原理其实比较好理解,首先将游戏窗口想象为大小一样的方格,如下图所示:
pygame系列教程——使用tile贴片实现游戏地图_第2张图片
然后使用贴图对窗口中的方格进行填充,使用不同的编号代表不同的贴图,然后再将其加载到游戏窗口中:
pygame系列教程——使用tile贴片实现游戏地图_第3张图片
这样我们就可以将地图表示为:

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

如果地图很大

如果地图很大,一个一个编辑将会非常的麻烦,推荐使用tiled贴图编辑器。这里就不详细描述了,关于这个软件的教程,网上有很多,一看就会,编辑好之后导出csv文件就可以使用了:
pygame系列教程——使用tile贴片实现游戏地图_第4张图片
csv文件的格式如下图所示:
pygame系列教程——使用tile贴片实现游戏地图_第5张图片

实现代码如下:

import csv
import os
import pygame

# -----------------------------------对象定义---------------------------------

class World(object):
    def __init__(self):
        self.tile_list = []
        self.data = self.load_data("./level_0.csv")

        # load raw_images
        dirt_img = pygame.image.load('images/dirt.png')
        grass_img = pygame.image.load('./images/grassMid.png')

        row_count = 0
        for row in self.data:
            col_count = 0
            for tile in row:
                if tile == "0":
                    img = pygame.transform.scale(grass_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                if tile == "1":
                    img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                col_count += 1
            row_count += 1

    def load_data(self, filename):
        map = []
        with open(os.path.join(filename)) as data:
            data = csv.reader(data, delimiter=',')
            for row in data:
                map.append(list(row))
        return map

    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])


# ------------------------------加载基本的窗口和时钟----------------------------
# 使用pygame之前必须初始化
pygame.init()
# 设置标题
pygame.display.set_caption("MyGame")
# 设置用于显示的窗口,单位为像素
screen_width, screen_height = tile_size * 32, tile_size * 16
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()  # 设置时钟
tile_size = 50 # 单个磁贴的大小
# -------------------------------- 加载对象 ----------------------------------
# 加载图片
bg_img = pygame.image.load("./images/bg.png").convert()  # 背景图片
cloud_img = pygame.image.load("./images/cloud.png").convert_alpha()  # 背景图片
bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height))
cloud_img = pygame.transform.scale(cloud_img, (screen_width, screen_height))

world = World()

# ------------------------------- 游戏主循环 ---------------------------------
run = True
while run:
    clock.tick(60)
    # -------------------------------- 渲染对象 -------------------------------
    screen.blit(bg_img, (0, 0))
    screen.blit(cloud_img, (0, 0))
    world.draw()
    # ------------------------ 事件检测及状态更新 ------------------------------
    for event in pygame.event.get():  # 循环获取事件
        if event.type == pygame.QUIT:  # 若检测到事件类型为退出,则退出系统
            run = False
    # -------------------------- 窗口更新并绘制 -------------------------------
    pygame.display.update()  # 更新屏幕内容
pygame.quit()

项目结构及使用到的素材

项目结构

pygame系列教程——使用tile贴片实现游戏地图_第6张图片

使用到的图片素材

pygame系列教程——使用tile贴片实现游戏地图_第7张图片

你可能感兴趣的:(pygame系列,pygame,python,游戏开发)