TEMA 22: Programa Carreras
10. Programa: este programa es un pequeño juego, típico a las carreras de caballos de las ferias. Utilizamos la función Rnd, que se utiliza para generar números aleatorios.
| OBJETO | PROPIEDAD | VALOR |
| Form1 | BorderStyle Caption Icon |
1 Carreras Litening.ico (Graphics, Icons, Elements) |
| LblIntro | Caption Font |
Antes de comenzar elegir
un número y realizar una apuesta. Negrita |
| CmbNum | List Style |
1, 2, 3 2 |
| LblApuesta | Caption Font |
Apuesta: Negrita |
| TxtApuesta | Alignment Font MaxLength MultiLine Text |
2-Center 14 4 True - |
| CmdStart | Caption Enabled Picture Style ToolTipText |
- False Ok.ico 1 Empezar |
| TmrM | Enabled Interval |
False 100 |
| ImgCorredor (0..2) | Picture | Face03.ico, Misc27.ico, Misc04.ico (Graphics, Icons, Misc) |
| * Añadimos 4 líneas, 3 números y una etiqueta de Meta, como muestra la imagen. | ||
Dim Var As Variant
Bajate el programa completo haciendo clic en la imagen
Dim Recorrido, Corredor As Integer
Private Sub CmdStart_Click()
'Comprueba que los valores introducidos para la apuesta sean correctos
If CmbNum.Text = "" Then MsgBox "Debes elegir un número.": Exit Sub
Var = TxtApuesta.Text
If Not IsNumeric(Var) Then MsgBox "Debes apostar correctamente.": Exit Sub
If Var < 1 Then MsgBox "Elige una apuesta superior a 1.": Exit Sub
'Comienza la carrera
CmbNum.Enabled = False: TxtApuesta.Enabled = False: CmdStart.Enabled = False
TmrM.Enabled = True
End Sub
Private Sub Form_Load()
'Inicializa posiciones y números aleatorios, activa botones
Randomize Timer
CmbNum.Enabled = True: TxtApuesta.Enabled = True: TxtApuesta.Text = ""
ImgCorredor(0).Left = 360
ImgCorredor(1).Left = 360
ImgCorredor(2).Left = 360
End Sub
Private Sub TmrM_Timer()
Recorrido = Int((Rnd * 100) + 20)
Corredor = Int((Rnd * 3) + 0)
ImgCorredor(Corredor).Left = ImgCorredor(Corredor).Left + Recorrido
'Comprobación de que corredor llega a la meta primero
If ImgCorredor(Corredor).Left >= 6960 Then
If CmbNum.Text = Corredor + 1 Then
MsgBox "GANADOR" + Chr(13) + " " + Str(Corredor + 1) + Chr(13) _
+ "Has ganado: " + Str(Var * 3), vbInformation, "CARRERA"
Else
MsgBox "GANADOR" + Chr(13) + " " + Str(Corredor + 1) + Chr(13) _
+ "Has perdido: " + Str(Var), vbInformation, "CARRERA"
End If
TmrM.Enabled = False
If MsgBox("¿Desea realizar otra carrera?", vbYesNo, "CARRERA") = vbNo Then End
Form_Load 'Comienza una carrera nueva
End If
End Sub
Private Sub TxtApuesta_Change()
CmdStart.Enabled = True
End Sub
![]()