pygame编写的坦克游戏(三)

资源准备工作:

1、地图文件

.....r.lCC...
.lR.l...rClrC
.lR.l.A.rClrC
.A..A.G.AC.AC
...rU.A.uL.CC
AAl..CAC..rAA
....rCCCl....
GAA.uCCCurAAG
UUU.d.C.d.UUU
.A..A...A....
.Al..u.u..rAC
..u..4d3..CCC
..d..rWl..dCC

13x13的格子,每个字节点32x32像素,分别为8x8x4(4个小图块组成),在我的设置里字节含义大概是这样:

"l,r,u,d"小写上下左右表示1/2个砖头,“LRUD”大写上下左右表示1/2个钢板,“1234”表示四个角上1/8个砖头,

“C”草,“A"整个砖头,”H"河,“B"冰,”G"整个钢板,“W"老窝

2、图像资源,在《pygame编写的坦克游戏(三)》里已经贴了图,按常量文件配置可以取到

3、声音资源:这个只能手工录制了,后面会给出整个源码的链接,就包含在里面了


上地图代码:

import os, sys
from const import *
from block import *

class Map:
    #地图
    _CONFIG = {
        "C":{"grid":(M_C0, M_C0, M_C0, M_C0), "class":Block}, 
        "H":{"grid":(M_R1, M_R2, M_R2, M_R1), "class":River}, 
        "B":{"grid":(M_I0, M_I0, M_I0, M_I0), "class":Ice}, 
        "G":{"grid":(M_S0, M_S0, M_S0, M_S0), "class":Steel}, 
        "U":{"grid":(M_S0, M_S0, M_00, M_00), "class":Steel}, 
        "L":{"grid":(M_S0, M_00, M_S0, M_00), "class":Steel}, 
        "R":{"grid":(M_00, M_S0, M_00, M_S0), "class":Steel}, 
        "D":{"grid":(M_00, M_00, M_S0, M_S0), "class":Steel}, 
        "A":{"grid":(M_BA, M_BA, M_BA, M_BA), "class":Brick}, 
        "u":{"grid":(M_BA, M_BA, M_00, M_00), "class":Brick}, 
        "l":{"grid":(M_BA, M_00, M_BA, M_00), "class":Brick}, 
        "r":{"grid":(M_00, M_BA, M_00, M_BA), "class":Brick}, 
        "d":{"grid":(M_00, M_00, M_BA, M_BA), "class":Brick}, 
        "1":{"grid":(M_BA, M_00, M_00, M_00), "class":Brick}, 
        "2":{"grid":(M_00, M_BA, M_00, M_00), "class":Brick}, 
        "3":{"grid":(M_00, M_00, M_BA, M_00), "class":Brick}, 
        "4":{"grid":(M_00, M_00, M_00, M_BA), "class":Brick}, 
        "W":{"grid":(M_H0, M_00, M_00, M_00), "class":Base}, 
        }    
    @classmethod
    def load(self, engine, level):
        self.grid = []
        map_path = os.getcwd() + os.sep + "map" + os.sep
        fname = map_path + "level%.2d.txt"%(level)
        
        map_cfg = []
        try:
            f = open(fname)
            for line in f.readlines():
                map_cfg.append(line)
        finally:
            f.close()
        
        #构造地图上的对象
        for i in range(len(map_cfg)):
            for j in range(len(map_cfg[i]) - 1):
                ctype = map_cfg[i][j]
                self.create_mapblock(engine, ctype, j, i)
    
    @classmethod
    def create_mapblock(self, engine, ctype, x, y):
        if ctype in Map._CONFIG.keys():
            cfg = Map._CONFIG[ctype]
            imp = cfg["class"]
            grid = cfg["grid"]
            delay = 35 if ctype == "B" else 0            
            for t in range(len(grid)):
                if grid[t] !=  M_00:
                    pos = [32 + (x * 2 + t % 2) * 16, 32 + (y * 2 + t // 2) * 16]                    
                    #0 6 12三个位置的不初始化
                    if (y == 0) and (x in [0, 6, 12]):
                        continue
                    #保护老窝的砖自动生成(砖可以加锹变钢板,持续10秒,闪烁5秒后还原成砖)                    
                    pt = (pos[0], pos[1])
                    if pt in SPADE_CFG.keys():
                        layer = LIST_SPADE
                    elif ctype == "C":
                        layer = LIST_GRASS
                    else:
                        layer = LIST_BALKS
                    impclass = imp
                    impclass(engine, WHO_BALK, grid[t], pos, delay, layer)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值