pygame写的坦克大战,望大神前来指导

pygame写的坦克大战,望大神前来指导

这个游戏可以运行,其中常量部分是直接复制的别人的。这个游戏过程中有几个地方没解决,希望有大神能够给予好的办法建议。
第一个问题,敌方坦克打掉的墙虽然消失了,但是对于我方坦克还是存在的并且子弹打不穿。
第二个问题,如何解决我方坦克碰到墙后不可以继续移动的问题。我用检测相交来判断的,如果相交则stop否则stop为FALSE。结果相交过程中stop就为FALSE了。
第三个问题,这个问题我还没有写,就是写一个敌机组。每个敌机组都给敌机同样的初始位置。敌机死亡后会在这个地方重生并且有个重生过程,如果同一组的敌机死亡时间紧挨着,那么他们就会在同一地方重生,这样就重叠了,如何错时?
首先是常量的代码:
#coding=utf-8
#常用颜色
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)
#图块, 空地和两个88的砖块
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+532,48+1132):M_BA, (32+632,48+1132):M_BA, (48+632,48+1132):M_BA, (32+732,48+1132):M_BA,
(48+532,32+1232):M_BA, (32+732,32+1232):M_BA,
(48+532,48+1232):M_BA, (32+732,48+1232):M_BA
}
#图像块配置
IMG_CFG = {
M_01:{“frame”:1, “rect1”?(432+8, 332+16), (8, 8))}, #显示片头用的砖块1
M_02:{“frame”:1, “rect1”?(432+8, 332+24), (8, 8))}, #显示片头用的砖块2
M_H0:{“frame”:2, “rect1”?(032, 332), (32, 32)), “rect2”?(132, 332), (32, 32))}, #窝
M_C0:{“frame”:1, “rect1”?(432+16, 332), (16, 16))}, #草
M_I0:{“frame”:1, “rect1”?(532, 332), (16, 16))}, #冰
M_S0:{“frame”:1, “rect1”?(532+16, 332), (16, 16))}, #钢
M_R1:{“frame”:2, “rect1”?(632, 332), (16, 16)), “rect2”?(632+16, 332), (16, 16))}, #河1
M_R2:{“frame”:2, “rect1”?(632+16, 332), (16, 16)), “rect2”?(632, 332), (16, 16))}, #河2
M_BA:{“frame”:1, “rect1”?(232, 332), (16, 16))}, #整砖
M_BU:{“frame”:1, “rect1”?(232+16, 332), (16, 16))}, #上半砖
M_BD:{“frame”:1, “rect1”?(332, 332), (16, 16))}, #下半砖
M_BL:{“frame”:1, “rect1”?(332+16, 332), (16, 16))}, #左半砖
M_BR:{“frame”:1, “rect1”?(432, 332), (16, 16))}, #右半砖
M_B1:{“frame”:1, “rect1”?(232, 332+16), (16, 16))},
M_B2:{“frame”:1, “rect1”?(232+16, 332+16), (16, 16))},
M_B3:{“frame”:1, “rect1”?(332, 332+16), (16, 16))},
M_B4:{“frame”:1, “rect1”?(332+16, 332+16), (16, 16))},
M_FG:{“frame”:1, “rect1”?(632, 132), (32, 32))}, #旗帜
M_TE:{“frame”:1, “rect1”?(432+16, 332+16), (16, 16))},#敌方剩余坦克
M_P1:{“frame”:1, “rect1”?(532, 332+16), (32, 32))}, #玩家剩余坦克
M_P2:{“frame”:1, “rect1”?(632, 332+16), (32, 32))}, #玩家剩余坦克
E_000:{“frame”:1, “rect1”?(432, 332+16), (5, 10))}, #子弹
E_NEW:{“frame”:7, “rect1”?(032, 032), (32, 32)), “rect2”?(132, 032), (32, 32)), “rect3”?(232, 032), (32, 32)), “rect4”?(332, 032), (32, 32)), “rect5”?(432, 032), (32, 32)), “rect6”?(532, 032), (32, 32)), “rect7”?(632, 032), (32, 32))},
E_CAP:{“frame”:2, “rect1”?(032, 132), (32, 32)), “rect2”?(132, 132), (32, 32))},
E_EXP:{“frame”:5, “rect1”?(032, 432), (32, 32)), “rect2”?(132, 432), (32, 32)), “rect3”?(232, 432), (32, 32)), “rect4”?(332, 432), (32, 32)), “rect5”?(432, 432), (32, 32))},
M_OVER:{“frame”:1, “rect1”?(432, 932), (32, 32))}, #游戏结束
P_SPAD:{“frame”:2, “rect1”?(032, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #锹
P_STAR:{“frame”:2, “rect1”?(132, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #五星(加一级火力)
P_TIMR:{“frame”:2, “rect1”?(232, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #定时器
P_CAPS:{“frame”:2, “rect1”?(332, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #无敌状态
P_BOMB:{“frame”:2, “rect1”?(432, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #爆炸
P_LIFE:{“frame”:2, “rect1”?(532, 232), (32, 32)), “rect2”?(632, 232), (32, 32))}, #加一命
T_Y01:{“frame”:2, “rect1”?(032, 532), (32, 32)), “rect2”?(132, 532), (32, 32))}, #玩家1黄色
T_Y02:{“frame”:2, “rect1”?(032, 632), (32, 32)), “rect2”?(132, 632), (32, 32))},
T_Y03:{“frame”:2, “rect1”?(032, 732), (32, 32)), “rect2”?(132, 732), (32, 32))},
T_Y04:{“frame”:2, “rect1”?(032, 832), (32, 32)), “rect2”?(132, 832), (32, 32))},
T_G01:{“frame”:2, “rect1”?(232, 532), (32, 32)), “rect2”?(332, 532), (32, 32))}, #玩家2绿色
T_G02:{“frame”:2, “rect1”?(232, 632), (32, 32)), “rect2”?(332, 632), (32, 32))},
T_G03:{“frame”:2, “rect1”?(232, 732), (32, 32)), “rect2”?(332, 732), (32, 32))},
T_G04:{“frame”:2, “rect1”?(232, 832), (32, 32)), “rect2”?(332, 832), (32, 32))},
T_E1N:{“frame”:2, “rect1”?(432, 532), (32, 32)), “rect2”?(532, 532), (32, 32))}, #轻型坦克
T_E1S:{“frame”:3, “rect1”?(432, 532), (32, 32)), “rect2”?(532, 532), (32, 32)), “rect3”?(632, 532), (32, 32))},
T_E2N:{“frame”:2, “rect1”?(432, 632), (32, 32)), “rect2”?(532, 632), (32, 32))}, #突击坦克
T_E2S:{“frame”:3, “rect1”?(432, 632), (32, 32)), “rect2”?(532, 632), (32, 32)), “rect3”?(632, 632), (32, 32))},
T_E3N:{“frame”:2, “rect1”?(432, 732), (32, 32)), “rect2”?(532, 732), (32, 32))}, #中型坦克
T_E3S:{“frame”:3, “rect1”?(432, 732), (32, 32)), “rect2”?(532, 732), (32, 32)), “rect3”?(632, 732), (32, 32))},
T_E4N:{“frame”:2, “rect1”?(432, 832), (32, 32)), “rect2”?(532, 832), (32, 32))}, #重型坦克白色
T_E4S:{“frame”:3, “rect1”?(432, 832), (32, 32)), “rect2”?(532, 832), (32, 32)), “rect3”?(632, 832), (32, 32))},
T_E4Y:{“frame”:2, “rect1”?(032, 932), (32, 32)), “rect2”?(132, 932), (32, 32))}, #重型坦克黄色
T_E4G:{“frame”:2, “rect1”?(232, 932), (32, 32)), “rect2”?(332, 932), (32, 32))}, #重型坦克绿色
S_100:{“frame”:1, “rect1”?(432, 932), (32, 16))}, #100
S_200:{“frame”:1, “rect1”?(432, 932+16), (32, 16))},#200
S_300:{“frame”:1, “rect1”?(532, 932), (32, 16))}, #300
S_400:{“frame”:1, “rect1”?(532, 932+16), (32, 16))},#400
S_500:{“frame”:1, “rect1”?(632, 932), (32, 16))}, #500
F_E01:{“frame”:1, “rect1”?(232, 132), (32, 32))}, #过关场景中的坦克1
F_E02:{“frame”:1, “rect1”?(332, 132), (32, 32))}, #过关场景中的坦克2
F_E03:{“frame”:1, “rect1”?(432, 132), (32, 32))}, #过关场景中的坦克3
F_E04:{“frame”:1, “rect1”?(532, 132), (32, 32))}, #过关场景中的坦克4
F_FLG:{“frame”:1, “rect1”?(532, 432+16), (16, 16))}, #左箭头
F_FRG:{“frame”:1, “rect1”?(532+16, 432+16), (16, 16))}, #右箭头
F_001:{“frame”:1, “rect1”?(632, 432+16), (14, 14))}, #I
F_002:{“frame”:1, “rect1”?(632+16, 432+16), (14, 14))},#II
F_003:{“frame”:1, “rect1”?(632, 932+16), (14, 14))}, #I
F_004:{“frame”:1, “rect1”?(632+16, 932+16), (14, 14))} #II
}

我方坦克如下:
import pygame,bullet
from pygame.locals import *
from const import *

tank_pic=pygame.image.load(‘images/1.png’)
T_Y01_U=tank_pic.subsurface(pygame.Rect(IMG_CFG[T_Y01][“rect1”]))
T_Y02_U=tank_pic.subsurface(pygame.Rect(IMG_CFG[T_Y01][“rect2”]))
T_Y01_D=pygame.transform.rotate(T_Y01_U,180)
T_Y02_D=pygame.transform.rotate(T_Y02_U,180)
T_Y01_L=pygame.transform.rotate(T_Y01_U,90)
T_Y02_L=pygame.transform.rotate(T_Y02_U,90)
T_Y01_R=pygame.transform.rotate(T_Y01_U,270)
T_Y02_R=pygame.transform.rotate(T_Y02_U,270)

Destory_images=[]
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect1”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect2”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect3”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect4”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect5”])))

bron_images=[]
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect1”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect2”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect3”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect4”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect5”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect6”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect7”])))

invincible_images=[]
invincible_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_CAP][“rect1”])))
invincible_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_CAP][“rect2”])))

class My_Tank(pygame.sprite.Sprite):
def init(self,bg_size,screen):
pygame.sprite.Sprite.init(self)
self.invincible = False
self.bron=True
self.live=False
self.bron_timer=1
self.invincible_timer=350
self.boom=2
self.count=0
self.stop=False
self.swith=False
self.screen=screen
self.speed=3
self.size=bg_size
self.direction=‘U’
self.images_U={}
self.images_D = {}
self.images_L = {}
self.images_R = {}
self.images_U[0]=T_Y01_U
self.images_U[1]=T_Y02_U
self.images_D[0]=T_Y01_D
self.images_D[1]=T_Y02_D
self.images_L[0]=T_Y01_L
self.images_L[1]=T_Y02_L
self.images_R[0]=T_Y01_R
self.images_R[1]=T_Y02_R
self.images={}
self.images[‘U’]=self.images_U
self.images[‘D’] = self.images_D
self.images[‘L’] = self.images_L
self.images[‘R’] = self.images_R
self.image=self.images[self.direction][(self.count%2)]
self.rect=self.image.get_rect()
self.rect.topleft=(288,768)
self.destory_image=Destory_images
self.bron_images=bron_images
self.invincible_images=invincible_images
self.offset = {pygame.K_LEFT: 0, pygame.K_RIGHT: 0, pygame.K_UP: 0, pygame.K_DOWN: 0}
self.bullets = pygame.sprite.Group()
def move(self):
x = self.rect.left + self.offset[pygame.K_RIGHT] - self.offset[pygame.K_LEFT]
y = self.rect.top + self.offset[pygame.K_DOWN] - self.offset[pygame.K_UP]
if y == self.rect.top and not self.stop:
if x < 0:
self.rect.left = 0

        elif x > self.size[0]- self.rect.width-100:
            self.rect.left = self.size[0]- self.rect.width-100
        else:
            self.rect.left = x
    if x == self.rect.left and not self.stop:
        if y < 0:
            self.rect.top = 0
        elif y > self.size[1] - self.rect.height:
            self.rect.top = self.size[1] - self.rect.height
        else:
            self.rect.top = y





def fire(self):
    if self.direction=='U':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.centerx-2,self.rect.top-10))
        self.bullets.add(bullets)

    if self.direction=='D':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.centerx-2.5,self.rect.top+30))
        self.bullets.add(bullets)

    if self.direction=='L':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.left-8,self.rect.centery-2.5))
        self.bullets.add(bullets)

    if self.direction=='R':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.left+30,self.rect.centery-2.5))
        self.bullets.add(bullets)





def rest(self,delay):
    self.screen.blit(self.bron_images[self.count%7],(288,768))
    self.rect.topleft = (288,768)
    self.direction='U'
    if not delay%3:
        self.count+=1
        if self.count==700:
            self.count=1








def Invincible(self,delay):
    self.screen.blit(self.invincible_images[self.boom % 2], self.rect)
    if not delay % 5:
        self.boom += 1
        if self.boom == 3001:
            self.boom = 2








def display(self,delay):
    if self.live:
        if self.swith:
            self.image=self.images[self.direction][self.count%2]
            self.screen.blit(self.image,self.rect)
            if not delay%5:
                self.count+=1
                if self.count==100:
                    self.count=1

        else:self.image=self.images[self.direction][1]
        self.screen.blit(self.image,self.rect)


def hit(self):
    for b in self.bullets:
        if b.live:
            self.screen.blit(b.image,b.rect)
            b.move()
        else:self.bullets.remove(b)
          # enemy_hit=pygame.sprite.spritecollide(b,enemys,False,pygame.sprite.collide_mask)
           #if enemy_hit:
               #b.live=False
               #self.bullets.remove(b)
               #for each in enemy_hit:
                   #each.live=False

           #bullet_hit = pygame.sprite.spritecollide(b, enemy_bullet, False, pygame.sprite.collide_mask)
           #if enemy_hit:
               #b.live = False
               #self.bullets.remove(b)
               #for each in bullet_hit:
                   #each.live = False
                   #enemy_bullet.remove(each)


def explode(self,delay):
    if self.count>5:
        self.count=0
    self.screen.blit(self.destory_image[self.count%5],self.rect)
    if not delay%5:
        self.count+=1
        if self.count==5:
            self.count=0
            self.boom=2
            self.bron_timer=1

地方坦克为:
import pygame,bullet,random
from pygame.locals import *
from const import *

tank_pic=pygame.image.load(‘images/1.png’)
T_E01_U=tank_pic.subsurface(pygame.Rect(IMG_CFG[T_E1N][“rect1”]))
T_E02_U=tank_pic.subsurface(pygame.Rect(IMG_CFG[T_E1N][“rect2”]))
T_E01_D=pygame.transform.rotate(T_E01_U,180)
T_E02_D=pygame.transform.rotate(T_E02_U,180)
T_E01_L=pygame.transform.rotate(T_E01_U,90)
T_E02_L=pygame.transform.rotate(T_E02_U,90)
T_E01_R=pygame.transform.rotate(T_E01_U,270)
T_E02_R=pygame.transform.rotate(T_E02_U,270)

Destory_images=[]
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect1”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect2”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect3”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect4”])))
Destory_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_EXP][“rect5”])))

bron_images=[]
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect1”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect2”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect3”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect4”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect5”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect6”])))
bron_images.append(tank_pic.subsurface(pygame.Rect(IMG_CFG[E_NEW][“rect7”])))

class Enemy_Tank(pygame.sprite.Sprite):
def init(self,bg_size,screen,pos):
pygame.sprite.Sprite.init(self)
self.bron_timer=1
self.bron=True
self.live=False
self.pos=pos
self.boom=2
self.count=0
self.stop=False
self.swith=True
self.screen=screen
self.speed=1
self.size=bg_size
self.direction=‘D’
self._direction=‘U’
self.images_U={}
self.images_D = {}
self.images_L = {}
self.images_R = {}
self.images_U[0]=T_E01_U
self.images_U[1]=T_E02_U
self.images_D[0]=T_E01_D
self.images_D[1]=T_E02_D
self.images_L[0]=T_E01_L
self.images_L[1]=T_E02_L
self.images_R[0]=T_E01_R
self.images_R[1]=T_E02_R
self.images={}
self.images[‘U’]=self.images_U
self.images[‘D’] = self.images_D
self.images[‘L’] = self.images_L
self.images[‘R’] = self.images_R
self.image=self.images[self.direction][(self.count%2)]
self.rect=self.image.get_rect()
self.rect.topleft=pos
self.destory_image=Destory_images
self.bron_images=bron_images

    self.bullets = pygame.sprite.Group()

def move(self):

    if self.direction=='U':
        self._direction=random.choice(['D','L','R'])
        if self.rect.top>0:
            self.rect.top-=self.speed
        else:self.direction=self._direction
    if self.direction=='D':
        self._direction=random.choice(['U','L','R'])
        if self.rect.top<self.size[1]-self.rect.height:
            self.rect.top+=self.speed
        else:self.direction=self._direction

    if self.direction=='L':
        self._direction=random.choice(['D','R'])
        if self.rect.left>0:
            self.rect.left-=self.speed
        else:self.direction=self._direction

    if self.direction=='R':
        self._direction=random.choice(['D','L'])
        if self.rect.left<self.size[0]-self.rect.width:
            self.rect.left+=self.speed
        else:self.direction=self._direction


def fire(self):

    if self.direction=='U':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.centerx-2,self.rect.top-10))
        self.bullets.add(bullets)

    if self.direction=='D':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.centerx-2.5,self.rect.top+30))
        self.bullets.add(bullets)

    if self.direction=='L':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.left-8,self.rect.centery-2.5))
        self.bullets.add(bullets)

    if self.direction=='R':

        bullets=bullet.Bullet(self.size,self.screen,self.direction,(self.rect.left+30,self.rect.centery-2.5))
        self.bullets.add(bullets)



def  random_move ( self , delay ):

    if not delay%(random.randint(50,90)):
        a=random.randint(0,100)
        if a<10:
            self.stop = True
            self.swith  = False
        if 10<a<40:
            self.direction ='D'
            self.stop = False
            self.swith = True

        if 40<a<70:
            self.direction='L'
            self.stop = False
            self.swith = True
        if 70<a<100:
            self.direction = 'R'
            self.stop = False
            self.swith = True

def random_fire(self,delay):
    if self.live:
        a=random.randint(1,3)
        if not delay%(a*30):
            self.fire()








def rest(self,delay):


    self.screen.blit(self.bron_images[self.count%7],self.pos)
    self.rect.topleft=self.pos
    self.direction='D'
    if not delay%3:
        self.count+=1
        if self.count==700:
            self.count=1






def display(self,delay):
    if self.live:
        if self.swith:
            self.image=self.images[self.direction][self.count%2]
            self.screen.blit(self.image,self.rect)
            if not delay%5:
                self.count+=1
                if self.count==100:
                    self.count=1

        else:self.image=self.images[self.direction][1]
        self.screen.blit(self.image,self.rect)


def hit(self):
    for b in self.bullets:
        if b.live:
            self.screen.blit(b.image,b.rect)
            b.move()
            #player_hit=pygame.sprite.spritecollide(b,enemys,False,pygame.sprite.collide_mask)
            #if player_hit:
                #b.live=False
                #self.bullets.remove(b)
                #for each in player_hit:
                    #each.live=False




def explode(self,delay):
    if self.count>5:
        self.count=0
    self.screen.blit(self.destory_image[self.count%5],self.rect)
    if not delay%5:
        self.count+=1
        if self.count==5:
            self.count=0
            self.boom=2
            self.bron_timer=1

子弹为:
import pygame,player
from pygame.locals import *
from const import *

bullet_pic=pygame.image.load(‘images/1.png’)
bullet_U=bullet_pic.subsurface(pygame.Rect(IMG_CFG[E_000][“rect1”]))
bullet_D=pygame.transform.rotate(bullet_U,180)

bullet_L=pygame.transform.rotate(bullet_U,90)

bullet_R=pygame.transform.rotate(bullet_U,270)

class Bullet(pygame.sprite.Sprite):
def init(self,size,screen,direction,pos):
pygame.sprite.Sprite.init(self)
self.direction=direction
self.screen=screen
self.size=size
self.images={}
self.images[‘U’]=bullet_U
self.images[‘D’]=bullet_D
self.images[‘L’]=bullet_L
self.images[‘R’]=bullet_R
self.image=self.images[self.direction]
self.rect=self.image.get_rect()
self.rect.topleft=pos
self.live=True
self.speed=10

def move(self):
    if self.direction=='U':

        if self.rect.top>-self.rect.height:
            self.rect.top-=self.speed
        else:
            self.live=False
            self.kill()

    if self.direction=='D':
        if self.rect.top<self.size[1]+self.rect.height:
            self.rect.top+=self.speed
        else:
            self.live=False
            self.kill()

    if self.direction=='L':
        if self.rect.left>-self.rect.width:
            self.rect.left-=self.speed
        else:
            self.live=False
            self.kill()


    if self.direction=='R':
        if self.rect.left<self.size[0]-100+self.rect.width:
            self.rect.left+=self.speed
        else:
            self.live=False
            self.kill()

地图为:
import random
import pygame
from const import *

map_pic=pygame.image.load(‘images/1.png’)
block_image_A=map_pic.subsurface(pygame.Rect(IMG_CFG[M_BA][“rect1”]))
block_image_U=map_pic.subsurface(pygame.Rect(IMG_CFG[M_BU][“rect1”]))
block_image_D=map_pic.subsurface(pygame.Rect(IMG_CFG[M_BD][“rect1”]))
block_image_L=map_pic.subsurface(pygame.Rect(IMG_CFG[M_BL][“rect1”]))
block_image_R=map_pic.subsurface(pygame.Rect(IMG_CFG[M_BR][“rect1”]))
block_image_UL=map_pic.subsurface(pygame.Rect(IMG_CFG[M_B1][“rect1”]))
block_image_UR=map_pic.subsurface(pygame.Rect(IMG_CFG[M_B2][“rect1”]))
block_image_DL=map_pic.subsurface(pygame.Rect(IMG_CFG[M_B3][“rect1”]))
block_image_DR=map_pic.subsurface(pygame.Rect(IMG_CFG[M_B4][“rect1”]))
grass_image=map_pic.subsurface(pygame.Rect(IMG_CFG[M_C0][“rect1”]))
ice_image=map_pic.subsurface(pygame.Rect(IMG_CFG[M_I0][“rect1”]))
steel_image=map_pic.subsurface(pygame.Rect(IMG_CFG[M_S0][“rect1”]))
river_image11=map_pic.subsurface(pygame.Rect(IMG_CFG[M_R1][“rect1”]))
river_image12=map_pic.subsurface(pygame.Rect(IMG_CFG[M_R1][“rect2”]))
river_image21=map_pic.subsurface(pygame.Rect(IMG_CFG[M_R2][“rect1”]))
river_image22=map_pic.subsurface(pygame.Rect(IMG_CFG[M_R2][“rect2”]))
home_image1=map_pic.subsurface(pygame.Rect(IMG_CFG[M_H0][“rect1”]))
home_image2=map_pic.subsurface(pygame.Rect(IMG_CFG[M_H0][“rect2”]))

class Basics(pygame.sprite.Sprite):
def init(self,screen):
pygame.sprite.Sprite.init(self)
self.screen=screen
self.live=True

class Brick(Basics):
def init(self,screen,pos):
super(Brick,self).init(screen)
self.destroy=True
self.bullet_cross = False
self.cross=False
self.images={}
self.type=‘A’
self.images[‘A’] = block_image_A
self.images[‘U’] = block_image_U
self.images[‘D’] = block_image_D
self.images[‘R’] = block_image_R
self.images[‘L’] = block_image_L
self.images[‘UL’] = block_image_UL
self.images[‘UR’] = block_image_UR
self.images[‘DL’] = block_image_DL
self.images[‘DR’] = block_image_DR
self.image=self.images[self.type]
self.rect=self.image.get_rect()
self.rect.topleft=pos

def display(self,delay):
    if self.live:
        self.image=self.images[self.type]
        self.screen.blit(self.image,self.rect)

class Grass(Basics):
def init(self,screen,pos):
super(Grass,self).init(screen)
self.destroy=False
self.bullet_cross = True
self.cross=True
self.image=grass_image
self.rect=self.image.get_rect()
self.rect.topleft=pos

def display(self,delay):
    if self.live:
        self.screen.blit(self.image,self.rect)

class Ice(Basics):
def init(self,screen,pos):
super(Ice,self).init(screen)
self.destroy=False
self.bullet_cross = True
self.cross=True
self.image=ice_image
self.rect=self.image.get_rect()
self.rect.topleft=pos

def display(self,delay):
    if self.live:
        self.screen.blit(self.image,self.rect)

class Steel(Basics):
def init(self,screen,pos):
super(Steel,self).init(screen)
self.destroy=False
self.bullet_cross = False
self.cross=False
self.image=steel_image
self.rect=self.image.get_rect()
self.rect.topleft=pos

def display(self,delay):
    if self.live:
        self.screen.blit(self.image,self.rect)

class Home(Basics):
def init(self,screen,pos):
super(Home,self).init(screen)
self.destroy=True
self.type=‘A’
self.cross=False
self.bullet_cross = False
self.images=[]
self.images.extend([home_image1,home_image2])
self.image=self.images[0]
self.rect=self.image.get_rect()
self.rect.topleft=pos

def display(self,delay):
    if self.type=='A':
        self.live=True
        self.image=self.images[0]
        self.screen.blit(self.image,self.rect)
    elif self.type!='A':
        self.image=self.images[1]
        self.screen.blit(self.image,self.rect)

class River(Basics):
def init(self,screen,type,pos):
super(River,self).init(screen)
self.destroy=False
self.cross=False
self.bullet_cross=True

    self.type=type
    self.count=1
    self.image1=[]
    self.image2=[]
    self.image1.extend([river_image11,river_image12])
    self.image2.extend([river_image21,river_image22])
    self.images={}
    self.images[1]=self.image1
    self.images[2]=self.image2

    self.image=self.images[self.type][1]
    self.rect=self.image.get_rect()
    self.rect.topleft=pos


def display(self,delay):
    if self.live:
        self.image=self.image=self.images[self.type][self.count%2]
        self.screen.blit(self.image,self.rect)
        if not delay%15:
            self.count+=1
            if self.count==100:
                self.count=1

class Tank_Map(Basics):
def init(self,screen):
super(Tank_Map,self).init(screen)
self.mapData_0=[]
self.type=1
self.c=[]
self.map_group=pygame.sprite.Group()

def mad_map(self):

    with open('levels/level01.txt')as b:
        for i in b:
            x=i.split(',')
            self.c.append(x)


        for d in self.c:
            f=[]
            for e in d:
                f.append(e)
            self.mapData_0.append(f)


    if len(self.map_group) == 0:
        for y in range(len(self.mapData_0)):
            for x in range(len(self.mapData_0[y])):
                self.type=self.mapData_0[y][x]
                pos1=x*32,y*32
                pos2=x*32,y*32+16
                pos3 = x * 32+16, y * 32
                pos4 = x * 32+16, y * 32+16

                if self.type=='B':
                    self.map_group.add(Brick(self.screen,pos1))
                    self.map_group.add(Brick(self.screen, pos2))
                    self.map_group.add(Brick(self.screen, pos3))
                    self.map_group.add(Brick(self.screen, pos4))

                elif self.type=='G':
                    self.map_group.add(Grass(self.screen, pos1))
                    self.map_group.add(Grass(self.screen, pos2))
                    self.map_group.add(Grass(self.screen, pos3))
                    self.map_group.add(Grass(self.screen, pos4))


                elif self.type=='I':
                    self.map_group.add(Ice(self.screen, pos1))
                    self.map_group.add(Ice(self.screen, pos2))
                    self.map_group.add(Ice(self.screen, pos3))
                    self.map_group.add(Ice(self.screen, pos4))

                elif self.type == 'S':
                    self.map_group.add(Steel(self.screen, pos1))
                    self.map_group.add(Steel(self.screen, pos2))
                    self.map_group.add(Steel(self.screen, pos3))
                    self.map_group.add(Steel(self.screen, pos4))


                elif self.type == 'R1':
                    self.map_group.add(River(self.screen,1, pos1))

                    self.map_group.add(River(self.screen,1, pos3))


                elif self.type == 'R2':
                    self.map_group.add(River(self.screen,2, pos1))

                    self.map_group.add(River(self.screen,2, pos3))



                elif self.type == 'H':
                    self.map_group.add(Home(self.screen, pos1))

def display(self,delay):
    for each in self.map_group:

        each.display(delay)

游戏主要程序为:
import pygame,sys,random,time
import player,bullet,enemy,block
from pygame.locals import *

bulecolour=pygame.Color(0,0,255)
redcolour=pygame.Color(255,0,0)
blackcolour=pygame.Color(0,0,0)
whitecolour=pygame.Color(255,255,255)

delay=0

pygame.init()
screen_width=800
screen_height=800
bj=screen_width,screen_height
screen=pygame.display.set_mode(bj)

clock=pygame.time.Clock()
my_tank=player.My_Tank(bj,screen)
enemy_tank=enemy.Enemy_Tank(bj,screen,(0,0))
enemys=pygame.sprite.Group()
enemys_bullet=pygame.sprite.Group()
players=pygame.sprite.Group()
players_bullet=pygame.sprite.Group()
players.add(my_tank)
timer_event=USEREVENT
game_map=block.Tank_Map(screen)
game_map.mad_map()
enemys.add(enemy_tank)
Runing=True
while Runing:
clock.tick(60)
pygame.time.set_timer(timer_event, 10)

screen.fill(blackcolour)
game_map.display(delay)
if not my_tank.live:
    if my_tank.boom==1:
        my_tank.explode(delay)
    if my_tank.bron_timer<200:
        my_tank.rest(delay)
else:
    my_tank.display(delay)
    if not my_tank.stop:
        my_tank.move()
    my_tank.hit()
    if my_tank.invincible_timer<300:
        my_tank.Invincible(delay)


if 220>my_tank.bron_timer>200:
    my_tank.invincible_timer=1
    my_tank.boom = 1
    my_tank.live = True
    my_tank.bron = True
    my_tank.invincible = True

if 330>my_tank.invincible_timer>300:
    my_tank.invincible = False
    my_tank.boom = 1



if not enemy_tank.live:
    if enemy_tank.boom==1:
        enemy_tank.explode(delay)
    if enemy_tank.bron_timer<200:
        enemy_tank.rest(delay)

if enemy_tank.live:
    enemy_tank.display(delay)
    enemy_tank.random_move(delay)
    enemy_tank.random_fire(delay)
    enemy_tank.hit()

    if  not enemy_tank.stop:
        enemy_tank.move()

if 220>enemy_tank.bron_timer>200:
    enemy_tank.boom = 1
    enemy_tank.live = True
    enemy_tank.bron = True



for e in enemys:
    for b in e.bullets:
        enemys_bullet.add(b)


for e in players:
    for b in e.bullets:
        players_bullet.add(b)




for b in players_bullet:
    if b.live:
        enemy_hit = pygame.sprite.spritecollide(b, enemys, False, pygame.sprite.collide_mask)
        if  enemy_hit:
            b.live=False
            players_bullet.remove(b)

            for each in enemy_hit:
                each.live=False
        bullet_hit = pygame.sprite.spritecollide(b, enemys_bullet, False, pygame.sprite.collide_mask)
        if bullet_hit:
            b.live=False
            players_bullet.remove(b)

            for each in bullet_hit:
                each.live=False
                enemy_tank.bullets.remove(each)


        map_hit=pygame.sprite.spritecollide(b, game_map.map_group, False, pygame.sprite.collide_mask)
        if map_hit:
            for M in map_hit:
                if not M .bullet_cross:
                    b.live=False
                    players_bullet.remove(b)
                    if M .destroy:
                        if M .type=='A':
                            M.type=b.direction

                        elif M .type=='U':
                            if b.direction=='U' or b.direction=='D':
                                M.live=False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'L':
                                M.type='UL'

                            elif b.direction == 'R':
                                M.type='UR'

                        elif M .type == 'D':
                            if b.direction == 'U' or b.direction == 'D':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'L':
                                M.type = 'DL'

                            elif b.direction == 'R':
                                M.type = 'DR'


                        elif M .type == 'L':
                            if b.direction == 'L' or b.direction == 'R':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'U':
                                M.type = 'UL'

                            elif b.direction == 'D':
                                M.type = 'DL'

                        elif M .type == 'R':
                            if b.direction == 'L' or b.direction == 'R':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'U':
                                M.type = 'UR'

                            elif b.direction == 'D':
                                M.type = 'DR'


                        elif M.type=='UR'or  M.type=='UL' or M.type=='DR' or M.type=='DR':
                            M.live=False
                            game_map.map_group.remove(M)
                            M.kill()








for b in enemys_bullet:
    if b.live:
        player_hit = pygame.sprite.spritecollide(b, players, False, pygame.sprite.collide_mask)
        if  player_hit:
            b.live=False
            enemys_bullet.remove(b)

            for each in player_hit:
                if not each.invincible:
                    each.live=False


        map_enemy_hit=pygame.sprite.spritecollide(b, game_map.map_group, False, pygame.sprite.collide_mask)
        if map_enemy_hit:
            for M in map_enemy_hit:
                if not M .bullet_cross:
                    b.live=False
                    enemys_bullet.remove(b)
                    if M .destroy:
                        if M .type=='A':
                            M.type=b.direction

                        elif M .type=='U':
                            if b.direction=='U' or b.direction=='D':
                                M.live=False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'L':
                                M.type='UL'

                            elif b.direction == 'R':
                                M.type='UR'

                        elif M .type == 'D':
                            if b.direction == 'U' or b.direction == 'D':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'L':
                                M.type = 'DL'

                            elif b.direction == 'R':
                                M.type = 'DR'


                        elif M .type == 'L':
                            if b.direction == 'L' or b.direction == 'R':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'U':
                                M.type = 'UL'

                            elif b.direction == 'D':
                                M.type = 'DL'

                        elif M .type == 'R':
                            if b.direction == 'L' or b.direction == 'R':
                                M.live = False
                                game_map.map_group.remove(M)
                                M.kill()

                            elif b.direction == 'U':
                                M.type = 'UR'

                            elif b.direction == 'D':
                                M.type = 'DR'


                        elif M.type=='UR'or  M.type=='UL' or M.type=='DR' or M.type=='DR':
                            M.live=False
                            game_map.map_group.remove(M)
                            M.kill()



for e in enemys:
    for m in game_map.map_group:
        if not m.cross:

            e_m=pygame.sprite.collide_rect(m,e)
            if e_m:

                if e.direction=='U':
                    e.rect.move_ip(0,1)
                    e.direction =  e._direction


                if e.direction=='D':
                    e.rect.move_ip(0,-1)
                    e.direction =  e._direction

                if e.direction=='L':
                    e.rect.move_ip(1,0)
                    e.direction =  e._direction

                if e.direction=='R':
                    e.rect.move_ip(-1,0)
                    e.direction =  e._direction

for p in players:
    for m in game_map.map_group:
        if not m.cross:

            p_m = pygame.sprite.collide_rect(p, m)
            if p_m:

                p.stop=True
                print(pygame.sprite.collide_rect(p, m))
                print(p.stop)

                if p.direction == 'U':
                    p.rect.move_ip(0, 1)


                elif p.direction == 'D':
                    p.rect.move_ip(0, -1)


                elif p.direction == 'L':
                    p.rect.move_ip(1, 0)


                elif p.direction == 'R':
                    p.rect.move_ip(-1, 0)
            else: p.stop=False

print(pygame.sprite.collide_rect(p, m))
for event in pygame.event.get():
    if event.type==QUIT:
        Runing=False
        pygame.quit()
        sys.exit()
    elif event.type==pygame.KEYDOWN:
        if event.key in my_tank.offset and not my_tank.stop:
            my_tank.swith=True
            my_tank.offset[event.key]=my_tank.speed
        if event.key == pygame.K_LEFT:
            my_tank.direction='L'
        if event.key == pygame.K_RIGHT:
            my_tank.direction='R'
        if event.key == pygame.K_UP:
            my_tank.direction='U'
        if event.key == pygame.K_DOWN:
            my_tank.direction='D'
        if event.key==pygame.K_SPACE:
            my_tank.fire()

    elif event.type==pygame.KEYUP:
        if event.key in my_tank.offset:
            my_tank.swith = False
            my_tank.offset[event.key]=0

    elif event.type == timer_event:

        my_tank.bron_timer+=1
        my_tank.invincible_timer+=1
        enemy_tank.bron_timer+=1
        if my_tank.bron_timer==1000:
            my_tank.bron_timer=500

        if my_tank.invincible_timer==1000:
            my_tank.invincible_timer=500

        if enemy_tank.bron_timer==1000:
            enemy_tank.bron_timer=500





pygame.display.flip()

delay+=1
if delay>1000:
    delay=1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值