Exemplo n.º 1
0
def main():
    pygame.mixer.music.play(-1)
    global background

    #游戏分数
    best_score = 0
    your_score = 0
    #字体
    font = pygame.font.Font('font/font.ttf', 36)
    game_over_font = pygame.font.Font('font/font.ttf', 50)
    #生成我方飞机
    me = myPlane.MyPlane(bg_size)
    #生成敌方飞机
    enemies = pygame.sprite.Group()
    small_planes = pygame.sprite.Group()
    add_small_planes(small_planes, enemies, 15)
    middle_planes = pygame.sprite.Group()
    add_middle_planes(middle_planes, enemies, 5)
    big_planes = pygame.sprite.Group()
    add_big_planes(big_planes, enemies, 2)
    #生成子弹
    bullet1 = []
    bullet1_index = 0
    BULLET1_NUM = 4
    for i in range(BULLET1_NUM):
        bullet1.append(bullet.Bullet1(me.rect.midtop))
    #生成超级子弹
    bullet2 = []
    bullet2_index = 0
    BULLET2_NUM = 4
    for i in range(BULLET2_NUM):
        bullet2.append(bullet.Bullet2(me.rect.midtop))
    #全屏炸弹
    bomb_image = pygame.image.load('image/bomb.png').convert_alpha()
    bomb_rect = bomb_image.get_rect()
    bomb_rect.left = 0
    bomb_rect.bottom = bg_height
    bomb_num = 3
    #玩家生命
    life_image = pygame.image.load('image/myPlaneLife.png').convert_alpha()
    life_rect = life_image.get_rect()
    life_rect.left = bg_width // 2
    life_rect.bottom = bg_height
    life_num = 3
    #补给包
    bomb_supply = supply.BombSupply(bg_size)
    bullet_supply = supply.BulletSupply(bg_size)
    #补给包定时器,设定每30秒发放一次补给包
    SUPPLY_TIME = USEREVENT
    pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
    #超级子弹定时器
    SUPPER_BULLET_TIME = USEREVENT + 1
    is_supper_bullet = False
    #我方飞机无敌定时器
    MY_PLANE_INVINCIBLE_TIME = USEREVENT + 2

    #记录飞机毁灭之后隔多久重置
    big_plane_destroy = 0
    middle_plane_destroy = 0
    small_plane_destroy = 0
    my_plane_destroy = 0
    
    #用于切换图片
    switch_image = True
    #用于延迟
    delay = 100
    #游戏暂停
    paused = False
    pause_image = pygame.image.load('image/pause.png').convert_alpha()
    pause_rect = pause_image.get_rect()
    pause_rect.left = bg_width // 2 - pause_rect.width // 2
    pause_rect.top = bg_height // 2 - pause_rect.height // 2
    #游戏难度级别
    level = 1
    #用于防止重复打开记录文件
    recorded = False
    #游戏结束后的选项及其属性
    quit_text = font.render('Quit', True, WHITE)
    again_text = font.render('Again', True, WHITE)
    quit_pos = quit_text.get_rect()
    again_pos = again_text.get_rect()
    quit_pos.left = bg_width // 3 - quit_pos.width // 2
    quit_pos.top = bg_height // 2 + 150
    again_pos.left = bg_width // 3 * 2 - again_pos.width // 2
    again_pos.top = bg_height // 2 + 150
    
    clock = pygame.time.Clock()
    running = True

    #绘制前言介绍
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            elif event.type == KEYDOWN:
                running = False

        info = pygame.image.load('image/info.jpg').convert()
        screen.blit(info, (0, 0))

        pygame.display.flip()
        clock.tick(30)

    #游戏正式开始
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            #点击鼠标游戏暂停
            elif life_num > 0 and event.type == MOUSEBUTTONDOWN:
                paused = not paused
                if paused:#暂停游戏时关闭音乐音效和补给
                    pygame.time.set_timer(SUPPLY_TIME, 0)
                    pygame.mixer.music.pause()
                    pygame.mixer.pause()
                else:#非暂停状态则重新开启资源
                    pygame.time.set_timer(SUPPLY_TIME, 30)
                    pygame.mixer.music.unpause()
                    pygame.mixer.unpause()
            #空格引爆全屏炸弹
            elif event.type == KEYDOWN and \
                 life_num > 0 and \
                 not paused:
                if event.key == K_SPACE and bomb_num > 0:
                    bomb_num -= 1
                    bomb_sound.play()
                    for each in enemies:
                        if each.rect.bottom > 0:#炸毁对象必须出现在屏幕中
                            each.is_alive = False
            #每30秒随机发放一个补给包
            elif event.type == SUPPLY_TIME:
                if random.choice([True, False]):
                    bomb_supply.reset()
                else:
                    bullet_supply.reset()
            #开启超级子弹18秒后自动关闭
            elif event.type == SUPPER_BULLET_TIME:
                is_supper_bullet = False
                pygame.time.set_timer(SUPPER_BULLET_TIME, 0)
            #重生3秒后关闭无敌状态
            elif event.type == MY_PLANE_INVINCIBLE_TIME:
                me.invincible = False
                pygame.time.set_timer(MY_PLANE_INVINCIBLE_TIME, 0)
            #gameover之后玩家的选择
            elif life_num == 0 and event.type == MOUSEBUTTONDOWN:
                mouse_pos = event.pos
                #玩家选择退出
                if mouse_pos[0] > quit_pos.left and \
                   mouse_pos[0] < quit_pos.right and \
                   mouse_pos[1] > quit_pos.top and \
                   mouse_pos[1] < quit_pos.bottom:
                    sys.exit()
                #玩家选择再玩
                if mouse_pos[0] > again_pos.left and \
                   mouse_pos[0] < again_pos.right and \
                   mouse_pos[1] > again_pos.top and \
                   mouse_pos[1] < again_pos.bottom:
                    #重设背景并播放背景音乐同时关闭gameover音效
                    background = pygame.image.load('image/background.jpg').convert()
                    pygame.mixer.stop()
                    pygame.mixer.music.play(-1)
                    #重设分数
                    your_score = 0
                    #重新生成敌机
                    enemies = pygame.sprite.Group()
                    small_planes = pygame.sprite.Group()
                    add_small_planes(small_planes, enemies, 15)
                    middle_planes = pygame.sprite.Group()
                    add_middle_planes(middle_planes, enemies, 5)
                    big_planes = pygame.sprite.Group()
                    add_big_planes(big_planes, enemies, 2)
                    #重设全屏炸弹数量及玩家生命数,重设我方飞机
                    bomb_num = 3
                    life_num = 3
                    me.reset()
                    pygame.time.set_timer(MY_PLANE_INVINCIBLE_TIME, 3000)
                    #开启补给包,超级子弹判断重设
                    pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
                    is_supper_bullet = False
                    #重设飞机毁灭恢复参数
                    big_plane_destroy = 0
                    middle_plane_destroy = 0
                    small_plane_destroy = 0
                    my_plane_destroy = 0
                    #其他设定
                    switch_image = True
                    delay = 100
                    paused = False
                    level = 1
                    recorded = False
        
        #根据用户得分增加游戏难度
        if level == 1 and your_score > 50000:
            level = 2
            #增加3加小型敌机,2架中型敌机,1加大型敌机
            add_small_planes(small_planes, enemies, 3)
            add_middle_planes(middle_planes, enemies, 2)
            add_big_planes(big_planes, enemies, 1)
            #增加敌机飞行速度
            inc_speed(small_planes, 1)
        elif level == 2 and your_score > 300000:
            level = 3
            #增加5加小型敌机,3架中型敌机,2加大型敌机
            add_small_planes(small_planes, enemies, 5)
            add_middle_planes(middle_planes, enemies, 3)
            add_big_planes(big_planes, enemies, 2)
            #增加敌机飞行速度
            inc_speed(small_planes, 1)
            inc_speed(middle_planes, 1)
        elif level == 3 and your_score > 600000:
            level = 4
            #增加5加小型敌机,3架中型敌机,2加大型敌机
            add_small_planes(small_planes, enemies, 5)
            add_middle_planes(middle_planes, enemies, 3)
            add_big_planes(big_planes, enemies, 2)
            #增加敌机飞行速度
            inc_speed(small_planes, 1)
            inc_speed(middle_planes, 1)
            inc_speed(big_planes, 1)
        elif level == 4 and your_score > 1000000:
            level = 5
            #增加5加小型敌机,3架中型敌机,2加大型敌机
            add_small_planes(small_planes, enemies, 5)
            add_middle_planes(middle_planes, enemies, 3)
            add_big_planes(big_planes, enemies, 2)
            #增加敌机飞行速度
            inc_speed(small_planes, 1)
            inc_speed(middle_planes, 1)
            inc_speed(big_planes, 1)

        #游戏主流程
        screen.blit(background, (0, 0))#画背景
        if life_num > 0 and not paused:
            #检测玩家键盘操作控制我方飞机方向
            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_UP]:
                me.moveUp()
            if key_pressed[K_DOWN]:
                me.moveDown()
            if key_pressed[K_LEFT]:
                me.moveLeft()
            if key_pressed[K_RIGHT]:
                me.moveRight()

            #若补给包存在则绘制补给包并检测玩家是否获得
            if bomb_supply.is_alive:
                bomb_supply.move()
                screen.blit(bomb_supply.image, bomb_supply.rect)
                if pygame.sprite.collide_mask(bomb_supply, me):
                    get_bomb_sound.play()
                    if bomb_num < 3: #全屏炸弹数量上限为3
                        bomb_num += 1
                    bomb_supply.is_alive = False
            if bullet_supply.is_alive:
                bullet_supply.move()
                screen.blit(bullet_supply.image, bullet_supply.rect)
                if pygame.sprite.collide_mask(bullet_supply, me):
                    get_bullet_sound.play()
                    bullet_supply.is_alive = False
                    is_supper_bullet = True
                    pygame.time.set_timer(SUPPER_BULLET_TIME, 18 * 1000) #设定18秒后关闭
            
            #发射子弹
            if not is_supper_bullet:
                if not(delay % 10):
                    bullet1[bullet1_index].reset(me.rect.midtop)
                    bullet1_sound.play()
                    bullet1_index = (bullet1_index + 1) % BULLET1_NUM
            else:
                if not(delay % 10):
                    bullet2[bullet2_index].reset(me.rect.midtop)
                    bullet2_sound.play()
                    bullet2_index = (bullet2_index + 1) % BULLET2_NUM
            #绘制子弹并检测子弹是否击中敌机
            for b in (bullet2 if is_supper_bullet else bullet1):
                if b.is_alive:
                    b.move()
                    screen.blit(b.image, b.rect)
                    enemies_hit = \
                                pygame.sprite.spritecollide(b, enemies, False, \
                                                            pygame.sprite.collide_mask)
                    if enemies_hit:
                        b.is_alive = False
                        for e in enemies_hit:
                            e.energy -= (2 if is_supper_bullet else 1) #普通子弹伤害为1,超级子弹为2
                            if e.energy <= 0:
                                e.is_alive = False

            #绘制大型敌机
            for each in big_planes:
                each.move()
                if each.is_alive:
                    if switch_image:#切换两张图片实现闪烁效果,BOSS特效
                        screen.blit(each.image1, each.rect)
                    else:
                        screen.blit(each.image2, each.rect)
                    #绘制血槽
                    pygame.draw.line(screen, BLACK, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.right, each.rect.top - 5), \
                                     2)
                    #血量 > 20%时显示绿色,否则红色
                    energy_remain = each.energy * 1.0 / enemy.BigPlane.energy
                    if energy_remain > 0.2:
                        energy_color = GREEN
                    else:
                        energy_color = RED
                    pygame.draw.line(screen, energy_color, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.left + each.rect.width * energy_remain, \
                                      each.rect.top - 5), 2)
                    #即将出现在画面中时播放音效
                    if each.rect.bottom >= -50:
                        bigPlane_out_sound.play(-1)#循环播放确保BOSS飞行中带有音效
                else: #毁灭
                    screen.blit(each.image1, each.rect)
                    if not(delay % 3):
                        if big_plane_destroy == 0: #确保只播放一次音效
                            bigPlane_down_sound.play()
                        screen.blit(each.destroy_image, each.rect)
                        big_plane_destroy = (big_plane_destroy + 1) % 6
                        if big_plane_destroy == 0:
                            bigPlane_out_sound.stop()#停止BOSS飞行音效
                            each.reset()
                            your_score += 10000
                    
            #绘制中型敌机
            for each in middle_planes:
                each.move()
                screen.blit(each.image, each.rect)
                #绘制血槽
                pygame.draw.line(screen, BLACK, \
                                (each.rect.left, each.rect.top - 5), \
                                (each.rect.right, each.rect.top - 5), \
                                2)
                #血量 > 20%时显示绿色,否则红色
                energy_remain = each.energy * 1.0 / enemy.MiddlePlane.energy
                if energy_remain > 0.2:
                    energy_color = GREEN
                else:
                    energy_color = RED
                pygame.draw.line(screen, energy_color, \
                                (each.rect.left, each.rect.top - 5), \
                                (each.rect.left + each.rect.width * energy_remain, \
                                each.rect.top - 5), 2)
                
                if not each.is_alive:#毁灭
                    if not(delay % 3):
                        if middle_plane_destroy == 0:
                            middlePlane_down_sound.play()
                        screen.blit(each.destroy_image, each.rect)
                        middle_plane_destroy = (middle_plane_destroy + 1) % 4
                        if middle_plane_destroy == 0:
                            each.reset()
                            your_score += 5000
                        
            #绘制小型敌机
            for each in small_planes:
                each.move()
                screen.blit(each.image, each.rect)
                if not each.is_alive:#毁灭
                    if not(delay % 3):
                        if small_plane_destroy == 0:
                            smallPlane_down_sound.play()
                        screen.blit(each.destroy_image, each.rect)
                        small_plane_destroy = (small_plane_destroy + 1) % 4
                        if small_plane_destroy == 0:
                            each.reset()
                            your_score += 1000

            #检测我方飞机是否被撞
            enemies_down = pygame.sprite.spritecollide(me, \
                                                       enemies, \
                                                       False, \
                                                       pygame.sprite.collide_mask)
            #若我方飞机在非无敌状态被撞,双方飞机都阵亡
            if enemies_down and not me.invincible:
                me.is_alive = False
                for each in enemies_down:
                    each.is_alive = False

            #绘制我方飞机
            if me.is_alive:
                if switch_image:
                    screen.blit(me.image1, me.rect)
                else:
                    screen.blit(me.image2, me.rect)
            else:#我方飞机毁灭
                screen.blit(me.image1, me.rect)
                if not(delay % 3):
                    if my_plane_destroy == 0:
                        myPlane_down_sound.play()
                    screen.blit(me.destroy_image, me.rect)
                    my_plane_destroy = (my_plane_destroy + 1) % 4
                    if my_plane_destroy == 0:
                        life_num -= 1
                        if life_num > 0:
                            me.reset()
                            pygame.time.set_timer(MY_PLANE_INVINCIBLE_TIME, 3000) #3秒后关闭无敌状态
        #游戏暂停
        elif paused:
            screen.blit(pause_image, pause_rect)

        if life_num > 0:#游戏正常运行
            #画出分数和难度级别
            score_text = font.render(\
                'Score: %s Level: %s' % (str(your_score), str(level)), True, WHITE)
            screen.blit(score_text, (10, 5))
            #绘制状态栏,全屏炸弹图标+数量,生命图标+数量
            screen.blit(bomb_image, bomb_rect)
            bomb_text = font.render(' X %s' % str(bomb_num), True, WHITE)
            screen.blit(bomb_text, (bomb_rect.left + bomb_rect.width, \
                                    bomb_rect.top + 20))
            screen.blit(life_image, life_rect)
            life_text = font.render(' X %s' % str(life_num), True, WHITE)
            screen.blit(life_text, (life_rect.left + life_rect.width, \
                                    life_rect.top + 20))

        if life_num == 0:#游戏结束
            #绘制gameover画面
            game_over = game_over_font.render('GAME OVER', True, WHITE)
            screen.blit(game_over, ((bg_width - game_over.get_width()) // 2, \
                                    (bg_height - game_over.get_height()) // 2))
            #关闭其他资源
            pygame.mixer.music.stop()
            pygame.time.set_timer(SUPPLY_TIME, 0)
            #记录历史最高分
            if not recorded:#确保只进行一次文件操作
                with open('record.data', 'r') as f:
                    temp = f.read()
                    if temp != '':
                        best_score = int(temp)
                if best_score < your_score:
                    best_score = your_score
                    with open('record.data', 'w') as f:
                        f.write(str(best_score))
                recorded = True
                pygame.mixer.stop() #停止其他音效
                game_over_sound.play() #顺便防止重复播放结束音乐
                #防止重复加载图片
                background = pygame.image.load('image/gameover_background.jpg').convert()
            #绘制分数,最高分和当前分数
            best_score_text = font.render('Best Score: %s' % str(best_score), True, WHITE)
            screen.blit(best_score_text, (40, 50))
            your_score_text = font.render('Your Score: %s' % str(your_score), True, WHITE)
            screen.blit(your_score_text, ((bg_width - your_score_text.get_width()) // 2, \
                                          (bg_height + game_over.get_height()) // 2 + 30))
            #绘制选择项
            screen.blit(quit_text, quit_pos)
            screen.blit(again_text, again_pos)
            #鼠标停在Quit上时字体变为红色,否则为白色
            #鼠标停在Again上时字体变为绿色,否则为白色
            mouse_pos = pygame.mouse.get_pos()
            #Quit部分
            if mouse_pos[0] > quit_pos.left and \
               mouse_pos[0] < quit_pos.right and \
               mouse_pos[1] > quit_pos.top and \
               mouse_pos[1] < quit_pos.bottom:
                quit_text = font.render('Quit', True, RED)
            else:
                quit_text = font.render('Quit', True, WHITE)

            if mouse_pos[0] > again_pos.left and \
               mouse_pos[0] < again_pos.right and \
               mouse_pos[1] > again_pos.top and \
               mouse_pos[1] < again_pos.bottom:
                again_text = font.render('Again', True, GREEN)
            else:
                again_text = font.render('Again', True, WHITE)
        #每隔5帧切换图片
        if not(delay % 5):
            switch_image = not switch_image 

        delay -= 1
        if not delay:
            delay = 100
        #显示游戏画面
        pygame.display.flip()
        #设置帧率
        clock.tick(60)
Exemplo n.º 2
0
def main():
    is_double_bullet = False
    pygame.mixer_music.play(-1)

    # create myPlane
    my_plane = myPlane.MyPlane(bg_size)

    # create enemy plane
    enemies = pygame.sprite.Group()

    #  小型敌方飞机
    smallEnemies = pygame.sprite.Group()
    addSmallEnemies(smallEnemies, enemies, data.BigEnemiesNum)
    #  中型敌方飞机
    midEnemies = pygame.sprite.Group()
    addMidEnemies(midEnemies, enemies, data.MidEnemiesNum)
    #  大型敌方飞机
    bigEnemies = pygame.sprite.Group()
    addBigEnemies(bigEnemies, enemies, data.BigEnemiesNum)

    bullets = []
    # 我方子弹 普通
    if True:
        bullet1 = []
        bullet1_index = 0
        bullet1_num = data.SingleBullet
        for i in range(bullet1_num):
            bullet1.append(bullet.Bullet1(my_plane.rect.midtop))

    # 我方子弹 二重
    if True:
        bullet2 = []
        bullet2_index = 0
        bullet2_num = data.DoubleBullet
        for i in range(bullet2_num // 2):
            bullet2.append(
                bullet.Bullet2(
                    (my_plane.rect.centerx - 33, my_plane.rect.centery)))
            bullet2.append(
                bullet.Bullet2(
                    (my_plane.rect.centerx + 30, my_plane.rect.centery)))
    # 二重子弹             定时器
    DOUBLE_BULLET_TIME = USEREVENT + 1

    # 飞机破坏图片索引
    if True:
        e1_destroy_index = 0
        e2_destroy_index = 0
        e3_destroy_index = 0
        me_destroy_index = 0

    # 统计得分
    score = 0
    score_font = pygame.font.Font("font/font.ttf", 36)

    # 暂停游戏
    if True:
        paused = False
        pause_nor_image = pygame.image.load(
            "./images/pause_nor.png").convert_alpha()
        pause_pressed_image = pygame.image.load(
            "./images/pause_pressed.png").convert_alpha()
        resume_nor_image = pygame.image.load(
            "./images/resume_nor.png").convert_alpha()
        resume_pressed_image = pygame.image.load(
            "./images/resume_pressed.png").convert_alpha()
        pause_rect = pause_nor_image.get_rect()
        pause_rect.left, pause_rect.top = width - pause_rect.width - 10, 10
        pause_image = pause_nor_image

    # 游戏难度
    level = data.GameLevel

    # 全屏炸弹
    if True:
        bomb_image = pygame.image.load("./images/bomb.png").convert_alpha()
        bomb_rect = bomb_image.get_rect()
        bomb_font = pygame.font.Font("font/font.ttf", 48)
        bomb_num = data.BombNum

    # 每20s触发一次补给
    if True:
        bullet_supply = supply.BulletSupply(bg_size)
        bomb_supply = supply.BombSupply(bg_size)
        SUPPLY_TIME = USEREVENT
        pygame.time.set_timer(SUPPLY_TIME, data.SupplyIntervelTime)

    clock = pygame.time.Clock()

    # 切换我放飞机
    switch_iamge = True

    # 生命数量
    if True:
        life_image = pygame.image.load("./images/life.png").convert_alpha()
        life_rect = life_image.get_rect()
        life_num = data.Life

    # 解除我方复活后的短暂无敌 计时器
    invincible_time = USEREVENT + 2

    # 绘制结束界面
    if True:
        gameover_font = pygame.font.Font("font/font.ttf", 48)
        again_image = pygame.image.load("./images/again.png").convert_alpha()
        again_rect = again_image.get_rect()
        gameover_image = pygame.image.load(
            "./images/gameover.png").convert_alpha()
        gameover_rect = gameover_image.get_rect()

    # 用于阻止重复开关存档文件 save
    saved = False

    # 用于延迟
    delay = 100

    running = True

    while running:
        # 所有事件event
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame, exit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1 and pause_rect.collidepoint(event.pos):
                    paused = not paused
                    if paused:
                        pause_image = resume_pressed_image
                        pygame.time.set_timer(SUPPLY_TIME, 0)
                        pygame.mixer.music.pause()
                        pygame.mixer.pause()
                    else:
                        pause_image = pause_pressed_image
                        pygame.time.set_timer(SUPPLY_TIME,
                                              data.SupplyIntervelTime)
                        pygame.mixer.music.unpause()
                        pygame.mixer.unpause()

            # elif event.type ==MOUSEMOTION:
            #     if pause_rect.collidepoint(event.pos):
            #         if paused:
            #             pause_image = resume_pressed_image
            #         else:
            #             pause_image = pause_pressed_image
            #     else:
            #         if paused:
            #             pause_image = resume_nor_image
            #         else:
            #             pause_image = pause_nor_image

            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    print("按之前还有%d个" % bomb_num)
                    if bomb_num:
                        bomb_num -= 1
                        bomb_sound.play()
                        for each in enemies:
                            if each.rect.bottom > 0:
                                each.alive = False

            elif event.type == SUPPLY_TIME:
                supply_sound.play()
                if random.randint(1, 4) % 4 == 0:
                    bomb_supply.reset()
                else:
                    bullet_supply.reset()

            elif event.type == DOUBLE_BULLET_TIME:
                is_double_bullet = False
                pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

            elif event.type == invincible_time:
                my_plane.invincible = False
                pygame.time.set_timer(invincible_time, 0)

        # 根据用户的得分来增加难度
        if score >= 5000 * (level**2):
            level += 1
            upgrade_sound.play()
            addSmallEnemies(smallEnemies, enemies, 5)
            addMidEnemies(midEnemies, enemies, 3)
            addBigEnemies(bigEnemies, enemies, 1)
            inc_speed(smallEnemies, 1)
            inc_speed(midEnemies, 0.5)
            # inc_speed(bigEnemies, 0.1)
            print("现在的等级是%d级" % level)

        screen.blit(background, (0, 0))

        # 游戏进行时相关 !important
        # 游戏进行时相关 !important
        # 游戏进行时相关 !important
        if life_num and not paused:
            # check keyboard event
            key_pressed = pygame.key.get_pressed()

            if key_pressed[K_w] or key_pressed[K_UP]:
                my_plane.moveUp()

            if key_pressed[K_s] or key_pressed[K_DOWN]:
                my_plane.moveDown()

            if key_pressed[K_a] or key_pressed[K_LEFT]:
                my_plane.moveLeft()

            if key_pressed[K_d] or key_pressed[K_RIGHT]:
                my_plane.moveRight()

            # 绘制大型敌机
            for each in bigEnemies:
                if each.alive:
                    each.move()
                    if each.hit:
                        screen.blit(each.image_hit, each.rect)
                        each.hit = False
                    else:
                        if switch_iamge:
                            screen.blit(each.image1, each.rect)
                        else:
                            screen.blit(each.image2, each.rect)

                    # 绘制血槽
                    pygame.draw.line(screen, BLACK,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5), 2)
                    # 生命大于20%时显示绿色,以下则为红色
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = GREEN
                    else:
                        energy_color = RED
                    pygame.draw.line(
                        screen, energy_color,
                        (each.rect.left, each.rect.top - 5),
                        (each.rect.left + each.rect.width * energy_remain,
                         each.rect.top - 5), 2)

                    # 即将出现时,播ngm
                    if each.rect.bottom == -50:

                        enemy3_fly_sound.play(-1)

                        # -1循环播放

                else:
                    # 飞机被破坏
                    if not (delay % 5):
                        if e3_destroy_index == 0:
                            enemy3_down_sound.play()

                        screen.blit(each.destroy_images[e3_destroy_index],
                                    each.rect)
                        e3_destroy_index = (e3_destroy_index + 1) % 6
                        if e3_destroy_index == 0:
                            enemy3_fly_sound.stop()
                            score += 5000
                            each.reset()

            # 绘制中型敌机
            for each in midEnemies:
                if each.alive:
                    each.move()
                    if each.hit:
                        screen.blit(each.image_hit, each.rect)
                        each.hit = False
                    else:
                        screen.blit(each.image, each.rect)

                    # 绘制血槽
                    pygame.draw.line(screen, BLACK,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5), 2)
                    # 生命大于20%时显示绿色,以下则为红色
                    energy_remain = each.energy / enemy.MidEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = GREEN
                    else:
                        energy_color = RED
                    pygame.draw.line(
                        screen, energy_color,
                        (each.rect.left, each.rect.top - 5),
                        (each.rect.left + each.rect.width * energy_remain,
                         each.rect.top - 5), 2)

                else:
                    # 飞机被破坏
                    if not (delay % 5):
                        if e2_destroy_index == 0:

                            enemy2_down_sound.play()
                        screen.blit(each.destroy_images[e2_destroy_index],
                                    each.rect)
                        e2_destroy_index = (e2_destroy_index + 1) % 4
                        if e2_destroy_index == 0:
                            score += 1000
                            each.reset()

            # 绘制小型敌机
            for each in smallEnemies:
                if each.alive:
                    each.move()
                    screen.blit(each.image, each.rect)
                else:
                    # 飞机被破坏
                    if not (delay % 5):
                        if e1_destroy_index == 0:
                            enemy1_down_sound.play()
                        screen.blit(each.destroy_images[e1_destroy_index],
                                    each.rect)
                        e1_destroy_index = (e1_destroy_index + 1) % 4
                        if e1_destroy_index == 0:
                            score += 100
                            each.reset()

            # 绘制我放飞机
            if my_plane.alive:
                if switch_iamge:
                    screen.blit(my_plane.image1, my_plane.rect)
                    # print(my_plane.rect)
                    # switch_iamge = False
                else:
                    screen.blit(my_plane.image2, my_plane.rect)
                    # switch_iamge = True
            else:
                # 我方飞机被破坏
                if not (delay % 3):
                    if me_destroy_index == 0:

                        me_down_sound.play()
                    screen.blit(my_plane.destroy_images[me_destroy_index],
                                my_plane.rect)
                    e1_destroy_index = (e1_destroy_index + 1) % 4
                    if e1_destroy_index == 0:
                        life_num -= 1
                        # 重生
                        my_plane.reset()
                        pygame.time.set_timer(invincible_time, 3 * 1000)

            # 检测我方飞机是否被撞
            enemies_down = pygame.sprite.spritecollide(
                my_plane, enemies, False, pygame.sprite.collide_mask)
            if enemies_down and not my_plane.invincible:
                # 这面这个控制我方无敌
                my_plane.alive = False
                for e in enemies_down:
                    e.alive = False

            # 发射我方子弹
            if not (delay % 10):
                bullet_sound.play()
                if is_double_bullet:
                    bullets = bullet2
                    bullets[bullet2_index].reset(
                        (my_plane.rect.centerx - 33, my_plane.rect.centery))
                    bullets[bullet2_index + 1].reset(
                        (my_plane.rect.centerx + 30, my_plane.rect.centery))
                    bullet2_index = (bullet2_index + 2) % bullet2_num
                else:
                    bullets = bullet1
                    bullets[bullet1_index].reset(my_plane.rect.midtop)
                    bullet1_index = (bullet1_index + 1) % bullet1_num

            # 检测子弹是否击中敌机
            for b in bullets:
                if b.alive:
                    b.move()
                    screen.blit(b.image, b.rect)

                    enemy_hit = pygame.sprite.spritecollide(
                        b, enemies, False, pygame.sprite.collide_mask)
                    if enemy_hit:
                        bullet.alive = False
                        for e in enemy_hit:
                            # 下面用来判断大中小敌机的不同情况
                            if e in midEnemies or e in bigEnemies:
                                e.hit = True
                                e.energy -= 1
                                if e.energy == 0:
                                    e.alive = False
                            else:
                                e.alive = False

            # 绘制补给炸弹并判断是否获得
            if bomb_supply.alive:
                bomb_supply.move()
                screen.blit(bomb_supply.image, bomb_supply.rect)
                if pygame.sprite.collide_mask(bomb_supply, my_plane):
                    get_bomb_sound.play()
                    bomb_num += 1
                    bomb_supply.alive = False

            # 绘制超级子弹并判断是否获得
            if bullet_supply.alive:
                bullet_supply.move()
                screen.blit(bullet_supply.image, bullet_supply.rect)
                if pygame.sprite.collide_mask(bullet_supply, my_plane):
                    get_bullet_sound.play()
                    # 发射二重子弹
                    is_double_bullet = True
                    pygame.time.set_timer(DOUBLE_BULLET_TIME,
                                          data.DoubleBulletTime)
                    bullet_supply.alive = False

            # 绘制剩余生命life数量
            if True:
                if life_num:
                    for i in range(life_num):
                        screen.blit(life_image,
                                    (width - 10 - (i + 1) * life_rect.width,
                                     height - 10 - life_rect.height))

            # 绘制剩余炸弹
            if True:
                bomb_text = bomb_font.render("× %d" % bomb_num, True, WHITE)
                text_rect = bomb_text.get_rect()
                screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
                screen.blit(
                    bomb_text,
                    (20 + bomb_rect.width, height - 5 - text_rect.height))

            # 绘制分数栏
            score_text = score_font.render(
                "LV:%d   Score : %s " % (level, str(score)), True, WHITE)
            screen.blit(score_text, (10, 5))

            # 绘制暂停按钮
            screen.blit(pause_image, pause_rect)

        elif life_num:
            # 绘制分数栏
            score_text = score_font.render(
                "LV:%d   Score : %s " % (level, str(score)), True, WHITE)
            screen.blit(score_text, (10, 5))

            # 绘制暂停按钮
            screen.blit(pause_image, pause_rect)

        # 绘制游戏结束画面
        elif life_num == 0:
            # 背景音乐停止
            pygame.mixer.music.stop()

            # 停止全部音效
            pygame.mixer.stop()

            # 停止补给发放
            pygame.time.set_timer(SUPPLY_TIME, 0)

            # 存档相关
            if not saved:
                # 读取历史最高分
                with open("save.txt", "r") as f:
                    record_score = int(f.read())
                # 如果高于最高分,则存档
                if score > record_score:
                    with open("save.txt", "w") as f:
                        f.write(str(score))

            # 结束界面
            if True:
                record_score_text = score_font.render(
                    "Best : %d" % record_score, True, WHITE)
                screen.blit(record_score_text, (50, 50))

                gameover_text1 = gameover_font.render("Your Score: ", True,
                                                      WHITE)
                gameover_text1_rect = gameover_text1.get_rect()
                gameover_text1_rect.left, gameover_text1_rect.top = \
                    (width - gameover_text1_rect.width) // 2, height // 2
                screen.blit(gameover_text1, gameover_text1_rect)

                gameover_text2 = gameover_font.render(str(score), True, WHITE)
                gameover_text2_rect = gameover_text2.get_rect()
                gameover_text2_rect.left, gameover_text2_rect.top = \
                    (width - gameover_text2_rect.width) // 2, \
                    gameover_text1_rect.bottom + 10
                screen.blit(gameover_text2, gameover_text2_rect)

                again_rect.left, again_rect.top = \
                    (width - again_rect.width) // 2, \
                    gameover_text2_rect.bottom + 50
                screen.blit(again_image, again_rect)

                gameover_rect.left, gameover_rect.top = \
                    (width - again_rect.width) // 2, \
                    again_rect.bottom + 10
                screen.blit(gameover_image, gameover_rect)

                score_text = score_font.render("Score : %s " % str(score),
                                               True, WHITE)
                screen.blit(score_text, (10, 5))

            # 检测用户的鼠标操作
            # 如果用户按下鼠标左键
            if pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()
                if again_rect.left < pos[0] < again_rect.right and \
                        again_rect.top < pos[1] < again_rect.bottom:
                    main()
                elif gameover_rect.left < pos[0] < gameover_rect.right and \
                        gameover_rect.top < pos[1] < gameover_rect.bottom:
                    pygame.quit()
                    sys.exit()

        # mark是否使用超级子弹

        # 切换照片
        if True:
            if not (delay % 5):
                switch_iamge = not switch_iamge
            delay -= 1
            if not delay:
                delay = 100

            pygame.display.flip()
            clock.tick(60)
Exemplo n.º 3
0
def main():
    clock = pygame.time.Clock()

    #用于切换图片
    switchImage = True

    #用于延迟图片切换
    delay = 100

    #玩家分数
    score = 0
    score_font = pygame.font.Font("myfount.TTF", 36)

    #初始游戏级别
    level = 1

    #开始播放音频资源(-1表示循环播放)
    pygame.mixer.music.play(-1)
    runing = True

    #创建我方英雄飞机对象
    me = myPlane.MyPlane(bg_size)

    #生成普通子弹
    bullet1 = []
    bullet1_index = 0
    bullet1_num = 4
    for i in range(bullet1_num):
        b = bullet.Bullet1(me.rect.midtop)
        bullet1.append(b)

    #生成高级子弹
    bullet2 = []

    #创建敌人飞机容器
    enemies = pygame.sprite.Group()
    #生成敌方小型飞机
    small_enemies = pygame.sprite.Group()
    add_small_enemies(enemies, small_enemies, 15)
    #生成敌方中型飞机
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(enemies, mid_enemies, 4)
    #生成敌方大型飞机
    big_enemies = pygame.sprite.Group()
    add_big_enemies(enemies, big_enemies, 2)

    #中弹图片索引
    e1_destroy_index = 0
    e2_destroy_index = 0
    e3_destroy_index = 0
    me_destroy_index = 0

    while runing:

        #根据用户得分情况改变游戏级别
        if level == 1 and score > 100:
            level = 2
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)
        elif level == 2 and score > 200:
            level = 3
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)
        elif level == 3 and score > 500:
            level = 4
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)
        elif level == 4 and score > 1000:
            level = 5
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)

        #关闭程序
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("正在关闭程序")
                pygame.quit()
                sys.exit()

        #检测用户的键盘操作
        key_pressed = pygame.key.get_pressed()

        if key_pressed[K_w] or key_pressed[K_UP]:
            me.moveUp()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            me.moveDown()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            me.moveLeft()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            me.moveRight()

        screen.blit(background, (0, 0))

        # 发射子弹
        if not (delay % 10):
            bullet1[bullet1_index].reset(me.rect.midtop)
            bullet1_index = (bullet1_index + 1) % bullet1_num

        #检测子弹是否击中飞机
        for b in bullet1:
            if b.active:
                b.move()
                screen.blit(b.image, b.rect)
                enem_hit = pygame.sprite.spritecollide(
                    b, enemies, False, pygame.sprite.collide_mask)
                if enem_hit:
                    b.active = False
                    for e in enem_hit:
                        # if e.hp <= 0
                        #     e.active = False
                        # else:
                        #     e.hp -= 1

                        if e in mid_enemies or e in big_enemies:
                            e.hp -= 1
                            # print("掉血")
                            e.hit = True
                            if e.hp == 0:
                                e.active = False
                                score += e.score
                                # print(score)
                        else:
                            e.active = False
                            score += e.score
                            # print(score)

        #绘制小飞机
        for each in small_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else:
                if not (delay % 4):
                    screen.blit(each.destroy_images[e1_destroy_index],
                                each.rect)
                    e1_destroy_index = (e1_destroy_index + 1) % 4
                    if e1_destroy_index == 0:
                        each.reset()

        #绘制中飞机
        for each in mid_enemies:
            if each.active:
                each.move()
                if each.hit:
                    screen.blit(each.image_hit, each.rect)
                else:
                    screen.blit(each.image, each.rect)
                #绘制血槽
                pygame.draw.line(screen, BLACK,
                                 (each.rect.left, each.rect.top - 10),
                                 (each.rect.right, each.rect.top - 10), 2)
                #当血量大于20%时显示绿色,否则红色
                enemy_remain = each.hp / enemy.MidEnemy.hp
                if enemy_remain > 0.2:
                    yanse = GREEN
                else:
                    yanse = RED
                pygame.draw.line(
                    screen, yanse, (each.rect.left, each.rect.top - 10),
                    (each.rect.left + each.rect.width * enemy_remain,
                     each.rect.top - 10), 2)
            else:
                if not (delay % 4):
                    screen.blit(each.destroy_images[e2_destroy_index],
                                each.rect)
                    e2_destroy_index = (e2_destroy_index + 1) % 4
                    if e2_destroy_index == 0:
                        each.reset()

        #绘制大飞机
        for each in big_enemies:
            if each.active:
                each.move()
                if each.hit:
                    screen.blit(each.image_hit, each.rect)
                else:
                    if switchImage:
                        screen.blit(each.image1, each.rect)
                    else:
                        screen.blit(each.image2, each.rect)

                #绘制血槽
                pygame.draw.line(screen, BLACK,
                                 (each.rect.left, each.rect.top - 10),
                                 (each.rect.right, each.rect.top - 10), 2)
                #当血量大于20%时显示绿色,否则红色
                enemy_remain = each.hp / enemy.BigEnemy.hp
                if enemy_remain > 0.2:
                    yanse = GREEN
                else:
                    yanse = RED

                pygame.draw.line(
                    screen, yanse, (each.rect.left, each.rect.top - 10),
                    (each.rect.left + each.rect.width * enemy_remain,
                     each.rect.top - 10), 2)

                if each.rect.bottom > -50:
                    enemy3_out.play()
            else:
                #毁灭
                if not (delay % 4):
                    if e3_destroy_index == 0:
                        enemy3_down.play()
                    screen.blit(each.destroy_images[e3_destroy_index],
                                each.rect)
                    e3_destroy_index = (e3_destroy_index + 1) % 7
                    if e3_destroy_index == 0:
                        enemy3_down.stop()
                        each.reset()

        #将我方飞机绘制到屏幕中
        # screen.blit(me.image1, me.rect)

        #飞机碰撞检测
        # enemies_down = pygame.sprite.spritecollide(me, enemies, False)
        enemies_down = pygame.sprite.spritecollide(me, enemies, False,
                                                   pygame.sprite.collide_mask)
        if enemies_down:
            me.active = False
            for e in enemies_down:
                e.active = False

        #绘制英雄机
        if me.active:
            if switchImage:
                screen.blit(me.image1, me.rect)
            else:
                screen.blit(me.image2, me.rect)
        else:
            if not (delay % 10):
                screen.blit(me.destroy_images[me_destroy_index], me.rect)
                me_destroy_index = (me_destroy_index + 1) % 4
                if me_destroy_index == 0:
                    me.reset()
                    # runing = False
                    print(score)

        # 显示分数
        score_text = score_font.render("分数: %s" % str(score), True, RED)
        screen.blit(score_text, (0, 0))

        # 显示级别
        score_text = score_font.render("级别: %s" % str(level), True, RED)
        screen.blit(score_text, (width - 150, 0))

        #切换图片
        if not (delay % 10):
            switchImage = not switchImage

        delay -= 1
        if not delay:
            delay = 100
        pygame.display.flip()

        clock.tick(100)
Exemplo n.º 4
0
def main():
    clock = pygame.time.Clock()
    # pygame.mixer.music.play(-1);

    #每10秒发放一个补给包
    bullet_supply = supply.Bullet_Stupply(bg_size)
    bomb_supple = supply.Bomb_Stupply(bg_size)
    supply_time = USEREVENT
    pygame.time.set_timer(supply_time, 10 * 1000)

    #超级子弹的定时器
    double_bullet_time = USEREVENT + 1
    #标志你的飞机是否在使用超级子弹
    is_double_bullet = False

    #用于切换图片
    switchImage = True

    #用于延迟
    delay = 100

    #分数
    score = 0
    score_font = pygame.font.Font("AaxiaoNangua.ttf", 36)

    #全屏炸弹
    bomb_image = pygame.image.load("gameArts/bomb.png").convert_alpha()
    bomb_rect = bomb_image.get_rect()
    bomb_font = pygame.font.Font("AaxiaoNangua.ttf", 36)
    bomb_num = 3

    #设置游戏难度级别
    level = 1

    running = True

    # 创建英雄飞机
    me = myPlane.MyPlane(bg_size)
    enemies = pygame.sprite.Group()

    # 生成普通子弹
    bullet1 = []
    bullet1_index = 0
    bullet1_num = 4

    for i in range(bullet1_num):
        bullet1.append(bullet.Bullet1(me.rect.midtop))

    # 生成超级子弹
    bullet2 = []
    bullet2_index = 0
    bullet2_num = 8

    for i in range(bullet2_num):
        bullet2.append(bullet.Bullet2((me.rect.left + 8, me.rect.centery)))

    #生成敌方小型飞机
    small_enemies = pygame.sprite.Group()
    add_small_enemies(enemies, small_enemies, 15)

    #生成敌方中型飞机
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(enemies, mid_enemies, 4)

    #生成敌方大型飞机
    big_enemies = pygame.sprite.Group()
    add_big_enemies(enemies, big_enemies, 2)

    #中弹图片索引
    e1_destroy_index = 0
    e2_destroy_index = 0
    e3_destroy_index = 0
    me_destroy_index = 0

    # 游戏运行
    while running:

        # 根据用户的得分情况,改变游戏级别
        if level == 1 and score > 100:
            level = 2
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)
        elif level == 2 and score > 200:
            level = 3
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)
        elif level == 3 and score > 500:
            level = 4
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            increseEnemySpeed(enemies)

        # 事件队列
        for event in pygame.event.get():
            # 退出事件
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == double_bullet_time:
                is_double_bullet = False
                pygame.time.set_timer(double_bullet_time, 0)

            if event.type == supply_time:
                if randint(0, 11) % 2:
                    bomb_supple.reset()
                else:
                    bullet_supply.reset()

            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if bomb_num:
                        bomb_num -= 1
                        for each in enemies:
                            if each.rect.bottom > 0:
                                each.active = False

        # 检测用户的键盘操作
        key_pressed = pygame.key.get_pressed()
        if key_pressed[K_w] or key_pressed[pygame.K_UP]:
            me.moveUp()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            me.moveDown()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            me.moveLeft()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            me.moveRight()

        # 渲染游戏对象
        screen.blit(background, (0, 0))

        # 绘制全屏炸弹 并检测有没有被me获得
        if bomb_supple.active:
            bomb_supple.move()
            screen.blit(bomb_supple.image, bomb_supple.rect)
            if pygame.sprite.collide_mask(bomb_supple, me):
                bomb_num = 3
                bomb_supple.active = False

        #绘制超级子弹

        if bullet_supply.active:
            bullet_supply.move()
            screen.blit(bullet_supply.image, bullet_supply.rect)
            if pygame.sprite.collide_mask(bullet_supply, me):
                bullet_supply.active = False
                is_double_bullet = True
                pygame.time.set_timer(double_bullet_time, 10 * 1000)

        # 开始发射子弹
        if not delay % 10:
            if is_double_bullet:
                bullets = bullet2
                bullets[bullet2_index].reset(
                    (me.rect.left + 8, me.rect.centery))
                bullet2_index = (bullet2_index + 1) % bullet2_num
            else:
                bullets = bullet1
                bullet1[bullet1_index].reset(me.rect.midtop)
                bullet1_index = (bullet1_index + 1) % bullet1_num

        # 检测子弹是否击中敌人飞机
        for b in bullets:
            if b.active:
                b.move()
                screen.blit(b.image, b.rect)
                enemy_hit = pygame.sprite.spritecollide(
                    b, enemies, False, pygame.sprite.collide_mask)
                if enemy_hit:
                    b.active = False
                    for e in enemy_hit:
                        if e in mid_enemies or e in big_enemies:
                            e.hp -= 1
                            e.hit = True

                            if e.hp == 0:
                                e.active = False

                        else:
                            e.active = False

        # 绘制大型飞机
        for each in big_enemies:
            if each.active:
                each.move()
                if each.hit:
                    screen.blit(each.image_hit, each.rect)
                    each.hit = False
                else:
                    if switchImage:
                        screen.blit(each.image1, each.rect)
                    else:
                        screen.blit(each.image2, each.rect)

                # 绘制血槽
                pygame.draw.line(screen, BLACK,
                                 (each.rect.left, each.rect.top - 10),
                                 (each.rect.right, each.rect.top - 10), 2)
                # 当生命大于20%的时候显示绿色,否则红色
                enemy_remain = each.hp / enemy.BigEnemy.hp
                if enemy_remain > 0.2:
                    yanse = GREEN
                else:
                    yanse = RED

                pygame.draw.line(
                    screen, yanse, (each.rect.left, each.rect.top - 10),
                    (each.rect.left + each.rect.width * enemy_remain,
                     each.rect.top - 10), 2)

            else:
                #毁灭
                if not (delay % 4):
                    screen.blit(each.destroy_images[e3_destroy_index],
                                each.rect)
                    e3_destroy_index = (e3_destroy_index + 1) % 7
                    if e3_destroy_index == 0:
                        each.reset()

        # 绘制中型飞机
        for each in mid_enemies:

            if each.active:
                each.move()
                if each.hit:
                    screen.blit(each.image_hit, each.rect)
                    each.hit = False
                else:
                    screen.blit(each.image, each.rect)

                # 绘制血槽
                pygame.draw.line(screen, BLACK,
                                 (each.rect.left, each.rect.top - 10),
                                 (each.rect.right, each.rect.top - 10), 2)
                # 当生命大于20%的时候显示绿色,否则红色
                enemy_remain = each.hp / enemy.MidEnemy.hp
                if enemy_remain > 0.2:
                    yanse = GREEN
                else:
                    yanse = RED

                pygame.draw.line(
                    screen, yanse, (each.rect.left, each.rect.top - 10),
                    (each.rect.left + each.rect.width * enemy_remain,
                     each.rect.top - 10), 2)

            else:
                #毁灭
                if not (delay % 4):
                    screen.blit(each.destroy_images[e2_destroy_index],
                                each.rect)
                    e2_destroy_index = (e2_destroy_index + 1) % 4
                    if e2_destroy_index == 0:
                        each.reset()
        # 绘制小型飞机
        for each in small_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else:
                #毁灭
                if not (delay % 4):
                    screen.blit(each.destroy_images[e1_destroy_index],
                                each.rect)
                    e1_destroy_index = (e1_destroy_index + 1) % 4
                    if e1_destroy_index == 0:
                        score += 10
                        each.reset()

        # 检查是否被碰撞
        # enemies_down = pygame.sprite.spritecollide(me,enemies,False);
        enemies_down = pygame.sprite.spritecollide(me, enemies, False,
                                                   pygame.sprite.collide_mask)
        if enemies_down:
            me.active = False
            for e in enemies_down:
                e.active = False

        # 绘制我方飞机
        if me.active:
            if switchImage:
                screen.blit(me.image1, me.rect)
            else:
                screen.blit(me.image2, me.rect)
        else:
            #毁灭
            if not (delay % 4):
                screen.blit(me.destroy_images[me_destroy_index], each.rect)
                me_destroy_index = (me_destroy_index + 1) % 4
                if me_destroy_index == 0:
                    # me.reset();

                    #退出游戏
                    running = False

        #显示分数
        score_text = score_font.render("分数: %s" % str(score), True, RED)
        screen.blit(score_text, (0, 0))

        #绘制全屏炸弹
        bomb_text = bomb_font.render("x %d" % bomb_num, True, WHITE)
        text_rect = bomb_text.get_rect()
        screen.blit(bomb_image, (0, height - bomb_rect.height))
        screen.blit(bomb_text,
                    (5 + bomb_rect.width, height - text_rect.height))

        #切换图片

        if not (delay % 5):
            switchImage = not switchImage

        delay -= 1
        if not delay:
            delay = 100

        # 更新游戏对象到界面上
        pygame.display.flip()
        clock.tick(60)
Exemplo n.º 5
0
def main():
    clock = pygame.time.Clock()

    #用于切换图片
    switchImage = True

    #用于延迟图片切换
    delay = 100

    #开始播放音频资源(-1表示循环播放)
    pygame.mixer.music.play(-1)
    runing = True

    #创建我方英雄飞机对象
    me = myPlane.MyPlane(bg_size)

    #创建敌人飞机容器
    enemies = pygame.sprite.Group()
    #生成敌方小型飞机
    small_enemies = pygame.sprite.Group()
    add_small_enemies(enemies, small_enemies, 15)
    #生成敌方中型飞机
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(enemies, mid_enemies, 4)
    #生成敌方大型飞机
    big_enemies = pygame.sprite.Group()
    add_big_enemies(enemies, big_enemies, 2)

    #中弹图片索引
    e1_destroy_index = 0
    e2_destroy_index = 0
    e3_destroy_index = 0
    me_destroy_index = 0

    while runing:

        #关闭程序
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("正在关闭程序")
                pygame.quit()
                sys.exit()

        #检测用户的键盘操作
        key_pressed = pygame.key.get_pressed()

        if key_pressed[K_w] or key_pressed[K_UP]:
            me.moveUp()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            me.moveDown()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            me.moveLeft()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            me.moveRight()

        screen.blit(background, (0, 0))

        #绘制小飞机
        for each in small_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else:
                if not (delay % 4):
                    screen.blit(each.destroy_images[e1_destroy_index],
                                each.rect)
                    e1_destroy_index = (e1_destroy_index + 1) % 4
                    if e1_destroy_index == 0:
                        each.reset()

        #绘制中飞机
        for each in mid_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else:
                if not (delay % 4):
                    screen.blit(each.destroy_images[e2_destroy_index],
                                each.rect)
                    e2_destroy_index = (e2_destroy_index + 1) % 4
                    if e2_destroy_index == 0:
                        each.reset()

        #绘制大飞机
        for each in big_enemies:
            if each.active:
                each.move()
                if switchImage:
                    screen.blit(each.image1, each.rect)
                else:
                    screen.blit(each.image2, each.rect)

                if each.rect.bottom > -50:
                    enemy3_out.play()
            else:
                #毁灭
                if not (delay % 4):
                    if e3_destroy_index == 0:
                        enemy3_down.play()
                    screen.blit(each.destroy_images[e3_destroy_index],
                                each.rect)
                    e3_destroy_index = (e3_destroy_index + 1) % 7
                    if e3_destroy_index == 0:
                        enemy3_down.stop()
                        each.reset()

        #将我方飞机绘制到屏幕中
        # screen.blit(me.image1, me.rect)

        #飞机碰撞检测
        # enemies_down = pygame.sprite.spritecollide(me, enemies, False)
        enemies_down = pygame.sprite.spritecollide(me, enemies, False,
                                                   pygame.sprite.collide_mask)
        if enemies_down:
            me.active = False
            for e in enemies_down:
                e.active = False

        #绘制英雄机
        if me.active:
            if switchImage:
                screen.blit(me.image1, me.rect)
            else:
                screen.blit(me.image2, me.rect)
        else:
            if not (delay % 10):
                screen.blit(me.destroy_images[me_destroy_index], me.rect)
                me_destroy_index = (me_destroy_index + 1) % 4
                if me_destroy_index == 0:
                    me.reset()

        if not (delay % 10):
            switchImage = not switchImage

        delay -= 1
        if not delay:
            delay = 100
        pygame.display.flip()

        clock.tick(100)
Exemplo n.º 6
0
def main():
    clock = pygame.time.Clock()
    switch_image = False
    delay = 60
    running = True
    pygame.mixer.music.play(-1)
    me = myPlane.MyPlane(bg_size)
    score = 0
    paused = False
    pause_nor_image = pygame.image.load("resources/image/game_pause_nor.png")
    pause_pressed_image = pygame.image.load("resources/image/game_pause_pressed.png")
    resume_nor_image = pygame.image.load("resources/image/game_resume_nor.png")
    resume_pressed_image = pygame.image.load("resources/image/game_resume_pressed.png")
    paused_rect = pause_nor_image.get_rect()
    paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
    paused_image = pause_nor_image
    score_font = pygame.font.SysFont("Helvetica", 50)
    color_black = (0, 0, 0)
    color_green = (0, 255, 0)
    color_red = (255, 0, 0)
    color_white = (255, 255, 255)
    bomb_image = pygame.image.load("resources/image/bomb.png")
    bomb_rect = bomb_image.get_rect()
    bomb_front = pygame.font.SysFont("Helvetica", 50)
    bomb_num = 3
    level = 1
    life_image = pygame.image.load("resources/image/enemy1.png")
    life_rect = life_image.get_rect()
    life_num = 3
    invincible_time = USEREVENT + 2
    flag_recorded = False
    gameover_image = pygame.image.load("resources/image/game_over.png")
    gameover_rect = gameover_image.get_rect()


    bullet1 = []
    bullet1_index = 0
    bullet1_num = 6
    for i in range(bullet1_num):
        bullet1.append(bullet.Bullet1(me.rect.midtop))


    double_bullet_timer = USEREVENT + 1
    is_double_bullet = False
    bullet2 = []
    bullet2_index = 0
    bullet2_num = 10
    for i in range(bullet2_num//2):
        bullet2.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery)))
        bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))


    enemies = pygame.sprite.Group()
    small_enemies = pygame.sprite.Group()
    add_small_enemies(small_enemies, enemies, 1)
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(mid_enemies, enemies, 1)
    big_enemies = pygame.sprite.Group()
    add_big_enemies(big_enemies, enemies, 1)


    bullet_supply = supply.BulletSupply(bg_size)
    bomb_supply = supply.BombSupply(bg_size)
    supply_timer = USEREVENT
    pygame.time.set_timer(supply_timer, 10 * 1000)


    e1_destroy_index = 0
    e2_destroy_index = 0
    e3_destroy_index = 0
    me_destroy_index = 0


    while running:
        screen.blit(background, (0, 0))
        score_text = score_font.render("Score : %s" % str(score), True, color_white)
        screen.blit(score_text, (10, 5))


        if level == 1 and score > 5000:
            level = 2
            level_up_sound.play()
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            inc_speed(small_enemies, 1)
        elif level == 2 and score > 30000:
            level = 3
            level_up_sound.play()
            pygame.time.set_timer(supply_timer, 20 * 1000)
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            inc_speed(small_enemies, 1)
            inc_speed(mid_enemies, 1)
        elif level == 3 and score > 60000:
            level = 4
            level_up_sound.play()
            pygame.time.set_timer(supply_timer, 30 * 1000)
            add_small_enemies(small_enemies, enemies, 3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            inc_speed(small_enemies, 1)
            inc_speed(mid_enemies, 1)
            inc_speed(big_enemies, 1)


        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                button_down_sound.play()
                if event.button == 1 and paused_rect.collidepoint(event.pos):
                    paused = not paused
                    if paused:
                        paused_image = resume_pressed_image
                        pygame.time.set_timer(supply_timer, 0)
                        pygame.mixer.music.pause()
                        pygame.mixer.pause()
                    else:
                        paused_image = pause_pressed_image
                        pygame.time.set_timer(supply_timer, 30 * 1000)
                        pygame.mixer.music.unpause()
                        pygame.mixer.unpause()

            elif event.type == MOUSEMOTION:
                if paused_rect.collidepoint(event.pos):
                    if paused:
                        paused_image = resume_pressed_image
                    else:
                        paused_image = pause_pressed_image
                else:
                    if paused:
                        paused_image = resume_nor_image
                    else:
                        paused_image = pause_nor_image
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if bomb_num:
                        bomb_num -= 1
                        bomb_sound.play()
                        for each in enemies:
                            if each.rect.bottom > 0:
                                each.active = False
            elif event.type == supply_timer:
                if choice([True, False]):
                    bomb_supply.reset()
                else:
                    bullet_supply.reset()
            elif event.type == double_bullet_timer:
                is_double_bullet = False
                pygame.time.set_timer(double_bullet_timer, 0)
            elif event.type == invincible_time:
                me.invincible = False
                pygame.time.set_timer(invincible_time, 0)
        screen.blit(paused_image, paused_rect)

        if life_num and (not paused):

            bomb_text = bomb_front.render("X {}".format(bomb_num), True, color_black)
            bomb_text_rect = bomb_text.get_rect()
            screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
            screen.blit(bomb_text, (20 + bomb_rect.width, height - 10 - bomb_text_rect.height))
            if life_num:
                for i in range(life_num):
                    screen.blit(life_image, (width - 10 - (i + 1) * life_rect.width, height - 10 - life_rect.height))


            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_w] or key_pressed[K_UP]:
                me.move_up()
            if key_pressed[K_s] or key_pressed[K_DOWN]:
                me.move_down()
            if key_pressed[K_a] or key_pressed[K_LEFT]:
                me.move_left()
            if key_pressed[K_d] or key_pressed[K_RIGHT]:
                me.move_right()

            if not (delay % 10):
                bullet_sound.play()
                if not is_double_bullet:
                    bullets = bullet1
                    bullets[bullet1_index].reset(me.rect.midtop)
                    bullet1_index = (bullet1_index + 1) % bullet1_num
                else:
                    bullets = bullet2
                    bullets[bullet2_index].reset((me.rect.centerx - 33, me.rect.centery))
                    bullets[bullet2_index + 1].reset((me.rect.centerx + 30, me.rect.centery))
                    bullet2_index = (bullet2_index + 2) % bullet2_num


            if bomb_supply.active:
                bomb_supply.move()
                screen.blit(bomb_supply.image, bomb_supply.rect)
                if pygame.sprite.collide_mask(bomb_supply, me):
                    get_bomb_sound.play()
                    if bomb_num < 3:
                        bomb_num += 1
                    bomb_supply.active = False
            if bullet_supply.active:
                bullet_supply.move()
                screen.blit(bullet_supply.image, bullet_supply.rect)
                if pygame.sprite.collide_mask(bullet_supply, me):
                    get_bullet_sound.play()
                    is_double_bullet = True
                    pygame.time.set_timer(double_bullet_timer, 18 * 1000)
                    bullet_supply.active = False


            for b in bullets:
                if b.active:
                    b.move()
                    screen.blit(b.image, b.rect)
                    enemies_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
                    if enemies_hit:
                        b.active = False
                        for e in enemies_hit:
                            if e in big_enemies or e in mid_enemies:
                                e.energy -= 1
                                e.hit = True
                                if e.energy == 0:
                                    e.active = False
                            else:
                                e.active = False


            enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
            if enemies_down and not me.invincible:
                me.active = False
                for e in enemies_down:
                    e.active = False


            if delay == 0:
                delay = 60
            delay -= 1
            if not delay % 3:
                switch_image = not switch_image
            if me.active:
                if switch_image:
                    screen.blit(me.image1, me.rect)
                else:
                    screen.blit(me.image2, me.rect)
            else:
                if not (delay % 3):
                    screen.blit(me.destroy_images[me_destroy_index], me.rect)
                    me_destroy_index = (me_destroy_index + 1) % 4
                    if me_destroy_index == 0:
                        me_down_sound.play()
                        life_num -= 1
                        me.reset()
                        pygame.time.set_timer(invincible_time, 3 * 1000)


            for each in big_enemies:
                if each.active:
                    each.move()
                    if not each.hit:
                        if switch_image:
                            screen.blit(each.image1, each.rect)
                        else:
                            screen.blit(each.image2, each.rect)
                    else:
                        screen.blit(each.image_hit, each.rect)
                        each.hit = False


                    pygame.draw.line(screen, color_black,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5),
                                     2)
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = color_green
                    else:
                        energy_color = color_red
                    pygame.draw.line(screen, energy_color,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
                                     2)
                    if each.rect.bottom == -50:
                        big_enemy_flying_sound.play(-1)

                else:
                    big_enemy_flying_sound.stop()
                    if e3_destroy_index == 0:
                        enemy3_down_sound.play()
                    if not (delay % 3):
                        screen.blit(each.destroy_images[e3_destroy_index], each.rect)
                        e3_destroy_index = (e3_destroy_index + 1) % 6
                        if e3_destroy_index == 0:
                            score += 6000
                            each.reset()

            for each in mid_enemies:
                if each.active:
                    each.move()
                    if not each.hit:
                        screen.blit(each.image, each.rect)
                    else:
                        screen.blit(each.image_hit, each.rect)
                        each.hit = False


                    pygame.draw.line(screen, color_black,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5),
                                     2)
                    energy_remain = each.energy / enemy.MidEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = color_green
                    else:
                        energy_color = color_red
                    pygame.draw.line(screen, energy_color,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
                                     2)
                else:
                    if e2_destroy_index == 0:
                        enemy2_down_sound.play()
                    if not (delay % 3):
                        screen.blit(each.destroy_images[e2_destroy_index], each.rect)
                        e2_destroy_index = (e2_destroy_index + 1) % 4
                        if e2_destroy_index == 0:
                            score += 2000
                            each.reset()

            for each in small_enemies:
                if each.active:
                    each.move()
                    screen.blit(each.image, each.rect)
                else:
                    if e1_destroy_index == 0:
                        enemy1_down_sound.play()
                    if not (delay % 3):
                        screen.blit(each.destroy_images[e1_destroy_index], each.rect)
                        e1_destroy_index = (e1_destroy_index + 1) % 4
                        if e1_destroy_index == 0:
                            score += 500
                            each.reset()
        elif life_num == 0:
            screen.blit(gameover_image, gameover_rect)
            pygame.mixer.music.stop()
            pygame.mixer.stop()
            pygame.time.set_timer(supply_timer, 0)

            if not flag_recorded:
                flag_recorded = True
                with open("score_record.txt", "r") as f:
                    record_score = int(f.read())
                if score > record_score:
                    with open("score_record.txt", "w") as f:
                        f.write(str(score))

            record_score_text = score_font.render("%d" % record_score, True, color_white)
            screen.blit(record_score_text, (150, 25))
            game_over_score_text = score_font.render("%d" % score, True, color_white)
            screen.blit(game_over_score_text, (180, 370))

        pygame.display.flip()
        clock.tick(60)