pygame城市之战横版射击游戏

"""pygame城市之战横版射击游戏,按上下左右方向箭头操作飞机。这是一个横板射击小游戏,在黑夜的城市上空,你将要操作一架飞机去射击敌机,爆炸效果还不错。
在游戏中定义了滚动的背景类,定义了飞机类Plane,定义了子弹类,敌机类,爆炸类等,是学习Pygame和面向对象编程的好例子。"""

import math
import time
import pygame
from pygame.locals import *
from random import choice,randint
from scrolledbackground import *   # 导入滚动背景类
from plane import *                # 导入Plane类 
from bullet import *               # 导入Bullet类
from explosion import *            # 导入爆炸类 
             
def split_images(image,rows,cols):
    """image是一张图片,把它切分为若干图,返回列表"""
    global explosion_images_list
    image = pygame.image.load(image)
    step_width = image.get_width()//cols
    step_height = image.get_height()//rows
    
    for r in range(rows):
        for c in range(cols):
            x = c * step_width
            y = r * step_height
            rect = pygame.Rect(x,y,step_width,step_height)
            explosion_images_list.append(image.subsurface(rect))            
 
                
class Enemy(pygame.sprite.Sprite):
    def __init__(self,images,group,screen):
        """images是surface列表"""
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.screen_width = self.screen.get_width()     # 获取屏幕宽度
        self.screen_height = self.screen.get_height()   # 获取屏幕高度
        self.image = choice(images)                     # 随机选择一个surface
        self.rect = self.image.get_rect()
        self.rect.left = self.screen_width + randint(10,self.screen_width)
        self.rect.centery = randint(0,self.screen_height)
        self.group = group
        self.group.add(self)                            # 加入到自己的组
    def update(self):
        self.rect.move_ip(-5,0)
        if self.rect.right <= 0 :
            self.group.remove(self)
        
class Eullet(pygame.sprite.Sprite):
    """敌方子弹类"""
    def __init__(self,image,selfgroup,enemy_group,plane,screen):
        """参数列表:image,已转换成surface的对象
                     selfgroup,所在的组
                     enemy_group,敌人组.
                     plane,我方飞机
                     screen,屏幕对象
        """
        pygame.sprite.Sprite.__init__(self)
        self.group = selfgroup                    # 自己所在的组
        self.group.add(self)
        self.image = image
        e = choice(list(enemy_group))             # 随机选择一架敌机
        self.rect = self.image.get_rect()         # 矩形对象
        self.rect.center = e.rect.center          # 放在这架敌机的坐标
        self.plane = plane                        # 我方飞机对象
        self.screen = screen
        self.screen_width = screen.get_width()
        self.screen_height = screen.get_height()        
        self.dx = self.rect.centerx - self.plane.rect.centerx
        self.dy = self.rect.centery - self.plane.rect.centery
        
    def update(self):
        self.rect.move_ip(-self.dx//30,-self.dy//30)
        if self.beyond_edge() :
            self.group.remove(self)
    def beyond_edge(self):
        b1 = self.rect.left > self.screen_width
        b2 = self.rect.right < 0
        b3 = self.rect.bottom < 0
        b4 = self.rect.top > self.screen_height
        return b1 or b2 or b3 or b4

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