与之前相比实现了坦克的射击功能

写了一个子弹类
1.己方坦克按j射击,可以连发子弹
2.敌方坦克有ai,随机发射子弹
`
import pygame
import sys
import time
import random

BACK = (250,250,250)
SCREEN_X = 1400
SCREEN_Y = 750

class Tank:
def init(self,screen,team=0):
self.screen = screen
self.all_bullet = []
if team == 0:
self.imgs = {
'w': pygame.image.load('E:/图片/heroW.png'),
's': pygame.image.load('E:/图片/heroS.png'),
'a': pygame.image.load('E:/图片/heroA.png'),
'd': pygame.image.load('E:/图片/heroD.png')
}
else:
self.imgs = {
'w': pygame.image.load('E:/图片/eW.png'),
's': pygame.image.load('E:/图片/eS.png'),
'a': pygame.image.load('E:/图片/eA.png'),
'd': pygame.image.load('E:/图片/eD.png')
}
self.team = team
self.direction = 'w'
self.img = self.imgs[self.direction]
# tank初始化位置
self.rect = self.img.get_rect()
if team == 0:
self.speed = 5
self.rect.topleft = [300, 600]
else:
self.ran = [0, 4]
self.speed = random.randint(1, 5)
self.rect.topleft = [random.random()SCREEN_X, random.random()SCREEN_Y]

def control(self):
    # 按键控制
    key_pressed = pygame.key.get_pressed()
    if key_pressed[pygame.K_w]:
        self.direction = 'w'
        self.rect.top -= self.speed
    if key_pressed[pygame.K_s]:
        self.direction = 's'
        self.rect.top += self.speed
    if key_pressed[pygame.K_a]:
        self.direction = 'a'
        self.rect.left -= self.speed
    if key_pressed[pygame.K_d]:
        self.direction = 'd'
        self.rect.left += self.speed
    if key_pressed[pygame.K_j]:
        b = Bullet(self.screen,self.direction,self.rect)
        self.all_bullet.append(b)

def ai(self):
    self.ran[0] += 1
    if self.ran[0] % 30 == 0:
        bb = Bullet(self.screen, self.direction, self.rect)
        self.all_bullet.append(bb)
    if self.ran[1] % 4 == 0:
        self.direction = 'w'
        self.rect.top -= self.speed
        if self.ran[0] % 20 == 0:
            self.ran[1] = random.randint(4,7)
    elif self.ran[1] % 4 == 1:
        self.direction = 's'
        self.rect.top += self.speed
        if self.ran[0] % 20 == 0:
            self.ran[1] = random.randint(4,7)
    elif self.ran[1] % 4 == 2:
        self.direction = 'a'
        self.rect.left -= self.speed
        if self.ran[0] % 20 == 0:
            self.ran[1] = random.randint(4,7)
    elif self.ran[1] % 4 == 3:
        self.direction = 'd'
        self.rect.left += self.speed
        if self.ran[0] % 20 == 0:
            self.ran[1] = random.randint(4,7)

def display(self):
    self.img = self.imgs[self.direction]
    self.screen.blit(self.img, self.rect)
    for i in self.all_bullet:
        i.move()
        i.display()

class Bullet:
def init(self,screen,dir,rect):
self.img = pygame.image.load('E:/图片/bullet.png')
self.direction = dir
self.rect = self.img.get_rect()
if self.direction == 'w':
self.rect.left = rect.left + rect.width/2 - self.rect.width/2
self.rect.top = rect.top - self.rect.top
elif self.direction == 's':
self.rect.left = rect.left + rect.width / 2 - self.rect.width / 2
self.rect.top = rect.top + rect.height
elif self.direction == 'a':
self.rect.left = rect.left - rect.width / 2 - self.rect.width / 2
self.rect.top = rect.top + rect.width/2 - self.rect.width/2
elif self.direction == 'd':
self.rect.left = rect.left + rect.width
self.rect.top = rect.top + rect.width/2 - self.rect.width/2
self.speed = 7
self.screen = screen

def move(self):
    if self.direction == 'w':
        self.rect.top -= self.speed
    elif self.direction == 's':
        self.rect.top += self.speed
    elif self.direction == 'a':
        self.rect.left -= self.speed
    elif self.direction == 'd':
        self.rect.left += self.speed

def display(self):
    self.screen.blit(self.img, self.rect)

class Manager:
def init(self):
self.screen = pygame.display.set_mode((SCREEN_X,SCREEN_Y))

def main(self):
    pygame.init()
    t1 = Tank(self.screen)
    enemy = [Tank(self.screen, 1) for _ in range(17)]
    while True:
        self.screen.fill(BACK)
        t1.control()
        t1.display()
        for i in range(16):
            enemy[i].ai()
            enemy[i].display()
        time.sleep(0.1)#防止坦克跑得太快
        pygame.display.update()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

m = Manager()
m.main()

`