コード例 #1
0
ファイル: war.py プロジェクト: wwb001/planeGame
class PlaneWar(object):
    """飞机大战"""
    # 游戏状态

    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY  # 准备中,1游戏中,2游戏结束

    our_plane = None

    frame = 0  # 播放帧数

    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        # 初始化游戏
        pygame.init()
        self.width, self.height = 480, 852

        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置窗口标题
        pygame.display.set_caption('飞机大战')

        # 加载背景图片
        self.bg = pygame.image.load(BG_IMG)
        self.bg_over = pygame.image.load(BG_IMG_OVER)
        # 游戏的标题
        self.img_game_title = pygame.image.load(IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 宽度和高度
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height))

        # 开始按钮
        self.btn_start = pygame.image.load(IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - btn_width) / 2), int(self.height / 2 + btn_height))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('华文隶书', 32)

        # 加载背景音乐
        # pygame.mixer.music.load(BG_MUSIC)
        # pygame.mixer.music.play(-1)  # 无限循环播放
        # pygame.mixer.music.set_volume(0.2)  # 设置音量

        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=4)

        self.clock = pygame.time.Clock()

        # 上次按的键盘上的某一个键,用于控制飞机
        self.key_down = None

    def bind_event(self):
        """绑定事件"""
        # 1.监听事件
        for events in pygame.event.get():
            # 退出游戏
            if events.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif events.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏正在准备中,点击才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif events.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = events.key
                # 游戏正在游戏中,才需要控制键盘 ASWD
                if self.status == self.PLAYING:
                    if events.key == pygame.K_w or events.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif events.key == pygame.K_s or events.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif events.key == pygame.K_a or events.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif events.key == pygame.K_d or events.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif events.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        飞机的产生数量 num
        :param num:
        :return:
        """
        # 随机添加6架小型敌机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 3)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环部分"""
        while True:
            # 设置帧速率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 1.绑定事件
            self.bind_event()

            # 更新游戏的状态
            if self.status == self.READY:
                # 游戏正在准备中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False, TEXT_SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())

            elif self.status == self.OVER:
                # 游戏结束
                # 游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 1.本次总分
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False, TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int(self.height / 2))

                self.screen.blit(score_text, score_text_rect)
                # 2.历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    TEXT_SCORE_COLOR)
                self.screen.blit(score_his, (150, 40))

            pygame.display.flip()
コード例 #2
0
ファイル: war.py プロジェクト: laojiuh/PlaneWar
class PlaneWar(object):
    """飞机大战"""

    READY = 0  # 游戏准备中
    PLAYING = 1  # 游戏中
    OVER = 2  # 游戏结束
    status = READY

    our_plane = None

    frame = 0  # 播放帧数

    # 为了方便之后的碰撞检测
    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()

    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        # 初始化游戏
        pygame.init()
        self.width, self.height = 480, 852

        # 加载游戏屏幕
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置标题
        pygame.display.set_caption('PlaneWar')
        # 背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        # 结束背景
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # 开始标题的位置设置
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height))
        # 开始按钮的位置设置
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        b_width, b_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - b_width) / 2), int(self.height / 2 + b_height))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('华文隶书', 32)

        # background music
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.play(-1)  # 无限循环播放
        pygame.mixer.music.set_volume(0.2)  # 设置音量

        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=8)

        # 设置帧数
        self.clock = pygame.time.Clock()

        self.key_down = None

    def bind_event(self):
        """绑定事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 游戏正在等待中,点击鼠标进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:

                self.key_down = event.key
                # 先判断在游戏中,在判断上下左右控制方向
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
        # 让飞机一直发射炮
        if self.status == self.PLAYING and self.frame % 10 == 0:
            self.our_plane.shoot()

    def add_small_enemies(self, num):
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 4)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏的主循环"""
        while True:
            # 1. 设置帧数
            self.clock.tick(60)
            # 计数
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0

            # 2. 绑定事件
            self.bind_event()

            # 3. 更新游戏状态  硬编码
            if self.status == self.READY:
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制开始背景
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                # 重置按键
                self.key_down = None
                self.rest.score = 0
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制我方飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分:{}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                # 绘制分数
                self.screen.blit(score_text, score_text.get_rect())

            elif self.status == self.OVER:
                # 游戏结束背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 1.本次总分
                score_text = self.score_font.render(
                    '{}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                score_text_rect.topleft = (int(
                    (self.width - text_w) / 2), int(self.height / 2))

                # 绘制分数
                self.screen.blit(score_text, score_text_rect)
                # 2.历史最高分
                score_his = self.score_font.render(
                    '{}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SCORE_COLOR)

                self.screen.blit(score_his, (150, 40))

            pygame.display.flip()
コード例 #3
0
class PlaneWar(object):
    """ Aircraft War Game """
    # situation of game
    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY

    our_plane = None

    frame = 0

    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()

    # result of the game
    rest = PlayRest()

    def __init__(self):
        pygame.init()
        self.width, self.height = 480, 852
        self.screen = pygame.display.set_mode((self.width, self.height))
        #name of window
        pygame.display.set_caption('Aircraft Game')

        # load background images
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # title
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()

        # start button
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int((self.width - btn_width) / 2),
                                  int(self.height / 2 + btn_height))

        # font
        self.score_font = pygame.font.SysFont('test_sans', 32)

        # load music
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.play(-1)         # infinite loop for playing music
        pygame.mixer.music.set_volume(0.2)  # set the volume

        # our plane
        self.our_plane = OurPlane(self.screen, speed = 40)

        self.clock = pygame.time.Clock()

        # use keyboard to control aircraft
        self.key_down = None

    def bind_event(self):
        # 1. listening event
        for event in pygame.event.get():
            # quit
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # click the mouse to get in the game
                if self.status == self.READY:
                    self.status = self.PLAYING
                    self.rest.score = 0
                elif self.status == self.PLAYING:
                    # click the mouse to fire the bullet
                    self.our_plane.shoot()
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(20)
            elif event.type == pygame.KEYDOWN:
                # keyborad
                self.key_down = event.key
                # only in the game, need to control the keyboard ASWD
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        # shoot
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        add small enemy aircraft at random
        :param num: number of aircraft produced
        :return:
        """
        # add 6 small enemy aircraft at random
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 10)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """ main part of game """
        while True:
            # 1. set the frame rate
            self.clock.tick(10)
            self.frame += 1
            if self.frame >= 10:
                self.frame = 0
            # 2. bind event
            self.bind_event()

            # 3.update the situation of game
            if self.status == self.READY:
                # get ready
                # setting background
                self.screen.blit(self.bg, self.bg.get_rect())
                # title
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # start button
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # playing
                # background
                self.screen.blit(self.bg, self.bg.get_rect())
                # plane
                self.our_plane.update(self)
                # bullet
                self.our_plane.bullets.update(self)
                # enemy plane
                self.small_enemies.update()
                # score
                score_text = self.score_font.render(
                    'Score: {0}'.format(self.rest.score),
                    False,
                    constants.TEXT_SOCRE_COLOR
                )
                self.screen.blit(score_text, score_text.get_rect())

            elif self.status == self.OVER:
                # end game
                # background
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # account score
                # 1. total score
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score),
                    False,
                    constants.TEXT_SOCRE_COLOR
                )
                self.screen.blit(score_text, (350,520))

                # 2. the highest score
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()),
                    False,
                    constants.TEXT_SOCRE_COLOR
                )
                self.screen.blit(score_his, (350,300))

            pygame.display.flip()
コード例 #4
0
def main():
    pygame.init()
    width, height = 480, 852  #设置游戏屏幕宽和高
    screen = pygame.display.set_mode((width, height))  #加载屏幕对象
    pygame.display.set_caption('飞机大战')  #设置标题
    bg = pygame.image.load(constants.BG_IMG)  #加载游戏背景图片
    img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)  #加载游戏标题的图片
    t_width, t_height = img_game_title.get_size()  #获取游戏标题图片的宽和高
    img_game_title_rect = img_game_title.get_rect()  #获取游戏标题图片的位置
    img_game_title_rect.left = (width - t_width) // 2  #使游戏标题图片位于屏幕中央
    img_game_title_rect.top = (height // 2 - t_height)  #使游戏标题图片位于屏幕中央
    btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)  #获取开始按钮
    btn_width, btn_height = btn_start.get_size()  #获取开始按钮的宽高
    btn_start_rect = btn_start.get_rect()  #获取开始按钮的位置
    btn_start_rect.left = (width - btn_width) // 2  #使开始按钮位于屏幕中央
    btn_start_rect.top = (height // 2 + btn_height)  #使开始按钮位于屏幕中央

    pygame.mixer.music.load(constants.BG_MUSIC)  #加载游戏背景音乐
    pygame.mixer.music.play(-1)  #设置无限播放模式
    '''设置游戏的状态,并将状态设置为准备'''
    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY

    frame = 0  #设置帧数变量
    ourplane = OurPlane(screen)

    clock = pygame.time.Clock()  #设置帧数

    while True:
        clock.tick(60)
        frame += 1
        if frame >= 60:
            frame = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if status == READY:
                    status = PLAYING
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    ourplane.move_up()
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    ourplane.move_down()
                elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    ourplane.move_left()
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    ourplane.move_right()
                elif event.key == pygame.K_SPACE:
                    ourplane.shoot()

        if status == READY:
            screen.blit(bg, bg.get_rect())
            screen.blit(img_game_title, img_game_title_rect)
            screen.blit(btn_start, btn_start_rect)
        elif status == PLAYING:
            screen.blit(bg, bg.get_rect())
            ourplane.update(frame)
            ourplane.bullets.update()

        pygame.display.flip()
コード例 #5
0
def main():
    """
    游戏入口,main方法
    """
    pygame.init()

    # 游戏状态
    # 0 代表等待中
    # 1 代表游戏中
    # 2 代表游戏结束
    READY = 0
    PLAYING = 1
    END = 2
    # 窗口窗口大小配置
    width, height = 480, 852

    # 创建屏幕对象
    screen = pygame.display.set_mode((width, height))
    # 设置窗口名称
    pygame.display.set_caption('打飞机')

    # 加载背景图片
    bg = pygame.image.load(BG_IMG)
    bg_over = pygame.image.load(BG_IMG_OVER)
    # 加载标题图片
    title_img = pygame.image.load(TITLE_IMG)
    # 加载开始按钮图片
    start_btn_img = pygame.image.load(START_BTN_IMG)

    # 加载背景音乐
    pygame.mixer.music.load(BG_MUSIC)
    # 设置背景音乐的音量
    pygame.mixer.music.set_volume(0.2)
    # 无限循环播放背景音乐
    pygame.mixer.music.play(-1)

    # 获取游戏标题图片位置
    title_img_rect = title_img.get_rect()
    # 获取游戏标题图片尺寸
    title_img_width, title_img_height = title_img.get_size()
    # 设置初始位置
    title_img_rect.topleft = ((width - title_img_width) / 2,
                              (height - title_img_height) / 4)

    # 获取游戏开始按钮图片位置
    start_btn_img_rect = start_btn_img.get_rect()
    start_btn_img_width, start_btn_img_height = start_btn_img.get_size()
    # 设置初始位置
    start_btn_img_rect.topleft = ((width - start_btn_img_width) / 2,
                                  (height - start_btn_img_height) / 2)

    # 初始化状态
    status = READY
    # 帧数计数器
    frame = 0
    clock = pygame.time.Clock()
    # 创建积分实例
    score = Score()
    # 我方飞机实例
    our_plane = OurPlane(screen)
    # 创建敌机精灵组
    enemies_group = pygame.sprite.Group()
    # 生成小型敌方飞机
    for i in range(6):
        small_enemy_plane = SmallEnemyPlane(screen)
        enemies_group.add(small_enemy_plane)

    # 创建积分文字
    score_font = pygame.font.Font('./demo/my_font.ttf', 32)
    # 获取显示屏的宽高
    s_wd, s_ht = screen.get_size()
    # 记录键盘按下的按钮
    key_down = None

    while True:
        clock.tick(60)
        frame += 1
        if frame == 60:
            frame = 0

        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if status == READY:
                    status = PLAYING
            elif event.type == pygame.KEYDOWN:
                if status == PLAYING:
                    key_down = event.key
                    if event.key == pygame.K_DOWN:
                        our_plane.move_down()
                    elif event.key == pygame.K_UP:
                        our_plane.move_up()
                    elif event.key == pygame.K_LEFT:
                        our_plane.move_left()
                    elif event.key == pygame.K_RIGHT:
                        our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        our_plane.shoot()

        # 准备状态
        if status == READY:
            screen.blit(bg, bg.get_rect())
            screen.blit(title_img, title_img_rect)
            screen.blit(start_btn_img, start_btn_img_rect)
        elif status == PLAYING:
            screen.blit(bg, bg.get_rect())
            our_plane.blit_self()
            # 更新飞机
            if our_plane.update(frame, enemies_group, key_down):
                status = END
            # 更新绘制子弹
            our_plane.bullets.update(enemies_group, score)
            enemies_group.update()
            # 绘制当前的积分
            score_text = score_font.render('积分: {}'.format(score.score), False,
                                           SCORE_COLOR)
            screen.blit(score_text, (10, 10))
        elif status == END:
            screen.blit(bg_over, bg_over.get_rect())
            # 显示此次积分数值
            score_text = score_font.render('{}'.format(score.score), False,
                                           SCORE_COLOR)
            score_text_wd, score_text_ht = score_text.get_size()
            screen.blit(score_text, ((s_wd - score_text_wd) / 2,
                                     (s_ht - score_text_ht) / 2))

        # 绘制
        pygame.display.flip()
コード例 #6
0
ファイル: war.py プロジェクト: AnacondaFeng/pygame
class PlaneWar(object):
    """飞机大战"""

    # 游戏状态 0.准备中,1.游戏中,2.游戏结束
    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY

    # 实例化飞机对象
    our_plane = None

    frame = 0  # 播放帧数

    # 敌方小飞机精灵组,可以属于多个精灵组,便于碰撞检测
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        # 初始化游戏
        pygame.init()

        # 指定屏幕高度、宽度
        self.width, self.height = 480, 852

        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))

        # 设置窗口标题
        pygame.display.set_caption('飞机大战')

        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)

        # 加载游戏标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 标题高度和宽度
        self.t_width, self.t_height = self.img_game_title.get_size()

        self.img_game_title_rect.topleft = (int(
            (self.width - self.t_width) /
            2), int(self.height / 2 - self.t_height))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        self.btn_width, self.btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int((self.width - self.btn_width) / 2),
                                       int(self.height / 2 + self.btn_height))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('arial', 32)

        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        # 无限循环播放
        pygame.mixer.music.play(-1)
        # 设置音量
        pygame.mixer.music.set_volume(0.3)

        # 上次按的键盘
        self.key_down = None

        # 实例化飞机对象
        self.our_plane = OurPlane(self.screen, speed=8)

        self.clock = pygame.time.Clock()

    def bind_event(self):
        # 1.监听事件
        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                #         鼠标点击进入游戏
                # 游戏正在准备中才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                #         键盘事件
                # 游戏正在游戏中,才需要控制键盘 ASWD
                self.key_down = event.key
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        # 随机添加num架小飞机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 4)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环部分"""
        while True:
            # 设置帧速率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 绑定事件
            self.bind_event()

            # 2.更新游戏的状态
            if self.status == self.READY:
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 游戏正在准备中,绘制开始画面,绘制标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                # 重置按键
                self.key_down = None

            elif self.status == self.PLAYING:
                #     游戏进行中
                self.screen.blit(self.bg, self.bg.get_rect())
                #     绘制飞机
                self.our_plane.update(self)
                #     绘制子弹
                self.our_plane.bullets.update(self)
                #     绘制敌方飞机
                self.small_enemies.update()
                #     游戏分数
                score_text = self.score_font.render(
                    'Score:{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())

            elif self.status == self.OVER:
                #     游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 1.本次总分
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)
                # 2.历史最高分
                score_hist = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_hist, (150, 40))
                # 刷新屏幕

            pygame.display.flip()
コード例 #7
0
ファイル: war.py プロジェクト: paipij/PlaneWars
class PlaneWar:
    """飞机大战"""
    # 游戏状态
    READY = 0  # 准备状态
    PLAYING = 1  # 游戏中
    OVER = 2  # 结束游戏
    status = READY

    our_plane = None

    frame = 0  # 播发帧数
    clock = pygame.time.Clock()

    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        pygame.init()
        self.width, self.height = 480, 852

        # 屏幕对象,设计窗口大小
        self.screen = pygame.display.set_mode((self.width, self.height))

        # 设计窗口标题
        pygame.display.set_caption('飞机大战')

        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)

        # 游戏标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 游戏标题的宽和高
        self.t_width, self.t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - self.t_width) /
            2), int((self.height - self.t_height) / 2 - 40))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START)
        self.btn_start_rect = self.btn_start.get_rect()
        self.btn_width, self.btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - self.btn_width) /
            2), int((self.height / 2 + self.btn_height)))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('华文隶书', 32)

        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)

        pygame.mixer.music.play(-1)  # 无限的循环
        pygame.mixer.music.set_volume(0.2)

        # 我放飞机对象
        self.our_plane = OurPlane(self.screen, speed=5)

        self.clock = pygame.time.Clock()
        # 上次按得键盘上的某一个,用于控制飞机
        self.key_down = None

    def bind_event(self):
        """绑定事件"""
        # 1.监听事件
        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏正在准备中,点击才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                    self.rest.score = 0
                elif self.status == self.PLAYING:
                    # 点击鼠标发射子弹
                    self.our_plane.shoot()
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = event.key
                # 游戏中,才需要控制键盘AWSD
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_rige()
                    elif event.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        随机生成N架小型飞机
        :param num: 飞机的生产数量
        :return:
        """
        # 随机生成num架小型飞机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 5)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环部分"""
        while True:
            # 1.设计帧数
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 2.绑定事件
            self.bind_event()

            # 3.更新游戏状态
            if self.status == self.READY:
                self.screen.blit(self.bg, self.bg.get_rect())
                #标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                #开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分: {0}'.format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.OVER:
                # 游戏结束
                # 游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 1. 本次总分
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)
                # 2. 历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SOCRE_COLOR)
                self.screen.blit(score_his, (150, 40))

            pygame.display.flip()
コード例 #8
0
ファイル: war.py プロジェクト: Dunky-Z/PlaneWar
class PlaneWar(object):
    """飞机大战"""
    # 游戏状态
    READY = 0  # 游戏准备着
    PLAYING = 1  # 游戏中
    OVER = 2  # 游戏结束
    status = READY

    our_plane = None

    frame = 0  # 播放帧数
    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()

    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        # 初始化游戏
        pygame.init()
        # 屏幕对象
        self.width, self.height = 480, 852
        self.screen = pygame.display.set_mode((self.width, self.height))

        # 设置窗口标题
        pygame.display.set_caption('Plane War')

        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)

        # 游戏的标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()

        # 获取背景宽度和高度
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(self.width - t_width) / 2,
                                            int(self.height / 2 - t_height))
        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(self.width - btn_width) / 2,
                                       int(self.height / 2 + btn_height))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('Yahei Consolas Hybrid', 20)
        self.score_font_all = pygame.font.SysFont('Yahei Consolas Hybrid', 80)
        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.play(-1)  # 循环播放
        pygame.mixer.music.set_volume(0.8)

        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=10)
        self.clock = pygame.time.Clock()

        # 上次按的键盘的按键
        self.key_down = None

    def bind_event(self):
        """绑定事件"""
        # 1.监听事件
        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏真正准备中,
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                # 监听键盘按下
                self.key_down = event.key
                print(event.key)
                # 正在游戏,控制键盘
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        self.our_plane.shot()

    def add_small_enemies(self, num):
        """
        随机添加N架飞机
        :param num:飞机产生的数量
        :return:
        """
        for i in range(num):
            # 随机添加六架飞机
            plane = SmallEnemyPlane(self.screen, 3)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环"""
        while True:
            # 1.设置帧速率
            self.clock.tick(60)
            self.frame += 1
            if (self.frame >= 60):
                self.frame = 0

            # 2.绑定事件
            self.bind_event()

            # 3. 更新游戏的状态
            if self.status == self.READY:
                # 游戏真正准备中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 绘制开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.OVER:
                # 游戏结束
                # 1.游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 2.分数统计
                # 本次总分
                score_text = self.score_font_all.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int((self.height / 2)))
                self.screen.blit(score_text, score_text_rect)
                # 历史最高分
                score_history = self.score_font.render(
                    '{0}'.format(self.rest.get_max_score()), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_history, (150, 40))
            pygame.display.flip()
コード例 #9
0
class PlaneWar(object):
    """飞机大战"""

    READY = 0  # 游戏准备中
    PLAYING = 1  # 游戏中
    OVER = 2  # 游戏结束
    status = READY
    # 实例化我方飞机
    our_plane = None
    # # 实例化游戏结果
    # rest = None
    # 播放帧数
    frame = 0
    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    clock = pygame.time.Clock()

    def __init__(self):
        # 初始化
        pygame.init()
        # 设置宽度高度
        self.width, self.height = 480, 852
        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # 游戏标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 获取游戏标题的宽度高度
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int((self.height - t_height) / 2))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        # 获取开始按钮的宽度高度
        self.btn_width, self.btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - self.btn_width) /
            2), int(self.height / 2 + self.btn_height + 20))

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('kaiti', 50)

        # 加载背景音乐
        # pygame.mixer.music.load(constants.BG_MUSIC)
        # pygame.mixer.music.play(-1)  # 设置循环播放
        # pygame.mixer.music.set_volume(0.2)  # 音量设置
        # 设置窗口标题
        pygame.display.set_caption('飞机大战')
        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=20)
        # 实例化游戏结果
        self.rest = PlayRest()
        self.clock = pygame.time.Clock()
        # 上次按的键盘的某一个键
        self.key_down = None

    def bind_event(self):
        """绑定事件"""
        # 监听事件
        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏正在准备中,点击才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                # 游戏结束后,鼠标点击可以继续进行游戏
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
                    self.rest.score = 0
            elif event.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = event.key
                # 游戏正在进行中,才需要控制键盘
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """随机添加N架小型飞机"""
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, speed=8)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环部分"""
        while True:
            # 1.设置帧速率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 2.绑定事件
            self.bind_event()
            # 3.更新游戏的状态
            if self.status == self.READY:
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 绘制游戏分数
                score_text = self.score_font.render(
                    "得分:{}".format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.OVER:
                # 游戏结束
                # 游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 绘制本次总分数
                score_text = self.score_font.render(
                    "{}".format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                score_text_rect.topleft = (int(
                    (self.width - text_w) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)
                # 历史最高分
                score_his = self.score_font.render(
                    '{}'.format(self.rest.get_max_score()), False,
                    constants.TEXT_SOCRE_COLOR)
                self.screen.blit(score_his, (150, 30))
            pygame.display.flip()
コード例 #10
0
ファイル: war.py プロジェクト: Mark910413/plane-game
class PlaneWar(object):
    """飞机大战"""
    # 游戏状态 0 准备中 1 游戏中 2 结束
    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY
    # 装载我方飞机
    our_plan = None
    # 帧数计数器
    frame = 0
    # 敌方飞机精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    # 分数
    rest = PlayRest()

    def __init__(self):
        # 初始化游戏
        pygame.init()
        # 屏幕宽高
        self.width, self.height = 480, 852
        # 打开屏幕
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        # 加载结束图片
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # 加载开始标题图片
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 标题的宽高
        t_width, t_height = self.img_game_title.get_size()
        # 设置标题的位置
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int((self.height - t_height) / 2))

        # 加载开始按钮
        self.img_game_start_btn = pygame.image.load(
            constants.IMG_GAME_START_BTN)
        self.img_game_start_btn_rect = self.img_game_start_btn.get_rect()
        btn_width, btn_height = self.img_game_start_btn.get_size()
        # 设置按钮开始按钮位置
        self.img_game_start_btn_rect.topleft = (int(
            (self.width - btn_width) / 2), int(
                (self.height - btn_height) / 2) + 100)

        # 游戏文字对象
        self.score_font = pygame.font.SysFont('fangsong', 32)
        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.set_volume(0.8)
        pygame.mixer.music.play(-1)

        # 装载我方飞机
        self.our_plan = OurPlane(self.screen, 8)

        self.clock = pygame.time.Clock()

    def bind_event(self):
        """ 绑定事件 """
        # 监听事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.status == self.READY:
                    self.status = self.PLAYING

        # 被按下的键
        key_pressed = pygame.key.get_pressed()
        if self.status == self.PLAYING:
            if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
                self.our_plan.move_up()
            if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
                self.our_plan.move_down()
            if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
                self.our_plan.move_left()
            if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
                self.our_plan.move_right()
            if key_pressed[pygame.K_SPACE] and (self.frame % 10 == 0):
                self.our_plan.shoot(10)

    def add_small_enemies(self, num):
        # 装载敌方小飞机
        for i in range(num):
            plan = SmallEnemyPlane(self.screen, 2)
            plan.add(self.small_enemies, self.enemies)

    def run_game(self):
        """ 游戏的主循环 """
        while True:
            # 绑定事件
            self.bind_event()
            # 设置帧数率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0

            # 各个状态下
            if self.status == self.READY:
                # 游戏准备中
                # 绘制背景图
                # 绘制开始按钮
                self.screen.blit(self.bg, self.bg.get_rect())
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                self.screen.blit(self.img_game_start_btn,
                                 self.img_game_start_btn_rect)
            elif self.status == self.PLAYING:
                # 绘制游戏中的画面
                # 绘制背景图片
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制我方飞机
                self.our_plan.update(self.frame, self)
                # 绘制子弹
                self.our_plan.bullets.update(self)
                # 绘制敌方飞机
                # print(self.small_enemies)
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                score_text_rect.topleft = (20, 50)
                self.screen.blit(score_text, score_text_rect)
            elif self.status == self.OVER:
                # 绘制游戏结束的画面
                # 绘制背景图
                self.screen.blit(self.bg_over, self.bg.get_rect())
                # 统计分数
                # 游戏分数
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                score_text_rect.centerx = int(self.width / 2)
                score_text_rect.centery = int(self.height / 2)
                self.screen.blit(score_text, score_text_rect)
                # 历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SCORE_COLOR)
                score_his_rect = score_text.get_rect()
                score_his_rect.topleft = (150, 40)
                self.screen.blit(score_his, score_his_rect)
            pygame.display.flip()
コード例 #11
0
ファイル: war.py プロジェクト: Vincent9966/aircraft-battle
class PlaneWar(object):
    """ 飞机大战     控制飞机的整个流程 """
    # 设置游戏状态
    READY = 0  # 游戏准备中
    PLAYING = 1  # 游戏中
    OVER = 2  # 游戏结束
    status = READY  # 0准备中,1 游戏中,2 游戏结束

    our_plane = None

    frame = 0  # 播放帧数

    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        """   将所有需要初始化的东西初始化一遍    """
        # 忘记函数应该调用什么元素时,应该将pygame官网打开进行查阅
        # 初始化游戏
        pygame.init()
        self.width, self.height = 480, 852
        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置窗口标题
        pygame.display.set_caption('飞机大战')

        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # 游戏的标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 宽度和高度
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - btn_width) / 2), int(self.height / 2 + btn_height))

        # 游戏文字对象
        self.score_font = pygame.font.Font(constants.TEXT_FONT, 35)

        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.play(-1)  # 无限循环播放
        pygame.mixer.music.set_volume(0.2)  # 设置音量

        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=4)

        self.clock = pygame.time.Clock()

        # 保存上次按下的键
        self.key_down = None

    def bind_event(self):
        """ 绑定事件 """
        # 1. 监听事件
        for event in pygame.event.get():
            # 退出游戏
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏正在准备中,点击才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
                    self.rest.score = 0

            elif event.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = event.key
                # 游戏正在游戏中,才需要控制键盘 A S W D
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        随机添加N架小型敌机
        :param num: 飞机的产生数量
        :return:
        """
        # 随机添加6架小型敌机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 4)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """ 游戏主循环部分 """
        while True:
            # 1. 设置帧速率
            self.clock.tick(60)
            self.frame = (self.frame + 1) % 60

            # 2. 绑定事件
            self.bind_event()

            # 3.更新游戏的状态
            if self.status == self.READY:
                # 游戏正在准备中
                self.key_down = None
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update(self)
                # 游戏分数
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False,
                    constants.TEXT_SOCER_COLOR)
                self.screen.blit(score_text, score_text.get_rect())

                # 游戏结束
            elif self.status == self.OVER:
                # 游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                #  绘制本次总分
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SOCER_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_w) / 2), int(self.height / 2) - 30)
                self.screen.blit(score_text, score_text_rect)
                # 绘制历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_sore()), False,
                    constants.TEXT_SOCER_COLOR)
                self.screen.blit(score_his, (150, 38))

            pygame.display.flip()
コード例 #12
0
class PlaneWar(object):
    """飞机大战"""

    READY = 0
    PLAYING = 1
    OVER = 2
    status = READY

    our_plane = None

    frame = 0  #设置帧数
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    #游戏结果
    rest = PlayRest()

    def __init__(self):
        #初始化游戏
        pygame.init()
        self.width, self.height = 480, 852
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置窗口标题
        pygame.display.set_caption("飞机大战")
        # 加载背景
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # 游戏标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()  # 获取图片位置对象
        t_width, t_height = self.img_game_title.get_size()  # 获取图片宽度和高度
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height)
                                            )  # 设置图片的位置
        # 开始按钮,
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - btn_width) / 2), int(self.height / 2 + btn_height)
                                       )  # 设置图片的位置

        #游戏文字对象
        self.score_font = pygame.font.SysFont('华文隶书', 32)
        # # 背景音乐
        # pygame.mixer.music.load(constants.BG_MUSIC)
        # # 循环播放
        # pygame.mixer.music.play(-1)
        # # 音量
        # pygame.mixer.music.set_volume(0.2)
        #我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=20)
        self.clock = pygame.time.Clock()
        self.key_down = None

    def bind_event(self):
        """"绑定事件"""

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == pygame.MOUSEBUTTONDOWN:  #鼠标点击按下
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                self.key_down = event.key
                if self.status == 1:
                    if event.key == pygame.K_w or event.type == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.type == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.type == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.type == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        #发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        随机添加num架小飞机
        :param num:
        :return:
        """
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 8)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """游戏主循环"""
        while True:
            # 设置帧数率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:  # 减少内存
                frame = 0
            self.bind_event()
            #更新游戏状态
            if self.status == self.READY:
                # 游戏准备中

                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 绘制开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中

                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.blit_me()
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                #游戏分数
                score_text = self.score_font.render(
                    "得分:{0}".format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.OVER:
                #游戏结束
                #游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())

                #分数统计
                #绘制总分数,本次
                score_text = self.score_font.render(
                    "{0}".format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                #改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)

                #历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_his, (150, 40))

            pygame.display.flip()
コード例 #13
0
class PlaneWar(object):
    ''' 飞机大战 '''

    READY = 0  #游戏准备中
    PLAYING = 1  #游戏中
    OVER = 2  #游戏结束
    status = READY  # 游戏的状态: 0.准备中 1. 游戏中  3.游戏结束

    # 我方飞机
    our_plane = None

    frame = 0  # 播放的帧数
    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    #游戏结果
    rest = PlayRest()

    def __init__(self):
        #初始化游戏
        pygame.init()
        # 屏幕宽度,高度
        self.width, self.height = 480, 852

        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置窗口标题
        pygame.display.set_caption('飞机大战')
        # 加载
        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMG)
        self.bg_over = pygame.image.load(constants.BG_IMG_OVER)
        # bg_ready = pygame.image.load(constants.BG_IMG_READY)       #图片一样不用实现

        # 游戏的标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 获取游戏标题的宽度和高度
        t_width, t_height = self.img_game_title.get_size()
        # 标题摆放的位置
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        # 获取按钮的宽度和高度
        btn_width, btn_height = self.btn_start.get_size()
        # 开始按钮摆放的位置
        self.btn_start_rect.topleft = (int(
            (self.width - btn_width) / 2), int(self.height / 2 + btn_height))

        #游戏文字对象
        self.score_font = pygame.font.Font(constants.FONT_SCORE, 32)

        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        # 循环播放音乐
        pygame.mixer.music.play(-1)
        # 设置音量的大小
        pygame.mixer.music.set_volume(0.2)

        #我放飞机实例
        self.our_plane = OurPlane(self.screen, speed=4)

        self.clock = pygame.time.Clock()

        #上次按的键盘的某一键,用于控制飞机
        self.key_down = None

    def bind_event(self):
        ''' 绑定事件 '''

        # 1.监听事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 退出事件
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏在准备中,才能进入 游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = event.key
                # 游戏正在游戏中才需要控制键盘事件 ASWD
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        # 发射子弹
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        '''
        随即添加n架小型敌机
        :param num: 飞机产生的数量
        :return:
        '''
        # 随机添加六架小型飞机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 4)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        ''' 游戏的主循环 '''
        #1.设置帧速率
        while True:
            self.clock.tick(60)
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 2.绑定事件
            self.bind_event()

            #3.更新游戏的状态:
            if self.status == self.READY:
                # 游戏正在准备中
                # 3.绘制游戏状态
                # 3.1绘制背景图片
                self.screen.blit(self.bg, self.bg.get_rect())
                #标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.key_down = None
                self.rest.score = 0
            elif self.status == self.PLAYING:
                #游戏进行中
                #绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                #绘制飞机
                self.our_plane.update(self)
                #绘制子弹
                self.our_plane.bullets.update(self)
                #绘制敌方飞机
                self.small_enemies.update()
                #绘制游戏分数
                score_text = self.score_font.render(
                    '得分:{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.OVER:
                #游戏结束
                #游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                #分数统计
                #1.绘制本次总分
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SCORE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                #改变文字位置
                score_text_rect.topleft = (int(
                    (self.width - text_h) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)
                #2.历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SCORE_COLOR)
                self.screen.blit(score_his, (150, 40))

            # 4.更新屏幕
            pygame.display.flip()
コード例 #14
0
ファイル: war.py プロジェクト: CAICAISKY/PWGame
class PlaneWar(object):
    """飞机大战程序执行类"""

    # 定义游戏状态常量
    READY = 0
    PLAYING = 1
    GAME_OVER = 2

    def __init__(self):
        # 游戏初始化
        pygame.init()
        """屏幕设定"""
        # 设置屏幕大小,这里根据游戏背景图片大小即可
        self.width, self.height = 480, 852
        # 获取屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        # 设置屏幕标题
        pygame.display.set_caption('飞机大战')
        """图片设定"""
        # 加载背景图片,获取背景surface对象
        self.bg_img = pygame.image.load(constants.BG_IMG)
        # 加载游戏开始标题, 设置图片位置
        self.game_start_title = pygame.image.load(
            constants.GAME_START_TITLE_IMG)
        self.game_start_title_rect = self.game_start_title.get_rect()
        t_width, t_height = self.game_start_title.get_size()
        self.game_start_title_rect.topleft = (int(
            (self.width - t_width) / 2), int((self.height / 2 - t_height)))
        # 加载游戏开始按钮,设置按钮位置
        self.game_start_btn = pygame.image.load(constants.GAME_START_BTN_IMG)
        self.game_start_btn_rect = self.game_start_btn.get_rect()
        b_width, b_height = self.game_start_btn.get_size()
        self.game_start_btn_rect.topleft = (int(
            (self.width - b_width) / 2), int((self.height / 2 + b_height)))
        # 加载游戏结束图片
        self.game_over_img = pygame.image.load(constants.GAME_OVER_IMAGE)
        # 加载得分字体
        self.score_font = pygame.font.SysFont("songtittc", 21, False, False)
        """音乐设定"""
        self.play_bg_music()
        """游戏状态设定"""
        self.status = self.READY
        """调节主循环运行帧数"""
        # 获取游戏时钟对象
        self.clock = pygame.time.Clock()
        # 设置主循环计数变量
        self.frame = 0
        """创建我方飞机"""
        # 创建我方飞机
        self.our_plane = OurPlane(self.screen)
        """创建敌方飞机"""
        # 创建敌方精灵组
        self.enemies = pygame.sprite.Group()
        # 创建敌方小型飞机精灵族
        self.small_enemy_planes = pygame.sprite.Group()
        """创建游戏结果实例"""
        self.result = PlaneResult()
        """上一次键盘按键,用于移动优化"""
        self.key_down = None

    @staticmethod
    def play_bg_music():
        """加载并播放背景音乐"""
        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        # 设置背景音乐音量为20%
        pygame.mixer.music.set_volume(0.2)
        # 背景音乐循环播放
        pygame.mixer.music.play(-1)

    def create_small_enemy_planes(self, num):
        """
        # 循环创建num架敌方小型飞机,并放入到精灵组中
        :param num: 敌方小型飞机数量
        :return:
        """
        for i in range(num):
            small_enemy_plane = SmallEnemyPlane(self.screen)
            # add方法为pygame的精灵类方法,目的是将小型飞机放入精灵组中
            small_enemy_plane.add(self.small_enemy_planes, self.enemies)

    def run_game(self):
        """运行游戏"""
        while True:
            # 控制帧数,每秒60帧
            self.clock.tick(60)

            # 计数
            self.frame += 1
            if self.frame > 60:
                self.frame = 0

            # 处理游戏事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    # 当游戏处于准备状态时,点击鼠标游戏开始
                    if self.status == self.READY:
                        self.status = self.PLAYING
                    # 当游戏处于结束转改,点击鼠标则重新开始
                    elif self.status == self.GAME_OVER:
                        # 消除我方子弹
                        self.our_plane.bullets.empty()
                        # 创建六架飞机
                        self.create_small_enemy_planes(6)
                        # 消除键盘记录
                        pass
                        # 更新游戏状态
                        self.status = self.PLAYING
                elif event.type == pygame.KEYDOWN:
                    # 监视键盘,当在游戏进行状态下按下键盘时,作出对应操作
                    if self.status == self.PLAYING:
                        # 记录键盘按钮
                        self.key_down = event.key
                        if event.key == pygame.K_w or event.key == pygame.K_UP:
                            self.our_plane.move_up()
                        elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                            self.our_plane.move_down()
                        elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                            self.our_plane.move_left()
                        elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                            self.our_plane.move_right()
                        elif event.key == pygame.K_SPACE:
                            self.our_plane.shoot()
                elif event.type == pygame.KEYUP:
                    # 清除上一次键盘按钮记录
                    self.key_down = None

            # 更新游戏状态
            if self.status == self.READY:
                # 屏幕加载背景、标题、开始按钮
                self.screen.blit(self.bg_img, self.bg_img.get_rect())
                self.screen.blit(self.game_start_title,
                                 self.game_start_title_rect)
                self.screen.blit(self.game_start_btn, self.game_start_btn_rect)
            elif self.status == self.PLAYING:
                # 屏幕加载背景
                self.screen.blit(self.bg_img, self.bg_img.get_rect())
                # 绘制我方飞机
                self.our_plane.blit_me()
                # 更换我方飞机图片造成动态效果
                self.our_plane.update(self)
                # 绘制、更新子弹
                self.our_plane.bullets.update(self)
                # 绘制、更新敌方小型飞机
                self.small_enemy_planes.update()
                # 绘制得分
                score_text = self.score_font.render(
                    "得分: {0}".format(self.result.score), False,
                    constants.SCORE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())
            elif self.status == self.GAME_OVER:
                # 绘制游戏结束画面
                self.screen.blit(self.game_over_img,
                                 self.game_over_img.get_rect())
                # 绘制目前得分
                score_text = self.score_font.render(
                    "{0}分".format(self.result.score), False,
                    constants.SCORE_COLOR)
                score_rect = score_text.get_rect()
                s_width, s_height = score_text.get_size()
                score_rect.topleft = (int(
                    (self.width - s_width) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_rect)
                # 记录游戏最高分
                self.result.record_highest_score()
                # 绘制游戏历史最高分
                history_score_text = self.score_font.render(
                    "{0}分".format(self.result.get_history_score()), False,
                    constants.SCORE_COLOR)
                history_score_rect = history_score_text.get_rect()
                history_score_rect.topleft = (150, 40)
                self.screen.blit(history_score_text, history_score_rect)

            # 展示屏幕
            pygame.display.flip()
コード例 #15
0
ファイル: war.py プロジェクト: wxywizard/imooc_learn_python
class PlaneWar(object):
    # 游戏状态

    READY = 0  # 游戏准备中
    PLAYING = 1  # 游戏中
    OVER = 2  # 游戏结束

    # 0.准备中,1.游戏中,2.游戏结束
    status = READY

    our_plane = None

    # 播放帧数
    frame = 0

    # 定义小型飞机精灵组
    # 一架飞机可以属于多个精灵组
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()

    # 游戏结果
    rest = PlayRest()

    def __init__(self):
        pygame.init()
        self.width, self.height = 480, 852
        # 屏幕对象
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption('Ryna的飞机大战')

        # 加载背景图片
        self.bg = pygame.image.load(constants.BG_IMAGE)
        self.bg_over = pygame.image.load(constants.BG_IMAGE_OVER)
        # 游戏的标题
        self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE)
        self.img_game_title_rect = self.img_game_title.get_rect()
        # 宽度和高度
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int(
            (self.width - t_width) / 2), int(self.height / 2 - t_height))

        # 开始按钮
        self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft = (int(
            (self.width - btn_width) / 2), int(self.height / 2 + btn_height))

        # ryna头像
        self.img_ryna = pygame.image.load(constants.IMG_RYNA)
        self.img_ryna_rect = self.img_ryna.get_rect()
        r_width, r_height = self.img_ryna.get_size()
        self.img_ryna_rect.topleft = (int(
            (self.width - r_width) / 2), int(self.height / 4 - r_height))

        # 游戏文字对象
        self.score_font = pygame.font.Font(constants.PLAY_FONT, 40)

        # 加载背景音乐
        pygame.mixer.music.load(constants.BG_MUSIC)
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.2)

        # 我方飞机对象
        self.our_plane = OurPlane(self.screen, speed=4)

        self.clock = pygame.time.Clock()

        # 上次按的键盘上的某一个键,用于控制飞机
        self.key_down = None

    def bind_event(self):
        """ 绑定事件 """
        # 1.监听事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠标点击进入游戏
                # 游戏正在准备中,点击才能进入游戏
                if self.status == self.READY:
                    self.status = self.PLAYING
                elif self.status == self.OVER:
                    self.status = self.READY
                    self.add_small_enemies(6)
            elif event.type == pygame.KEYDOWN:
                # 键盘事件
                self.key_down = event.key
                # 游戏正在游戏中,才需要控制键盘 ASWD
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.our_plane.move_up()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.our_plane.move_down()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.our_plane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.our_plane.move_right()
                    elif event.key == pygame.K_SPACE:
                        self.our_plane.shoot()

    def add_small_enemies(self, num):
        """
        随机添加N架小型敌机
        :param num: 飞机数量
        :return:
        """
        # 随机添加6架小型敌机
        for i in range(num):
            plane = SmallEnemyPlane(self.screen, 8)
            plane.add(self.small_enemies, self.enemies)

    def run_game(self):
        """ 游戏的主循环部分 """
        while True:
            # 1.设置帧速率
            self.clock.tick(60)
            self.frame += 1
            if self.frame >= 60:
                self.frame = 0
            # 2.绑定事件
            self.bind_event()
            # 3.更新游戏状态
            if self.status == self.READY:
                # 游戏正在准备中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 标题
                self.screen.blit(self.img_game_title, self.img_game_title_rect)
                # 开始按钮
                self.screen.blit(self.btn_start, self.btn_start_rect)
                self.screen.blit(self.img_ryna, self.img_ryna_rect)
                self.key_down = None
            elif self.status == self.PLAYING:
                # 游戏进行中
                # 绘制背景
                self.screen.blit(self.bg, self.bg.get_rect())
                # 绘制飞机
                self.our_plane.update(self)
                # 绘制子弹
                self.our_plane.bullets.update(self)
                # 绘制敌方飞机
                self.small_enemies.update()
                # 游戏分数
                score_text = self.score_font.render(
                    '得分: {0}'.format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                self.screen.blit(score_text, score_text.get_rect())

            elif self.status == self.OVER:
                # 游戏结束
                # 游戏背景
                self.screen.blit(self.bg_over, self.bg_over.get_rect())
                # 分数统计
                # 1. 本次总分
                score_text = self.score_font.render(
                    '{0}'.format(self.rest.score), False,
                    constants.TEXT_SOCRE_COLOR)
                score_text_rect = score_text.get_rect()
                text_w, text_h = score_text.get_size()
                # 改变文字的位置
                score_text_rect.topleft = (int(
                    (self.width - text_w) / 2), int(self.height / 2))
                self.screen.blit(score_text, score_text_rect)
                # 2. 历史最高分
                score_his = self.score_font.render(
                    '{0}'.format(self.rest.get_max_core()), False,
                    constants.TEXT_SOCRE_COLOR)

                self.screen.blit(score_his, (150, 40))

            pygame.display.flip()