Exemple #1
0
def run_game():
    bg = pygame.image.load("images/map01.jpeg")
    game_settings = Settings()
    pos_x = 0  #地图移动

    pygame.init()
    screen = pygame.display.set_mode(
        (game_settings.screen_width, game_settings.screen_height))
    pygame.display.set_caption("XiaoBai Game")
    stats = GameStats(game_settings)
    player = Game_Player(game_settings, screen)
    bullets = Group()
    boss = Group()
    enemys = Group()
    win_button = Button(game_settings, screen, "YOU WIN")
    gf.create_legion(game_settings, screen, enemys)
    gf.create_boss(game_settings, screen, player, boss)

    while True:
        pygame.mouse.set_visible(False)
        gf.check_events(game_settings, screen, player, bullets)
        gf.update_player(game_settings, stats, player, enemys)
        gf.update_bullet(game_settings, bullets, screen, enemys, boss)
        gf.update_enemys(game_settings, enemys)
        gf.update_boss(game_settings, boss)
        gf.update_screen(game_settings, bg, pos_x, screen, player, bullets,
                         enemys, boss, win_button)

        if player.moving_right and player.center > player.screen_rect.centerx and game_settings.boss_appear == False:
            game_settings.screen_rolling = True
            pos_x -= 5  #屏幕滚动速度
        else:
            game_settings.screen_rolling = False
Exemple #2
0
def start_game():
    # Initialize game and create a screen object.
    set_settings = Settings()
    screen = pygame.display.set_mode(
        (set_settings.screen_width, set_settings.screen_height))
    pygame.display.set_caption("GAME")

    dan = Dan(set_settings, screen)

    enes = Group()
    oranges = Group()

    start = True

    while start:
        image = pygame.image.load('images/homescreen.bmp')
        rect = image.get_rect()
        rect.centerx = set_settings.screen_width / 2
        rect.centery = set_settings.screen_height / 2
        screen.blit(image, rect)
        pygame.display.flip()
        game_functions.check_events(set_settings, screen, dan, oranges, enes)

        event = pygame.event.wait()
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            start = False
            pygame.mixer.music.load('sounds/music.mp3')
            pygame.mixer.Channel(0).play(
                pygame.mixer.Sound('sounds/music.mp3'))
            pygame.mixer.music.play(-1)

    clock = pygame.time.Clock()

    sendin = 0
    while True:
        clock.tick(500)
        if set_settings.game_over == False:
            game_functions.game_over(enes, dan, set_settings)
            game_functions.check_events(set_settings, screen, dan, oranges,
                                        enes)
            dan.update()
            if sendin % (int(500 / set_settings.enemy_difficulty)) == 0:
                game_functions.send_in_the_enemys(enes, set_settings, screen)
                if sendin % 2000 == 0:
                    set_settings.enemy_difficulty += 1
                sendin += 1
            else:
                sendin += 1

            game_functions.update_enemys(enes, dan, set_settings)
            game_functions.update_oranges(oranges, enes, set_settings)
            game_functions.screen_refresh(set_settings, screen, dan, enes,
                                          oranges)

        else:
            sendin = 0
            set_settings.enemy_difficulty = 1
            for ene in enes:
                enes.remove(ene)
            screen.fill((255, 000, 000))
            image = pygame.image.load('images/gameover.bmp')
            rect = image.get_rect()
            rect.centerx = set_settings.screen_width / 2
            rect.centery = set_settings.screen_height / 2
            screen.blit(image, rect)
            font = pygame.font.SysFont(None, 60)
            WHITE = (255, 255, 255)
            img = font.render("SCORE: " + str(set_settings.score), True, WHITE)
            screen.blit(img, (50, 50))
            pygame.display.flip()
            game_functions.check_events(set_settings, screen, dan, oranges,
                                        enes)
def run_game():
    pygame.init()
    ai_settings = Settings()
    #pygame.display.set_mode返回一个surface对象
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    bullet_sound = pygame.mixer.Sound('sound/bullet.wav')
    bullet_sound.set_volume(0.3)
    pygame.mixer.music.load('sound/game_music.wav')
    pygame.mixer.music.play(-1, 0.0)
    pygame.mixer.music.set_volume(0.25)
    enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
    enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
    enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
    game_over_sound = pygame.mixer.Sound('sound/game_over.wav')
    enemy1_down_sound.set_volume(0.3)
    enemy2_down_sound.set_volume(0.4)
    enemy3_down_sound.set_volume(0.5)
    game_over_sound.set_volume(0.3)
    shoot_frequency = 0
    enemy_frequency = 0
    background = pygame.image.load("images\\background_1.png").convert()
    _game_over = pygame.image.load('images/gameover.png')
    game_over = pygame.transform.scale(_game_over, (320, 568))
    pygame.display.set_caption("plane_war")
    play_button = Button(ai_settings, screen, "play")
    stats = GameStats(ai_settings)
    s = Score(ai_settings, screen, stats)
    plane = Plane(ai_settings, screen)
    bullets = Group()
    framerate = pygame.time.Clock()
    Heroplane = Mysprite(screen)
    Heroplane.load("images//hero_plane_1.png", 66, 80, 2)
    group = pygame.sprite.Group()
    group.add(Heroplane)
    enemys = Group()
    middle = Group()
    middle_frequency = 0
    enemies_down = pygame.sprite.Group()
    breakplane = Group()
    middle_down = pygame.sprite.Group()
    bosses = Group()
    break_boss = Group()
    delay = 0
    delay_1 = 0
    delay_2 = 0
    delay_3 = 0
    while True:
        framerate.tick(60)
        #blit用于在screen上绘制图像
        screen.blit(background, (0, 0))
        gf.check_events(ai_settings, screen, stats, play_button, plane, enemys,
                        bullets, bullet_sound, s)
        if stats.game_active and not stats.boss_state:
            plane.update()
            middle.update()
            gf.update_bullets(ai_settings, screen, plane, enemys, bullets,
                              stats, s, shoot_frequency, enemies_down,
                              enemy1_down_sound, middle, middle_down,
                              enemy2_down_sound, bosses, break_boss,
                              enemy3_down_sound)
            gf.update_enemys(ai_settings, stats, screen, plane, enemys,
                             bullets, s, play_button, breakplane,
                             game_over_sound, enemies_down, middle,
                             middle_down, bosses)
            if enemy_frequency % 60 == 0 and len(
                    enemys) < ai_settings.min_enemy_number:
                gf.create_fleet(ai_settings, screen, plane, enemys)
            else:
                enemy_frequency += 1
            if middle_frequency % 100 == 0 and len(
                    middle) < ai_settings.min_middle_number:
                gf.create_middle_enemys(ai_settings, screen, middle)
            else:
                middle_frequency += 1
        if stats.game_active and stats.boss_state:
            plane.update()
            gf.update_bullets(ai_settings, screen, plane, enemys, bullets,
                              stats, s, shoot_frequency, enemies_down,
                              enemy1_down_sound, middle, middle_down,
                              enemy2_down_sound, bosses, break_boss,
                              enemy3_down_sound)
            gf.update_bosses(ai_settings, plane, breakplane, stats, s,
                             game_over_sound, play_button, bosses)
            if (len(bosses) <= 0):
                gf.create_boss(ai_settings, screen, bosses)
            bosses.update()
        if not stats.game_active and stats.game_state:
            enemys.empty()
            bullets.empty()
            screen.blit(game_over, (0, 0))
        for planes in breakplane:
            if delay < 10:
                planes.image = pygame.image.load("images\\hero_break1.png")
                delay += 1
            elif delay < 20:
                planes.image = pygame.image.load("images\\hero_break2.png")
                delay += 1
            elif delay < 30:
                planes.image = pygame.image.load("images\\hero_break3.png")
                delay += 1
            elif delay < 40:
                planes.image = pygame.image.load("images\\hero_break4.png")
                delay += 1
            screen.blit(planes.image, plane.rect)
            if delay >= 40:
                delay = 0
                breakplane.remove(plane)
        for enemy_down in enemies_down:
            if delay_1 < 10:
                enemy_down.image = pygame.image.load("images\\enemy1_1.png")
                delay_1 += 1
            elif delay_1 < 20:
                enemy_down.image = pygame.image.load("images\\enemy1_2.png")
                delay_1 += 1
            elif delay_1 < 30:
                enemy_down.image = pygame.image.load("images\\enemy1_3.png")
                delay_1 += 1
            elif delay_1 < 40:
                enemy_down.image = pygame.image.load("images\\enemy1_4.png")
                delay_1 += 1

            screen.blit(enemy_down.image, enemy_down.rect)
            if delay_1 >= 40:
                delay_1 = 0
                enemies_down.remove(enemy_down)
        for mi_down in middle_down:
            if delay_2 < 10:
                mi_down.image = pygame.image.load("images\\middle_1.png")
                delay_2 += 1
            elif delay_2 < 20:
                mi_down.image = pygame.image.load("images\\middle_2.png")
                delay_2 += 1
            elif delay_2 < 30:
                mi_down.image = pygame.image.load("images\\middle_3.png")
                delay_2 += 1
            elif delay_2 < 40:
                mi_down.image = pygame.image.load("images\\middle_4.png")
                delay_2 += 1

            screen.blit(mi_down.image, mi_down.rect)
            if delay_2 >= 40:
                delay_2 = 0
                middle_down.remove(mi_down)
        for boss_down in break_boss:
            if delay_3 < 10:
                boss_down.image = pygame.image.load("images\\boss_1.png")
                delay_3 += 1
            elif delay_3 < 20:
                boss_down.image = pygame.image.load("images\\boss_2.png")
                delay_3 += 1
            elif delay_3 < 30:
                boss_down.image = pygame.image.load("images\\boss_3.png")
                delay_3 += 1
            elif delay_3 < 40:
                boss_down.image = pygame.image.load("images\\boss_4.png")
                delay_3 += 1
            elif delay_3 < 50:
                boss_down.image = pygame.image.load("images\\boss_4.png")
                delay_3 += 1
            elif delay_3 < 60:
                boss_down.image = pygame.image.load("images\\boss_4.png")
                delay_3 += 1

            screen.blit(boss_down.image, boss_down.rect)
            if delay_3 >= 60:
                delay_3 = 0
                break_boss.remove(boss_down)
        gf.update_screen(ai_settings, screen, stats, plane, enemys, bullets,
                         play_button, s, middle, bosses)

        pygame.display.update()
def run_game():
    #初始化游戏
    pygame.init()
    ai_settings=Settings()
    
    #初始化背景
    #screen=pygame.display.set_mode((1200,800))
    screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("model-yx")
    #初始化游戏窗口
#  创建按钮
    play_button=Button(ai_settings,screen,"play")
  
  
  
    #创建飞机
    plane=Plane(screen,ai_settings)
#    创建储存子弹的编组
    bullets = Group()
#    创建敌人编组
    enemys=Group()
    
    
    
    #设置背景色
#    bg_color=(230,230,231)
    
    
#    创建一个敌人
 #   enemy=Enemy(ai_settings,screen)
#    创建一群敌人
    gf.create_fleet(ai_settings,screen,plane,enemys)
    
    
    
    
#    创建一个用于储存游戏统计信息的实例
    stats=GameStats(ai_settings)
    
    
    
    #开始游戏循环
    while True:
      gf.check_events(ai_settings,screen,stats,play_button,plane,enemys,bullets)
      
#         #监视键盘和鼠标事件
#         '''for event in pygame.event.get():
#             if event.type==pygame.QUIT:  #退出事件
#                 sys.exit()'''
   
      if stats.game_active:
        plane.update()    
#        bullets.update()
#         删除已经消失的子弹 防止内存膨胀
#         for bullet in bullets.copy():
#           if bullet.rect.bottom<=0:
#             bullets.remove(bullet)
#移动到 update_bullets(bullets)
  
        gf.update_bullets(ai_settings,screen,plane,enemys,bullets)
        gf.update_enemys(ai_settings,stats,screen,plane,enemys,bullets)
#         print(len(bullets))
#             检查子弹是否真的删除
         
                 
#         #重绘屏幕
#         screen.fill(bg_color)
#         screen.fill(ai_settings.bg_color)
#         plane.blitme()
#         #绘制飞机
#         
#         
#         # 更新屏幕
#         pygame.display.flip()
      gf.update_screen(ai_settings,screen,stats,plane,enemys,bullets,play_button)
Exemple #5
0
def run_game():
    # нициализирует pygame, settings и объект экрана.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    background_image = pygame.image.load("background.jpg").convert()
    background_image = pygame.transform.scale(
        background_image,
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    clock = pygame.time.Clock()
    fps = 90

    # Создание кнопки Play.
    play_button = Button(ai_settings, screen, "Play")

    # Создание корабля, группы пуль и группы пришельцев.
    ship = Ship(ai_settings, screen)

    bullets = Group()
    enemys = Group()

    # Создание экземпляров GameStats и Scoreboard.
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Создание флота пришельцев.
    gf.create_fleet(ai_settings, screen, ship, enemys)

    def load_image(name):
        fullname = 'images' + '/' + name
        try:
            if name[-2:] == 'jpg' or name[-2:] == 'png':
                image = pygame.image.load(fullname).convert()
            else:
                image = pygame.image.load(fullname).convert_alpha()
        except:
            print('Cannot load image:', name)
            raise SystemExit()
        return image

    class AnimateSprite(pygame.sprite.Sprite):
        def __init__(self, image, columns, rows, x, y):
            super().__init__()
            self.frames = []
            self.cut_image(image, columns, rows)
            self.frame_number = 0
            self.image = self.frames[self.frame_number]
            self.rect = self.rect.move(x, y)
            self.add(all_sprites)

        def cut_image(self, image, columns, rows):
            """Разрезает картинку спрайта на равные части"""
            self.rect = pygame.Rect(0, 0,
                                    image.get_width() // columns,
                                    image.get_height() // rows)

            for j in range(rows):
                for i in range(columns):
                    frame_location = (self.rect.w * i, self.rect.h * j)
                    self.frames.append(
                        image.subsurface(
                            pygame.Rect(frame_location, self.rect.size)))

        def update(self):
            self.frame_number = (self.frame_number + 1) % len(self.frames)
            self.image = self.frames[self.frame_number]

    all_sprites = pygame.sprite.Group()
    deton = AnimateSprite(load_image("deton.png"), 8, 6, 400, 700)

    # Запуск основного цикла игры.
    while True:

        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
                        enemys, bullets)
        screen.blit(background_image, [0, 0])
        all_sprites.draw(screen)
        all_sprites.update()
        clock.tick(fps)

        if stats.game_active:
            ship.update()
            gf.update_bullets(ai_settings, screen, stats, sb, ship, enemys,
                              bullets)
            gf.update_enemys(ai_settings, screen, stats, sb, ship, enemys,
                             bullets)
        gf.update_screen(ai_settings, screen, stats, sb, play_button, ship,
                         enemys, bullets)

        # Отображение последнего прорисованного экрана.
        pygame.display.flip()