• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
Deepcoldwing
帮助1

前言

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

本节最终实现的效果

学新通

实现原理

实现原理其实比较好理解,首先将游戏窗口想象为大小一样的方格,如下图所示:
学新通
然后使用贴图对窗口中的方格进行填充,使用不同的编号代表不同的贴图,然后再将其加载到游戏窗口中:
学新通
这样我们就可以将地图表示为:

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

如果地图很大

如果地图很大,一个一个编辑将会非常的麻烦,推荐使用tiled贴图编辑器。这里就不详细描述了,关于这个软件的教程,网上有很多,一看就会,编辑好之后导出csv文件就可以使用了:
学新通
csv文件的格式如下图所示:
学新通

实现代码如下:

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()
学新通

项目结构及使用到的素材

项目结构

学新通

使用到的图片素材

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggciff
系列文章
更多 icon
同类精品
更多 icon
继续加载