最近无聊试着学习python,乱七八糟的资料看了一堆,还是不知道怎么写,算了,老办法直接动手搞几个项目估计也就差不多了,写个游戏也许也将兴趣持续得更久一点吧。
pygame用来写游戏正好,这玩意儿是干嘛的就不多说了,百度上都有。
环境:python 2.7 +pygame 1.9.2 win32 for py2.7
IDE:netbeans 8.0.2 + python插件 ,本人用不惯eclipse没办法
本来是用python 3.2写的,但是3.2没有py2exe支持,又降了一下,还好差别也就一个类覆盖方法而已
FC红白机上的坦克很多人都玩过的,下载个模拟器,然后边玩边取资源(图片、声音),以前用delphiX搞过一回,东西都有,直接开始了
先吐个槽,pygame的Sound很不好用,用WAV格式时参数太难调 ,播放声音要么太快,要么声音文件太大,用OGG格式吧
这还没完呢,声音播放响应还有问题,现在资料少也不知道是什么原因造成的,反正一开火就播放声音,结果子弹都飞出去好一会儿了,开火音才响起
接下来先定义个常量文件const.py
#常用颜色
BLACK = (0, 0, 0)
SILVER = (192, 192, 192)
GRAY = (128, 128, 128)
WHITE = (255, 255, 255)
ORANGE = (255, 128, 0)
HIRED = (192, 32, 0)
#敌我识别
WHO_BORDER = -1
#敌我识别(无,障碍,玩家1,玩家2,敌方)
WHO_NONE, WHO_BALK, WHO_FOOD, WHO_PLAYER1, WHO_PLAYER2, WHO_ENEMY = range(6)
#精灵列表(窝,地面,活动目标,子弹,草,宝)
LIST_SPADE, LIST_BALKS, LIST_TANKS, LIST_BULLET, LIST_GRASS, LIST_COVER = range(6)
LAYER_CFG = (LIST_SPADE, LIST_BALKS, LIST_TANKS, LIST_BULLET, LIST_GRASS, LIST_COVER)
#图块, 空地和两个8*8的砖块
M_00 , M_01, M_02 = -1, -2, -3
#图块类型(M开头表示地图元素,P开头为道具,T开头为坦克,E开关为效果,S为分数,F为过关场景中的元素
M_BA, M_BU, M_BD, M_BL, M_BR, M_B1, M_B2, M_B3, M_B4, \
M_H0, M_C0, M_I0, M_S0, M_R1, M_R2, \
M_FG, M_TE, M_P1, M_P2, M_OVER, E_000, \
E_NEW, E_EXP, E_CAP, \
P_SPAD, P_STAR, P_TIMR, P_CAPS, P_BOMB, P_LIFE, \
T_Y01, T_Y02, T_Y03, T_Y04, T_G01, T_G02, T_G03, T_G04, \
T_E1N, T_E1S, T_E2N, T_E2S, T_E3N, T_E3S, \
T_E4N, T_E4Y, T_E4G, T_E4S, \
S_100, S_200, S_300, S_400, S_500, \
F_E01, F_E02, F_E03, F_E04, F_FLG, F_FRG, F_001, F_002, F_003, F_004 = range(63)
#声音资源
SND_START, SND_PAUSE, SND_OVER, SND_SPEED1, SND_SPEED2, SND_SHOOT, SND_BORDER, SND_BRICK, \
SND_STEEL, SND_KILL, SND_SPECIAL, SND_PROPS, SND_INCSCORE, SND_ADDSCORE, SND_ADDLIFE = range(15)
CHNNEL_CFG = {
SND_START:0, SND_PAUSE:0, SND_OVER:0, SND_SPECIAL:0, SND_PROPS:0, SND_ADDLIFE:0,
SND_SPEED1:1, SND_SPEED2:1, SND_SHOOT:2, SND_BORDER:3, SND_BRICK:4, SND_STEEL:5,
SND_KILL:6, SND_INCSCORE:7, SND_ADDSCORE:7
}
#运行状态
SS_NONE, SS_PLAY, SS_EMPTY1, SS_EMPTY2, SS_PASS, SS_OVER = range(6)
#方向
DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT, \
KEY_SELECT, KEY_START, KEY_S1, KEY_S2, KEY_S3, KEY_S4 = range(10)
MOVE_PIXES = 1.5
STAGE_ENEMY = 20 #每关多少敌坦克
MYGOD_DURATION = 60 * 4 #出兵时无敌时间
TOGOD_DURATION = 60 * 10 #加宝后无敌时间
SCORE_DURATION = 60 * 1 #分数显示持续时间
ENEMY_INTERVAL = 60 * 8 #敌人每多少帧出兵一个
FOODS_DURATION = 60 * 10 #宝物显示持续时间
ESTOP_DURATION = 60 * 10 #敌人定住持续时间
SPADE_DURATION = 60 * 15 #老窝保护持续时间
#玩家坦克等级对应形象
TANK_CFG = {
WHO_PLAYER1:{0:T_Y01, 1:T_Y02, 2:T_Y03, 3:T_Y04},
WHO_PLAYER2:{0:T_G01, 1:T_G02, 2:T_G03, 3:T_G04}
}
PLAYER_CFG = (WHO_PLAYER1, WHO_PLAYER2)
#方向配置
DIR_CFG = {
DIR_UP:{"angle":0, "move":(0, -1)},
DIR_LEFT:{"angle":90, "move":(-1, 0)},
DIR_RIGHT:{"angle":-90, "move":(1, 0)},
DIR_DOWN:{"angle":180, "move":(0, 1)}
}
SHOOT_CFG = (KEY_S1, KEY_S2, KEY_S3, KEY_S4)
#砖头击中不同位置反应表
BRICK_TAB = {
DIR_UP:{M_BA:M_BU, M_BU:M_00, M_BD:M_00, M_BL:M_B1, M_BR:M_B2, M_B1:M_00, M_B2:M_00, M_B3:M_00, M_B4:M_00},
DIR_LEFT:{M_BA:M_BL, M_BU:M_B1, M_BD:M_B3, M_BL:M_00, M_BR:M_00, M_B1:M_00, M_B2:M_00, M_B3:M_00, M_B4:M_00},
DIR_RIGHT:{M_BA:M_BR, M_BU:M_B2, M_BD:M_B4, M_BL:M_00, M_BR:M_00, M_B1:M_00, M_B2:M_00, M_B3:M_00, M_B4:M_00},
DIR_DOWN:{M_BA:M_BD, M_BU:M_00, M_BD:M_00, M_BL:M_B3, M_BR:M_B4, M_B1:M_00, M_B2:M_00, M_B3:M_00, M_B4:M_00}
}
#老窝周围坐标(16*16)
SPADE_CFG = {
(48+5*32,48+11*32):M_BA, (32+6*32,48+11*32):M_BA, (48+6*32,48+11*32):M_BA, (32+7*32,48+11*32):M_BA,
(48+5*32,32+12*32):M_BA, (32+7*32,32+12*32):M_BA,
(48+5*32,48+12*32):M_BA, (32+7*32,48+12*32):M_BA
}
#图像块配置
IMG_CFG = {
M_01:{"frame":1, "rect1":((4*32+8, 3*32+16), (8, 8))}, #显示片头用的砖块1
M_02:{"frame":1, "rect1":((4*32+8, 3*32+24), (8, 8))}, #显示片头用的砖块2
M_H0:{"frame":2, "rect1":((0*32, 3*32), (32, 32)), "rect2":((1*32, 3*32), (32, 32))}, #窝
M_C0:{"frame":1, "rect1":((4*32+16, 3*32), (16, 16))}, #草
M_I0:{"frame":1, "rect1":((5*32, 3*32), (16, 16))}, #冰
M_S0:{"frame":1, "rect1":((5*32+16, 3*32), (16, 16))}, #钢
M_R1:{"frame":2, "rect1":((6*32, 3*32), (16, 16)), "rect2":((6*32+16, 3*32), (16, 16))}, #河1
M_R2:{"frame":2, "rect1":((6*32+16, 3*32), (16, 16)), "rect2":((6*32, 3*32), (16, 16))}, #河2
M_BA:{"frame":1, "rect1":((2*32, 3*32), (16, 16))}, #整砖
M_BU:{"frame":1, "rect1":((2*32+16, 3*32), (16, 16))}, #上半砖
M_BD:{"frame":1, "rect1":((3*32, 3*32), (16, 16))}, #下半砖
M_BL:{"frame":1, "rect1":((3*32+16, 3*32), (16, 16))}, #左半砖
M_BR:{"frame":1, "rect1":((4*32, 3*32), (16, 16))}, #右半砖
M_B1:{"frame":1, "rect1":((2*32, 3*32+16), (16, 16))},
M_B2:{"frame":1, "rect1":((2*32+16, 3*32+16), (16, 16))},
M_B3:{"frame":1, "rect1":((3*32, 3*32+16), (16, 16))},
M_B4:{"frame":1, "rect1":((3*32+16, 3*32+16), (16, 16))},
M_FG:{"frame":1, "rect1":((6*32, 1*32), (32, 32))}, #旗帜
M_TE:{"frame":1, "rect1":((4*32+16, 3*32+16), (16, 16))},#敌方剩余坦克
M_P1:{"frame":1, "rect1":((5*32, 3*32+16), (32, 32))}, #玩家剩余坦克
M_P2:{"frame":1, "rect1":((6*32, 3*32+16), (32, 32))}, #玩家剩余坦克
E_000:{"frame":1, "rect1":((4*32, 3*32+16), (5, 10))}, #子弹
E_NEW:{"frame":7, "rect1":((0*32, 0*32), (32, 32)), "rect2":((1*32, 0*32), (32, 32)), "rect3":((2*32, 0*32), (32, 32)), "rect4":((3*32, 0*32), (32, 32)), "rect5":((4*32, 0*32), (32, 32)), "rect6":((5*32, 0*32), (32, 32)), "rect7":((6*32, 0*32), (32, 32))},
E_CAP:{"frame":2, "rect1":((0*32, 1*32), (32, 32)), "rect2":((1*32, 1*32), (32, 32))},
E_EXP:{"frame":5, "rect1":((0*32, 4*32), (32, 32)), "rect2":((1*32, 4*32), (32, 32)), "rect3":((2*32, 4*32), (32, 32)), "rect4":((3*32, 4*32), (32, 32)), "rect5":((4*32, 4*32), (32, 32))},
M_OVER:{"frame":1, "rect1":((4*32, 9*32), (32, 32))}, #游戏结束
P_SPAD:{"frame":2, "rect1":((0*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #锹
P_STAR:{"frame":2, "rect1":((1*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #五星(加一级火力)
P_TIMR:{"frame":2, "rect1":((2*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #定时器
P_CAPS:{"frame":2, "rect1":((3*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #无敌状态
P_BOMB:{"frame":2, "rect1":((4*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #爆炸
P_LIFE:{"frame":2, "rect1":((5*32, 2*32), (32, 32)), "rect2":((6*32, 2*32), (32, 32))}, #加一命
T_Y01:{"frame":2, "rect1":((0*32, 5*32), (32, 32)), "rect2":((1*32, 5*32), (32, 32))}, #玩家1黄色
T_Y02:{"frame":2, "rect1":((0*32, 6*32), (32, 32)), "rect2":((1*32, 6*32), (32, 32))},
T_Y03:{"frame":2, "rect1":((0*32, 7*32), (32, 32)), "rect2":((1*32, 7*32), (32, 32))},
T_Y04:{"frame":2, "rect1":((0*32, 8*32), (32, 32)), "rect2":((1*32, 8*32), (32, 32))},
T_G01:{"frame":2, "rect1":((2*32, 5*32), (32, 32)), "rect2":((3*32, 5*32), (32, 32))}, #玩家2绿色
T_G02:{"frame":2, "rect1":((2*32, 6*32), (32, 32)), "rect2":((3*32, 6*32), (32, 32))},
T_G03:{"frame":2, "rect1":((2*32, 7*32), (32, 32)), "rect2":((3*32, 7*32), (32, 32))},
T_G04:{"frame":2, "rect1":((2*32, 8*32), (32, 32)), "rect2":((3*32, 8*32), (32, 32))},
T_E1N:{"frame":2, "rect1":((4*32, 5*32), (32, 32)), "rect2":((5*32, 5*32), (32, 32))}, #轻型坦克
T_E1S:{"frame":3, "rect1":((4*32, 5*32), (32, 32)), "rect2":((5*32, 5*32), (32, 32)), "rect3":((6*32, 5*32), (32, 32))},
T_E2N:{"frame":2, "rect1":((4*32, 6*32), (32, 32)), "rect2":((5*32, 6*32), (32, 32))}, #突击坦克
T_E2S:{"frame":3, "rect1":((4*32, 6*32), (32, 32)), "rect2":((5*32, 6*32), (32, 32)), "rect3":((6*32, 6*32), (32, 32))},
T_E3N:{"frame":2, "rect1":((4*32, 7*32), (32, 32)), "rect2":((5*32, 7*32), (32, 32))}, #中型坦克
T_E3S:{"frame":3, "rect1":((4*32, 7*32), (32, 32)), "rect2":((5*32, 7*32), (32, 32)), "rect3":((6*32, 7*32), (32, 32))},
T_E4N:{"frame":2, "rect1":((4*32, 8*32), (32, 32)), "rect2":((5*32, 8*32), (32, 32))}, #重型坦克白色
T_E4S:{"frame":3, "rect1":((4*32, 8*32), (32, 32)), "rect2":((5*32, 8*32), (32, 32)), "rect3":((6*32, 8*32), (32, 32))},
T_E4Y:{"frame":2, "rect1":((0*32, 9*32), (32, 32)), "rect2":((1*32, 9*32), (32, 32))}, #重型坦克黄色
T_E4G:{"frame":2, "rect1":((2*32, 9*32), (32, 32)), "rect2":((3*32, 9*32), (32, 32))}, #重型坦克绿色
S_100:{"frame":1, "rect1":((4*32, 9*32), (32, 16))}, #100
S_200:{"frame":1, "rect1":((4*32, 9*32+16), (32, 16))},#200
S_300:{"frame":1, "rect1":((5*32, 9*32), (32, 16))}, #300
S_400:{"frame":1, "rect1":((5*32, 9*32+16), (32, 16))},#400
S_500:{"frame":1, "rect1":((6*32, 9*32), (32, 16))}, #500
F_E01:{"frame":1, "rect1":((2*32, 1*32), (32, 32))}, #过关场景中的坦克1
F_E02:{"frame":1, "rect1":((3*32, 1*32), (32, 32))}, #过关场景中的坦克2
F_E03:{"frame":1, "rect1":((4*32, 1*32), (32, 32))}, #过关场景中的坦克3
F_E04:{"frame":1, "rect1":((5*32, 1*32), (32, 32))}, #过关场景中的坦克4
F_FLG:{"frame":1, "rect1":((5*32, 4*32+16), (16, 16))}, #左箭头
F_FRG:{"frame":1, "rect1":((5*32+16, 4*32+16), (16, 16))}, #右箭头
F_001:{"frame":1, "rect1":((6*32, 4*32+16), (14, 14))}, #I
F_002:{"frame":1, "rect1":((6*32+16, 4*32+16), (14, 14))},#II
F_003:{"frame":1, "rect1":((6*32, 9*32+16), (14, 14))}, #I
F_004:{"frame":1, "rect1":((6*32+16, 9*32+16), (14, 14))} #II
}