美文网首页
week3 打灰机 基本框架

week3 打灰机 基本框架

作者: HavenYoung | 来源:发表于2018-08-06 08:40 被阅读0次
    import pygame
    import time
    import random
    import replay
    from plane import Plane
    from bullet2 import Bullet2
    from bullet1 import Bullet1
    from enemy1 import Enemy1
    from enemy2 import Enemy2
    
    # 方向对应的key值
    up = 273
    down = 274
    left = 276
    right = 275
    
    # 定义敌人1集合
    all_enemy1 = []
    # 定义敌人1产生间隔
    enemy1_interval = 10
    enemy1_speed_y = 10
    
    # 定义敌人2
    all_enemy2 = []
    enemy2_interval = 100
    enemy2_speed_y = 5
    
    # 定义己方子弹集合
    all_bullet2 = []
    
    # 定义子弹速度
    bullet2_interval = 2
    
    # 定义敌方子弹
    all_bullet1 = []
    bullet1_interval = 10
    
    
    # 加载背景图片
    def set_background(local):
        # 背景图片
        back_ground = pygame.image.load('./image/background.png')
    
        local.blit(back_ground, (0, 0))
    
    
    # 加载飞机 100x124
    def set_plane(local, item):
        new_plane = pygame.image.load(item.plane)
        local.blit(new_plane, (item.plane_x, item.plane_y))
    
    
    # 己方飞机边界判定
    def is_border(item):
        if item.plane_x <= 0:
            item.plane_speed_x = 0
        if item.plane_x + item.weight >= 400:
            item.plane_speed_x = 0
        if item.plane_y <= 0:
            item.plane_speed_y = 0
        if item.plane_y + item.height >= 600:
            item.plane_speed_y = 0
    
    
    # 飞机开火,添加一个子弹对象
    def plane_fire(item):
        # 建立子弹对象
        bullet2 = Bullet2()
        # 设置子弹的初始位置
        bullet2.bullet2_x = item.plane_x + item.weight/2
        bullet2.bullet2_y = item.plane_y
    
        all_bullet2.append(bullet2)
    
    
    # 画出己方飞机的子弹
    def set_bullet2(local):
        for bullet2 in all_bullet2:
            new_bullet2 = pygame.image.load(bullet2.bullet2)
            bullet2.bullet2_y += bullet2.bullet2_speed_y
            local.blit(new_bullet2, (bullet2.bullet2_x, bullet2.bullet2_y))
            if bullet2.bullet2_y + bullet2.bullet2_speed_y <= -10:
                all_bullet2.remove(bullet2)
    
    
    # 产生敌方子弹
    def create_bullet1():
        for enemy1 in all_enemy1:
            bullet1 = Bullet1()
            bullet1.bullet1_x = enemy1.x + enemy1.weight/2
            bullet1.bullet1_y = enemy1.y + enemy1.height
    
            all_bullet1.append(bullet1)
    
        for enemy2 in all_enemy2:
            bullet1 = Bullet1()
            bullet1.bullet1_x = enemy2.x + enemy2.weight/2
            bullet1.bullet1_y = enemy2.y + enemy2.height
    
            all_bullet1.append(bullet1)
    
    
    # 画出敌方子弹
    def set_bullet1(local):
        for bullet1 in all_bullet1:
            new_bullet1 = pygame.image.load(bullet1.bullet1)
            bullet1.bullet1_y += bullet1.bullet1_speed_y
            local.blit(new_bullet1, (bullet1.bullet1_x, bullet1.bullet1_y))
            if bullet1.bullet1_y + bullet1.bullet1_speed_y >= 600:
                all_bullet1.remove(bullet1)
    
    
    # 判断己方飞机是否摧毁
    def is_destroy_plane_enemy1(item):
        for enemy1 in all_enemy1:
            if item.plane_x - enemy1.weight <= enemy1.x <= item.plane_x + item.weight \
                    and item.plane_y + item.height >= enemy1.y >= item.plane_y - enemy1.height:
                return True
    
    
    # 创建敌人1
    def create_enemy1():
        enemy1 = Enemy1()
        enemy1.x = random.randint(150, 250)
        enemy1.y = 0
        enemy1.speed_x = random.randint(-10, 10)
        enemy1.speed_y = enemy1_speed_y
        all_enemy1.append(enemy1)
    
    
    # 画敌人1
    def set_enemy1(local):
        for enemy1 in all_enemy1:
            new_enemy1 = pygame.image.load(enemy1.enemy1)
            enemy1.x += enemy1.speed_x
            enemy1.y += enemy1.speed_y
            local.blit(new_enemy1, (enemy1.x, enemy1.y))
            if enemy1.x <= -51 or enemy1.x >= 400 or enemy1.y >= 600:
                all_enemy1.remove(enemy1)
    
    
    # 子弹敌人1碰撞检测
    def is_destroy_enemy1():
        score_1 = 0
        for enemy1 in all_enemy1:
            for bullet2 in all_bullet2:
                if enemy1.x <= bullet2.bullet2_x <= enemy1.x + 60 and enemy1.y + 39 >= bullet2.bullet2_y:
                    all_bullet2.remove(bullet2)
                    enemy1.hp -= 1
                    if enemy1.hp <= 0:
                        all_enemy1.remove(enemy1)
                        score_1 += 1
                        break
        return score_1
    
    
    # 创建敌人2
    def create_enemy2():
        enemy2 = Enemy2()
        enemy2.x = random.randint(150, 250)
        enemy2.y = 0
        enemy2.speed_x = random.randint(-5, 5)
        enemy2.speed_y = enemy2_speed_y
        all_enemy2.append(enemy2)
    
    
    # 画敌人2
    def set_enemy2(local):
        for enemy2 in all_enemy2:
            new_enemy2 = pygame.image.load(enemy2.enemy2)
            enemy2.x += enemy2.speed_x
            enemy2.y += enemy2.speed_y
            local.blit(new_enemy2, (enemy2.x, enemy2.y))
            if enemy2.x <= 0 or enemy2.x + 69 >= 400:
                enemy2.speed_x *= -1
            if enemy2.y >= 600:
                all_enemy2.remove(enemy2)
    
    
    # 子弹敌人2碰撞检测
    def is_destroy_enemy2():
        score_2 = 0
        for enemy2 in all_enemy2:
            for bullet2 in all_bullet2:
                if enemy2.x <= bullet2.bullet2_x <= enemy2.x + 75 and enemy2.y + 89 >= bullet2.bullet2_y:
                    all_bullet2.remove(bullet2)
                    enemy2.hp -= 1
                    if enemy2.hp <= 0:
                        all_enemy2.remove(enemy2)
                        score_2 += 10
                        break
        return score_2
    
    
    # 显示游戏时间
    def time_show(local, timer):
        font = pygame.font.SysFont('Pristina', 20)
        surface = font.render('time:' + str(timer), True, (255, 100, 0))
        local.blit(surface, (10, 10))
    
    
    # 显示分数
    def score_show(local, score):
        font = pygame.font.SysFont('Pristina', 20)
        surface = font.render('score:' + str(score), True, (255, 255, 0))
        local.blit(surface, (10, 30))
    
    
    def set_plane_blowup(local, item):
        new_plane = pygame.image.load('./image/hero_blowup_n3.png')
        local.blit(new_plane, (item.plane_x, item.plane_y))
    
    
    if __name__ == '__main__':
        play = True
        while True:
            # 初始化
            pygame.init()
            # 设置窗口大小
            screen = pygame.display.set_mode((400, 600))
            # 设置窗口标题
            pygame.display.set_caption('打灰机')
    
            def game_start():
                # 加载背景图片
                set_background(screen)
    
                # 生成一个飞机
                plane = Plane()
    
                # 显示内容
                pygame.display.flip()
    
                # 定义分数:
                score = 0
    
                time_start = int(time.time())
                print(time_start)
                count = 1
    
                # 游戏循环
                while True:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            exit(0)
    
                        if event.type == pygame.MOUSEMOTION:
                            x, y = event.pos
                            # 重新定位飞机
                            plane.plane_x = x-plane.weight/2
                            plane.plane_y = y-plane.height/2
    
                    # 自动开火
                    if not count % bullet2_interval:
                        plane_fire(plane)
    
                    if not count % bullet1_interval:
                        create_bullet1()
    
                    # 边界检测
                    is_border(plane)
    
                    # 产生敌人1
                    if not count % enemy1_interval:
                        create_enemy1()
    
                    # 判断是否消灭
                    score1 = is_destroy_enemy1()
    
                    # 产生敌人2
                    if not count % enemy2_interval:
                        create_enemy2()
    
                    # 判断敌人2是否被消灭
                    score2 = is_destroy_enemy2()
                    score += score1 + score2
    
                    # 游戏时间
                    time_current = int(time.time())
                    time_interval = time_current - time_start
    
                    # 刷新背景、动态
                    set_background(screen)
                    if not is_destroy_plane_enemy1(plane):
                        set_plane(screen, plane)
                    else:
                        return time_interval, score, plane
                    set_bullet2(screen)
                    set_bullet1(screen)
                    set_enemy1(screen)
                    set_enemy2(screen)
                    time_show(screen, time_interval)
                    score_show(screen, score)
    
                    # 刷新屏幕
                    pygame.display.update()
                    pygame.time.delay(50)
                    count += 1
    
            if play:
                time_interval_end, score_end, plane_blowup = game_start()
    
            set_background(screen)
            set_plane_blowup(screen, plane_blowup)
            set_bullet2(screen)
            set_enemy1(screen)
            set_enemy2(screen)
            time_show(screen, time_interval_end)
            score_show(screen, score_end)
            replay.game_over(screen)
            pygame.display.flip()
    
    
    
            # 游戏出口
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit(0)
    
            play = False
    
    
    class Bullet1:
        """子弹类"""
        def __init__(self):
            # 子弹图片的路径
            self.bullet1 = './image/bullet1.png'
            # 子弹的默认速度
            self.bullet1_speed_x = 0
            self.bullet1_speed_y = 20
            # 子弹的size
            self.height = 21
            self.weight = 9
    
    class Bullet2:
        """子弹类"""
        def __init__(self):
            # 子弹图片的路径
            self.bullet2 = './image/bullet2.png'
            # 子弹的默认速度
            self.bullet2_speed_x = 0
            self.bullet2_speed_y = -20
            # 子弹的size
            self.height = 21
            self.weight = 9
    
    class Enemy1:
        """敌人1"""
        def __init__(self):
            self.enemy1 = './image/enemy1_5.png'
            self.weight = 39
            self.height = 51
            self.hp = 1
    
    
    if __name__ == '__main__':
        pass
    
    class Enemy2:
        """敌人2"""
        def __init__(self):
            self.enemy2 = './image/enemy2.png'
            self.weight = 69
            self.height = 89
            self.hp = 10
    
    
    if __name__ == '__main__':
        pass
    
    class Plane:
        """飞机类"""
        def __init__(self):
            # 飞机图片的路径
            self.plane = './image/hero1.png'
            # 飞机的初始位置
            self.plane_x = 150
            self.plane_y = 400
            # 飞机的初始速度
            self.plane_speed_x = 0
            self.plane_speed_y = 0
            # 飞机图片的size
            self.height = 124
            self.weight = 100
    
    
    if __name__ == '__main__':
        pass
    

    相关文章

      网友评论

          本文标题:week3 打灰机 基本框架

          本文链接:https://www.haomeiwen.com/subject/ltcwvftx.html