Results 1 to 5 of 5

Thread: How to make a random image generator that creates 2 images VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    3

    Question How to make a random image generator that creates 2 images VBA

    I'm doing a final project in action researh.I want to create a game that can improve students' speaking skills.

    I have a PowerPoint slide with a total of 57 different images. I want to generate VBA code in PowerPoint that pressing a button or clicking on an object will randomly run two images at a time for a total of 56 images.but it randomly only get one images at a time. I don't know where it's wrong. I am new to VBA and trying to learn. Thank you.
    Code:
    Sub Shape_Click()
        Set myShape = ActivePresentation.Slides(1).Shapes(1)
        myShape.ActionSettings(ppMouseClick).Action = ppActionLastSlide
        myShape.ActionSettings(ppMouseOver).SoundEffect.Name = "applause"
    End Sub
    
    Sub RandomImage()
         Dim i As Long
         Dim posLeft As Long
         For i = 1 To 2
         Randomize
         RanNum% = Int(57 * Rnd) + 1
         Path$ = ActivePresentation.Path
         FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".png"
         posLeft = 100 + ((i - 1) * 510)
         ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, 
         LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=posLeft, Top:=100, 
         Width:=500).Select
    Next
    End Sub
    Last edited by Shaggy Hiker; May 9th, 2022 at 08:43 AM. Reason: Added CODE tags.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to make a random image generator that creates 2 images VBA

    You shouldn't have Randomize in the loop.
    Randomize should be only used once at the start of the program and never called again.

    Since Randomize will seed the rnd function using the current clock value, when you call it two times in a row in a tight loop like that, the clock hasn't changed so you're using the same seed twice in a row and getting the same value from the rnd function that is generated from that seed.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    3

    Re: How to make a random image generator that creates 2 images VBA

    Thank you for the advice But I wanted to create a game that's similar to Spot it(Doodle), where you press a button and create an order to randomly output two images and have students find and say the names of the letters that are the same in both images. But I don't know what to do. Would you recommended what I Could do ?
    I also uploaded the video in this link. ↓ 
    https://drive.google.com/drive/folde...1Z?usp=sharing

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to make a random image generator that creates 2 images VBA

    I don't understand the requirements for your version of the game.
    Do you want to display a random pair from your list of 57 images?
    Then after user interaction, pick another random pair from your images, not repeating the random pair you've previously picked, i.e. so that once you've gone through 56 random unique images i.e. 26 random pairs, the game is over, or you reset to your original 57 images and start picking random pairs again?

    Or do you want to replicate the game, so you don't want to create two images, but rather two groups of images like the Spot it! game does, i.e. that game creates two groups of eight images, and you identify the one image that is common between the two groups?

    If you were trying to replicate the game, then I would think you would pick 14 unique random images from your list of images, and then display seven of the images in random spots in each group of eight. You would then pick a 15th unique rand image, and display that one in both groups in the last remaining spot of each group.

    I'm not fluent in VBA, so don't know how flexible the user interaction is with the Power Point Presentation, or IDE.
    From what you've posted, it seems like the game may be played in the IDE, not in a Presentation. Either way, it is new to me so I'm not sure how to advise you. I would think if I understood what you wanted the game to do, I could suggest code that could maintain the images and their selection, but presentation and interaction with those images in Power Point is beyond my scope.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    3

    Re: How to make a random image generator that creates 2 images VBA

    Thank you
    [Do you want to display a random pair from your list of 57 images?
    Then after user interaction, pick another random pair from your images, not repeating the random pair you've previously picked, i.e. so that once you've gone through 56 random unique images i.e. 26 random pairs, the game is over, or you reset to your original 57 images and start picking random pairs again?]

    Yes, I want to do that

    Now I can randomize two image in one click

    This sub I use
    Code:
    Sub RandomImage()
    
    Dim i As Long
    Dim posLeft As Long
      
    For i = 1 To 2
    
        Randomize
      
        RanNum% = Int(57 * Rnd) + 1
      
        Path$ = ActivePresentation.Path
      
        FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".png"
      
        posLeft = 100 + ((i-1) * 510)   '-->>> you will have to adjust this to your needs
        
        ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
            Left:=posLeft, Top:=100, Width:=500).Select
    Next
    
    End Sub
    Last edited by Shaggy Hiker; May 17th, 2022 at 08:23 AM. Reason: Added CODE tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width