Search code examples
vbapowerpointinteractive

How can I change slide master templates using macros?


I'm currently designing a pick-your-own-adventure style game for my English students. I want to run the game in PowerPoint as it's a platform all of my students have access too. The game features a branching story in which the player collects items and loses health.

My problem:

Throughput the course of the game, players will face decisions that could result in them losing health (three red hearts displayed along the top of the slide, with hearts turning grey as health is depleted). Similarly, the players are able to collect items that should be displayed in an inventory. Both of these elements should remain persistent as the player moves through the slides.

I thought a button push which calls a macro command that alters the slide master would be the best way to proceed. Perhaps by altering the transparency of the elements or by cycling to the next slide master template. Sadly, I have no experience using VBA and I have no idea where to begin.

Can someone assist me or point me in the right direction?


Solution

  • You will have a long way to create a game, especially if you don't have any VBA experience. However, I will try to answer your question and set you on the right path.

    You don't need to exchange the master itself. You simply set the visible-property of a shape to True or False. In case of the shapes indicating the life(s), instead of setting the shape to invisible, set the transparency to 100%. The hearts will loose the red color but you can still see the outline.

    Okay, so what do you need?

    First, create your master sheet. Place all the icons for the life and the inventory you need, and give every shape (in Powerpoint, every element on a slide is a "shape") a meaningful name so that your code can identify them.
    I have created a master with 3 hearts and 4 symbols for inventory items. I set names with prefix "Icon" for all of the symbols. To name a shape, click on any of them and select "Selection Pane" (in "Arrange") of the "Shape Format" ribbon.
    It looks like this:

    enter image description here

    Now create a VBA module and put the following code in it:

    Option Explicit
    
    Dim lifes As Long
    Dim inventoryMap As Boolean
    Dim inventoryPhone As Boolean
    Dim inventoryTicket As Boolean
    Dim inventoryPen As Boolean
    
    Sub InitGame()
        lifes = 3
        inventoryMap = False
        inventoryPhone = False
        inventoryTicket = False
        inventoryPen = False
        
        ShowLifeAndInventory
    End Sub
    
    Sub ShowAll()
        lifes = 3
        inventoryMap = True
        inventoryPhone = True
        inventoryTicket = True
        inventoryPen = True
        
        ShowLifeAndInventory
    End Sub
    
    
    Sub ShowLifeAndInventory()
        Dim slide As slide
        With ActivePresentation.SlideMaster
            .Shapes("IconLife1").Fill.Transparency = IIf(lifes >= 1, 0, 1)
            .Shapes("IconLife2").Fill.Transparency = IIf(lifes >= 2, 0, 1)
            .Shapes("IconLife3").Fill.Transparency = IIf(lifes >= 3, 0, 1)
        
            .Shapes("IconMap").Visible = inventoryMap
            .Shapes("IconPen").Visible = inventoryPen
            .Shapes("IconPhone").Visible = inventoryPhone
            .Shapes("IconTicket").Visible = inventoryTicket
        End With
    End Sub
    
    Sub PickPhone()
        inventoryPhone = True
        ShowLifeAndInventory
    End Sub
    
    Sub EatApple()
        RemoveLife
    End Sub
    
    Sub RemoveLife()
        lifes = lifes - 1
        ShowLifeAndInventory
        If lifes <= 0 Then
            MsgBox "You died"
        End If
    End Sub
    

    For every inventory item and for the life counter, you have a variable in the code (at the top). Calling InitGame set them to the starting values. Calling ShowAll sets all shapes to visible, you will need that when you want to edit the master. You will need to find a way to call InitGame when your game starts, but that's beyond the scope of this answer.

    Now every time anything changes (player lost a life, picked up an item or removed it from the inventory), your code will need to set the according variable and call the routine ShowLifeAndInventory. This routine sets the visibility (or transparency) of all the shapes on the master slide.

    Now in your "game", you will need "buttons" that the user presses. Those "buttons" call a piece of code that changes the according variables (and display the change). A button can be any shape (usually rectangles, but you can use images, stars, triangles or whatever else you like). You need to create an "Action" for every shape that changes the game. The following screenshot shows a slide with two buttons and the action definition for the first one.

    Actions are defined on the "Insert menu" ("Links"). Select "Run macro" and choose the macro to be executes (here: PickPhone).

    enter image description here

    Note that those actions are executed only in presentation mode.

    The routine PickPhone sets the variable inventoryPhone to True so that the code knows that the inventory contains the phone, and calls the routine ShowLifeAndInventory to reflect the change. Similarly, assign the macro EatApple to the second button. This routine will reduce the life of the user. As there are probably many ways to loose a life, I have created a separate routine for that.

    I am aware that there are many, many more things that you need to take care about: Jumping between different slides, show or hide buttons, starting and ending a game and so on, but that is beyond this question.