Example #1
0
def run_game(screen, level):

    level_file = get_level(str(level) + ".txt")
    
    with open(level_file) as l:
        level_objects = l.read()
                
    # Get level items
    level_items, player_items, game_items = level_objects.split("--\n")
    level_items = level_items.split("\n")
    
    # Create the side menu and game
    menu_group = pygame.sprite.Group()
    side_menu.containers = menu_group
    
    # Initialise the side menu
    side = side_menu(screen, player_items.split("\n"))
    
    text = [" "]
    # How long each text lasts for
    text_time = 300
    text_timer = 0
    
    # Initialise game
    for item in level_items:
        item = item.split(":")
        if item[0] == "Screen":
            game_size = item[1].split(",")
        if item[0] == "Text":
            text.append(item[1])
        if item[0] == "Cutscene":
            cut = Cutscene(screen, item[1])
            show_cutscene = True
            while show_cutscene:
                screen.fill((0, 0, 0))
                screen.blit(cut.image, (screen.get_width() / 2 - cut.image.get_width() / 2, screen.get_height() / 2 - cut.image.get_height() / 2))
                pygame.display.update()
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                    if event.type == pygame.KEYDOWN:
                        show_cutscene = False
                    # If the window is resized then update all objects
                    if event.type == pygame.VIDEORESIZE:
                        old_screen = pygame.display.get_surface()
                        screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags())
                        side.update_pos(screen)
                        play_area.update_size(screen)
        if item[0] == "Credits":
            credits(screen)
            font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
            text_render = font.render(" ", 1, (255, 255,255))
            with open(get_file("Levels/levels_info.txt")) as f:
                num_levels = f.readline()
                unlocked_levels = int(f.readline())
                
            with open(get_file("Levels/levels_info.txt"), "w") as f:
                max_level = max(unlocked_levels, level + 1)
                f.write(num_levels + str(max_level))
            return False
    
    play_area = Game(screen, (int(game_size[0]), int(game_size[1])), game_items.split("\n"))
    
    # Create music and additional music data
    music_group = pygame.sprite.Group()
    Music_Controls.containers = music_group
    music = Music_Controls(str(ceil((level * 8)/17)))
    music.play_music(pygame.mouse.get_pos(), pygame.mouse.get_pressed())
    
    # Create the clock
    # This is used to limit the frame-rate
    clock = pygame.time.Clock()
    
    # Initialise the mouse
    mouse = Mouse()
    
    while True:
        for event in pygame.event.get():
            # If the close button is pressed then quit
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    side.commands.append("play game")
                    side.play()
            # If the window is resized then update all objects
            if event.type == pygame.VIDEORESIZE:
                old_screen = pygame.display.get_surface()
                screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags())
                side.update_pos(screen)
                play_area.update_size(screen)
                font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
                text_render = font.render(text[0], 1, (255, 255,255))
                offset = [text_render.get_width() / 2, text_render.get_height() / 2]
                text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1]))
                
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_F11:
                    # get the flags of the surface
                    old_screen = pygame.display.get_surface()
                    
                    screen = pygame.display.set_mode((MAX_SIZE[0], MAX_SIZE[1]) if old_screen.get_flags() == pygame.FULLSCREEN else SCREEN_SIZE, 
                    pygame.RESIZABLE if old_screen.get_flags() == pygame.FULLSCREEN else pygame.FULLSCREEN)
                    
                    font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
                    text_render = font.render(text[0], 1, (255, 255,255))
                    offset = [text_render.get_width() / 2, text_render.get_height() / 2]
                    text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1]))
                
                
        # Update the side menu
        side.update(screen, play_area.pieces)
        play_area.pieces = []
        if "menu" in side.commands:
            font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
            text_render = font.render(" ", 1, (255, 255,255))
            return True
        if "level select" in side.commands:
            font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
            text_render = font.render(" ", 1, (255, 255,255))
            return False
        if "restart game" in side.commands:
            level_file = get_level(str(level) + ".txt")
    
            with open(level_file) as l:
                level_objects = l.read()
                        
            # Get level items
            level_items, player_items, game_items = level_objects.split("--\n")
            level_items = level_items.split("\n")
            #screen = pygame.display.set_mode(screen.get_size(), pygame.RESIZABLE)
            # Create the side menu and game
            menu_group = pygame.sprite.Group()
            side_menu.containers = menu_group
            
            # Initialise the side menu
            side = side_menu(screen, player_items.split("\n"))
            
            # Initialise game
            for item in level_items:
                item = item.split(":")
                if item[0] == "Screen":
                    game_size = item[1].split(",")
            
            play_area = Game(screen, (int(game_size[0]), int(game_size[1])), game_items.split("\n"))
            
        
        # Update the play area
        play_area.move(side.commands)
        
        # Remove the used commands
        side.commands = []
        
        # Draw the menu
        menu_group.draw(screen)
        screen_fill = pygame.Rect(play_area.draw_area)
        screen_fill.topleft = (0, 0)
        screen.fill((10, 20, 20), screen_fill)
        # Draw the game
        screen.blit(play_area.image, (0, 0), area=play_area.draw_area)
        
        # Draw the music controls
        music_group.draw(screen)
        
        # Draw text
        if text_timer == 0:
            text.pop(0)
            if len(text) == 0:
                text.append(" ")
            font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
            text_render = font.render(text[0], 1, (255, 255,255))
            offset = [text_render.get_width() / 2, text_render.get_height() / 2]
            text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1]))
            text_timer = text_time
                
        screen.blit(text_render, text_position)
        text_timer -= 1
        
        # Draw mouse
        if pygame.mouse.get_focused():
            x, y = pygame.mouse.get_pos()
            screen.blit(mouse.image, (x - 5, y - 5))
            
        # Draw screen
        pygame.display.update()
        
        # Cap framerate
        clock.tick(60)
        
        
        music.play_music(pygame.mouse.get_pos(), pygame.mouse.get_pressed())
            
        if play_area.finish == True:
            if play_area.loose == False:
                with open(get_file("Levels/levels_info.txt")) as f:
                    num_levels = f.readline()
                    unlocked_levels = int(f.readline())
                    
                with open(get_file("Levels/levels_info.txt"), "w") as f:
                    max_level = max(unlocked_levels, level + 1)
                    f.write(num_levels + str(max_level))
            
            font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32)))
            text_render = font.render(" ", 1, (255, 255,255))
            return False
Example #2
0
def credits(screen):
    credits_location = get_level("credits.txt")
    with open(credits_location) as l:
        credit_modules = l.read()
        
    credit_modules = credit_modules.split("--")
    for module in credit_modules:
        items = module.split("\n")
        
        static_items = []
        static_item_pos = []
        
        buttons = pygame.sprite.Group()
        Button.containers = buttons
        
        for item in items:
            item = item.split(":")
            if item[0] == "Header":
                item[1] = item[1].split(",")
                font = pygame.font.Font(get_font('arial.ttf'), int(item[1][6]))
                static_items.append(font.render(item[1][0], 1, (int(item[1][1]), int(item[1][2]), int(item[1][3]))))
                if item[1][7] == "centre":
                    offset = [static_items[-1].get_width() / 2, static_items[-1].get_height() / 2]
                else:
                    offset = [0,0]
                static_item_pos.append((item[1][4] + "-" + str(offset[0]), item[1][5]  + "-" + str(offset[1])))
                    
            elif item[0] == "Button":
                item[1] = item[1].split(",")
                Button(screen, "", PLAY_MUSIC, item[1][0], item[1][1], width=150, height=150, image=item[1][2], highlight_image=item[1][2], pressed_image=item[1][2], image_align="topleft", init_command="self.web=\"" + item[1][3].replace(";", ":") + "\"")
            
        
        # Initialise the mouse
        mouse = Mouse()
    
        finished = False
        while not finished:
            screen.fill((0, 0, 0))
            i = 0
            for item in static_items:
                x = eval(static_item_pos[i][0], {"width":screen.get_width()})
                y = eval(static_item_pos[i][1], {"height":screen.get_height()})
                screen.blit(item,(x,y))
                i+=1
                
            x, y = pygame.mouse.get_pos()
            
            buttons.update((x, y), pygame.mouse.get_pressed(), screen)
            buttons.draw(screen)
                
            screen.blit(mouse.image, (x - 5, y - 5))
            
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pass
                if event.type == pygame.KEYDOWN:
                    finished = True
                # If the window is resized then update all objects
                if event.type == pygame.VIDEORESIZE:
                    old_screen = pygame.display.get_surface()
                    screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags())
                    side.update_pos(screen)
                    play_area.update_size(screen)