"Airplane Wars" by pygame (2)

Foreword:

Following "Airplane Wars" (1), it is expanded and optimized this time.

Extended content:
  1. Determining and removing out-of-bounds bullets
  2. The hero's button keeps moving
  3. enemy plane fires bullets

Programming code:

1 - Hero bullet cross-border judgment and delete cross-border bullet

class HeroPlane(object):
    def __init__(self, screen_temp):
        self.x = 195
        self.y = 700
        self.screen = screen_temp
        self.image = pygame.image.load("./feiji/hero1.png")
        self.bullet_list = [] #存储发射出去的子弹的引用

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        bullet_list_temp = []#保存越界的子弹
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge():#判断子弹是否越界
                bullet_list_temp.append(bullet)

        for bullet in bullet_list_temp:
            self.bullet_list.remove(bullet)

    def move_left(self):
        self.x -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))
Analysis code:

**Why use the bullet_list_temp list to store out-of-bounds bullets, and then delete the same bullet reference in self.bullet_list?
Because: directly in self.bullet_list.remove(bullet) will cause its next bullet (moving to the position of the previous bullet, but the loop has already pointed to the next position, that is, missing deletion. Since main() is a loop call, that is, loop deletion , this code can also be implemented without using bullet_list_temp. In order to make the code strict, the intermediate variable is used here.) It cannot be deleted.

2 - The Bullet class adds a method for judging out-of-bounds

    def judge(self):
        if self.y < 0:
            return True
        else:
            return False

3 - The enemy plane fires bullets

class EnemyPlane(object):
    """敌机的类"""
    def __init__(self, screen_temp):
        self.x = 0
        self.y = 0
        self.screen = screen_temp
        self.image = pygame.image.load("./feiji/enemy0.png")
        self.bullet_list = [] #存储发射出去的子弹的引用
        self.direction = "right" #用来存储飞机默认显示方向

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        bullet_list_out = [] 
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge():
                bullet_list_out.append(bullet)
        for bullet in bullet_list_out:
            self.bullet_list.remove(bullet)

    def move(self):

        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 480-50:
            self.direction ="left"
        elif self.x < 0:
            self.direction = "right"


    def fire(self):
        random_num = random.randint(1, 100)#随机的发射子弹
        if random_num == 8 or random_num == 20:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))

4 - Enemy Bullets

class EnemyBullet(object):
    def __init__(self, screen_temp, x, y):
        self.x = x + 25
        self.y = y + 40
        self.screen = screen_temp
        self.image = pygame.image.load("./feiji/bullet1.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))


    def move(self):
        self.y += 5

    def judge(self):
        if self.y > 852:
            return True
        else:
            return False

5 - Populate the main() function

def main():
    #1. 创建窗口
    screen = pygame.display.set_mode((480,852),0,32)

    #2. 创建一个背景图片
    background = pygame.image.load("./feiji/background.png")

    #3. 创建一个飞机对象
    hero = HeroPlane(screen)

    #4. 创建一个敌机
    enemy = EnemyPlane(screen)


    while True:
        screen.blit(background, (0,0))

        hero.display()

        enemy.move() #控制敌机的移动

        enemy.fire() #敌机开火

        enemy.display()

        pygame.display.update()

        key_control(hero)

        time.sleep(0.01)

if __name__ == "__main__":
    main() 
Run the screenshot:

write picture description here

End of this chapter:

The next chapter will perform base class extraction.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644615&siteId=291194637