![[Imagen: q817Q94.png]](https://i.imgur.com/q817Q94.png)

' Gambas class file
Const MENSAJE As String = "Haz clic en la imagen del animal que hace ese soindo"
Private canimales As New Collection
Private rutaimagenes As String
Private rutasonidos As String
Private sonidoerror As Sound
Private aplausos As Sound
Private adivinaanimal As String
Public Sub Form_Open()
  Label1.text = MENSAJE
  Me.Text = "Adivina el animal"
 
  rutaimagenes = Application.Path &/ "Imagenes/"
  rutasonidos = Application.Path &/ "Sonidos/"
  sonidoerror = Sound.Load(Application.Path &/ "Sonidos/error.ogg")
  aplausos = Sound.Load(Application.Path &/ "Sonidos/aplausosfin.ogg")
  CreaAnimales()
  Randomize
  DistribuirAnimales
  Timer1.Delay = 3000
  Panel1.Enabled = False
End
Public Sub CreaAnimales()
  CreaObjetoAnimal(rutasonidos, rutaimagenes, "01_cat.png", "gato.wav", "gato")
  CreaObjetoAnimal(rutasonidos, rutaimagenes, "02_pig.png", "cerdo.ogg", "cerdo")
  CreaObjetoAnimal(rutasonidos, rutaimagenes, "04_hippopotamus.png", "hipopotamo.ogg", "hipopotamo")
  CreaObjetoAnimal(rutasonidos, rutaimagenes, "10_chicken.png", "gallina.ogg", "gallina")
End
Public Sub CreaObjetoAnimal(rutasonidos As String, rutaimagenes As String, dibujo As String, sonido As String, nombre As String)
  Dim animal As New Animal
  With animal
    .dibujo = Picture.Load(rutaimagenes & dibujo)
    .sonido = rutasonidos & sonido
    .nombre = nombre
  End With
  canimales.Add(animal, animal.nombre)
End
Public Function DevolverListaNombres(clista As Collection) As String[]
  Dim lista As New String[]
  Dim animal As Animal
  For Each animal In clista
    lista.Add(animal.nombre)
  Next
  Return lista
End
Public Sub DistribuirAnimales()
  Dim unpicturebox As PictureBox
  Dim aanimales As String[]
  Dim azar As Integer
  Dim fondo As Integer[] = [Color.red, Color.green, Color.Blue, Color.Yellow]
  aanimales = DevolverListaNombres(canimales)
  For Each unpicturebox In Panel1.Children
    If unpicturebox Is PictureBox Then
      azar = Rand(aanimales.Max)
      With unpicturebox
        .Mode = PictureBox.Fill
        .Tag = aanimales[azar]
        .Picture = canimales[aanimales[azar]].dibujo
        .Background = fondo[azar]
      End With
      aanimales.Remove(azar)
      fondo.Remove(azar)
    Endif
  Next
  Panel1.Enabled = False
 
End
Public Sub ImagenesDeAnimales_MouseDown()
  If Last.tag = adivinaanimal Then
    Label1.Text = "Correcto!"
    aplausos.Play()
  Else
    Label1.Text = "Incorrecto!"
    sonidoerror.Play()
  Endif
  Panel1.Enabled = False
  Timer1.Start()
   
End
Public Sub btnReproduce_Click()
  Dim lista As String[]
  Dim azar As Integer
  Panel1.Enabled = True  
  lista = DevolverListaNombres(canimales)
  azar = Rand(lista.Max)
  adivinaanimal = lista[azar]
  canimales[adivinaanimal].ReproducirSonido()
End
Public Sub Timer1_Timer()
  Label1.Text = MENSAJE
  DistribuirAnimales()
  Timer1.Stop
EndProperty nombre As String Use $nombre
Property sonido As String Use $sonido
Property dibujo As Picture Use $dibujo
Public Sub ReproducirSonido()
 
  Dim ssonido As Sound
 
  ssonido = Sound.Load(Me.sonido)
  ssonido.Play()
 
End(23-06-2023, 09:53)Shordi escribió: Sorry, nunca he usado ese componente. Prueba a soslayar el problema con un "Bieeennn", que dure un segundo. Así no se te montan... supongo.
 

(23-06-2023, 11:16)tincho escribió: Shell, tienes que cargar el archivo a reproducir cada vez que el archivo sea dirferente.
Public Sub Form_Open()
  Label1.text = MENSAJE
  Me.Text = "Adivina el animal"
 
  rutaimagenes = Application.Path &/ "Imagenes/"
  rutasonidos = Application.Path &/ "Sonidos/"
  sonidoerror = Sound.Load(Application.Path &/ "Sonidos/error.ogg")
  aplausos = Sound.Load(Application.Path &/ "Sonidos/aplausosfin.ogg")
  CreaAnimales()
  Randomize
  DistribuirAnimales
  'En el canal1 se va a reproducir los sonidos de los animales y el de error
  'En el canal2 se va a reproducir los aplausos
 
  Channels.Count = 2
  canal1 = Channels[0]
  canal2 = Channels[1]
 
  Timer1.Delay = 3000
  Panel1.Enabled = False
EndPublic Sub ImagenesDeAnimales_MouseDown()
  Panel1.Enabled = True
  If Last.tag = adivinaanimal Then
    Label1.Text = "Correcto!"
    canal1.Stop
    canal2.Play(aplausos)    
  Else
    Label1.Text = "Incorrecto!"
    canal1.Play(sonidoerror)
  Endif
 
  Timer1.Start()
   
EndPublic Sub btnReproduce_Click()
  Dim lista As String[]
  Dim azar As Integer
  Panel1.Enabled = True  
  lista = DevolverListaNombres(canimales)
  azar = Rand(lista.Max)
  adivinaanimal = lista[azar]
  canimales[adivinaanimal].ReproducirSonido(canal1, canal2)
   
EndProperty nombre As String Use $nombre
Property sonido As String Use $sonido
Property dibujo As Picture Use $dibujo
Public Sub ReproducirSonido(c1 As Channel, c2 As Channel)
 
  Dim ssonido As Sound
 
  ssonido = Sound.Load(Me.sonido)
 
  'Paramos el canal de los aplausos  
  c2.Stop
  'Hacemos sonar el canal dedicado a los sonidos de los animales y para el sonido de error
  c1.Play(ssonido)  
   
End
		
	Public Sub ImagenesDeAnimales_MouseDown()
  Panel1.Enabled = True
  If Last.tag = adivinaanimal Then
    Label1.Text = "Correcto!"
    canal1.Stop
    canal2.Play(aplausos)
  Else
    Label1.Text = "Incorrecto!"
    canal1.Play(sonidoerror)
  Endif
  Panel1.Enabled = False
 
  Timer1.Start()
   
EndPublic Sub btnReproduce_Click()
  Dim lista As String[]
  Dim azar As Integer
 
  lista = DevolverListaNombres(canimales)
  azar = Rand(lista.Max)
  adivinaanimal = lista[azar]
  canimales[adivinaanimal].ReproducirSonido(canal1, canal2)
  Panel1.Enabled = True     
End