中途岛海战大敌机类设计程序

"""
   中途岛海战的敌机,送子弹与生命值的,
   这架敌机经常会在云下面,这时候,无法射击它。
   当它在云上面时,才能够被击中。
"""

__author__= '李兴球'
__date__ = '2019/9/12'

import time
import pygame
from pygame.locals import *
from random import *

class Enemy_rover(pygame.sprite.Sprite):
    """
       随机移动的敌机,有三种状态,当往上升的时候,大小越来越大。
       当在云上的时候这时可以被击中,被击时会显示另一个造型,
       当往下移的时候,越来越小。
    """
    def __init__(self,images,hurt_image,explosion_images,screen):
        """
          images:从小到大的造型列表,
          hurt_image:被击中时的造型,
          explosion_images:爆炸时的造型列表,
          screen:所在屏幕
        """
        pygame.sprite.Sprite.__init__(self)
        # 爆炸造型列表
        self.explosion_images = explosion_images
        self.explosion_rects = [im.get_rect() for im in explosion_images]
        self.explosion_amounts = len(explosion_images)
        # 被攻击时的造型 
        self.hurt_image = hurt_image
        self.hurt_rect = hurt_image.get_rect()
        self.sw = screen.get_width()
        self.sh = screen.get_height()
        # 正常造型表
        self.images = images             # 从小到大的造型图列表
        self.shape_amounts = len(images) # 造型数量
        self.shape_index = 0             # 造型索引号
        self.rects = [im.get_rect() for im in images]
        
        
        # 下面是确定一个随机的起始坐标       
        x = randint(self.sw//2 - 200,self.sw//2 + 200)        
        y = randint(self.sh//2 - 200,self.sh//2 + 200)
        self._position = (x,y)
        self.set_costume()
       
        self.dx = randint(-2,2)
        self.dy = randint(-2,2)
        self.status = 'idle'             # 初始在最下面为漫游状态
        self.attack_time = randint(5,10) # 攻击时间
        self.attack_begin_time = time.time()
        self.idle_time = randint(10,20)  # 在云下面的漫游时间
        self.idle_begin_time = time.time()
        self.attacking = False          # 是否被玩家子弹所攻击
        self.lives = 10                # 生命值为100
        self.update_interval = 0.1      # 更新的间隔时间,要不然飞得太快
        self.update_begin_time = time.time()
        
    def set_costume(self):
        """设置造型"""
        
        self.image = self.images[self.shape_index]
        self.rect = self.rects[self.shape_index]
        self.rect.center = self._position

    def bounce_on_edge(self):
        """碰到边缘就反弹"""
        if self.rect.left <= 0 or self.rect.right>= self.sw:
            self.dx =  -self.dx
        if self.rect.top <= 0 or self.rect.bottom >= self.sh:
            self.dy = -self.dy
    

You must log in to read the rest of this article. Please log in or register as a user.