creacion de juegos en excel con macros
hola...
Este blog esta creado con el fin de enseñarles como crear un juego en excel con marcos.
el dia de hoy crearemos un PING-PONG es muy facil y divertido comenzemos...
..::Realizar el juego Ping Pong::..
Pasos a realizarPara realizar este juego se deberán de seguir un conjunto
de pasos, son lossiguientes:
1.- Dibujar el tablero
2. - Crear objetos móviles
3.- Controlar las 2 palas con el teclado
4.- Movimiento y rebotes de la bola
dibujar el tablero
Como se espera que el camp o de juego sea simétrico es necesario
hacer queel ancho de las columnas que conforman el campo sean iguales,
en este caso3 cm.Nuestro campo estará delimitado por celdas de fondo negro,
adicionalmente dibujamos una línea vertical y una circunferencia.
Ahora solo tienes que crear dos botones uno de inicio y otro de pausar o terminar el juego.
Ahora entra en visualbasic y escribe los siguientes codigos:
Public Sub ejecutar_juego()
Dim fin As Boolean, gol As Boolean
Dim velocidad_bolax As Long, velocidad_bolay As Long
Call calcular_posicion_velocidad(velocidad_bolax, velocidad_bolay)
Call posicion_inicial_juego("bola", "jugador1", "jugador2")
fin = False
While fin = False
If GetAsyncKeyState(48) Then
fin = True: gol = False
End If
If GetAsyncKeyState(76) Or GetAsyncKeyState(108) Then ’l
Call mover_pala("jugador2", 2)
End If
If GetAsyncKeyState(80) Or GetAsyncKeyState(112) Then ’p
Call mover_pala("jugador2", -2)
End If
If GetAsyncKeyState(81) Or GetAsyncKeyState(113) Then ’q
Call mover_pala("jugador1", -2)
End If
If GetAsyncKeyState(97) Or GetAsyncKeyState(65) Then ’a
Call mover_pala("jugador1", 2)
End If
If gestionar_rebotes_bola("bola", "jugador1", "jugador2", velocidad_bolax, velocidad_bolay) = True Then
fin = True: gol = True
End If
’Call gestionar_posiciones_bola("bola")
DoEvents
Call esperar(20)
Wend
DoEvents
Call posicion_inicial_juego("bola", "jugador1", "jugador2")
DoEvents
If gol Then
MsgBox "GOOOOOOOOOOOOOOL!!!!!!!!!!!!"
End If
End Sub
esto sirve para que el juego tenga funcion y para que se habra una ventana que diga gooooool.
Para que el juegopueda esperar tienes que entrar otra vez a visualbasic y ingresar el siguiente codigo:
’Espera activa
Public Sub esperar(milisegundos As Long)
Dim tiempo As Long
tiempo = GetTickCount
Do While GetTickCount - tiempo < milisegundos
DoEvents
Loop
End Sub
Este boton es para esperar para que en el juego se muevan las palas se tiene que ingresar el siguiente codigo en visualbasic :
Private Sub mover_pala(nombre_pala As String, desfase As Long)
Dim nueva_posicion As Long
nueva_posicion = ActiveSheet.Shapes(nombre_pala).Top + desfase
If nueva_posicion < CONST_PALA_EJEY_MAX And nueva_posicion > CONST_PALA_EJEY_MIN Then
ActiveSheet.Shapes(nombre_pala).Top = nueva_posicion
End If
Para mover la bola es el suiguiente codigo:
'Nueve la bola segun los desfases que reciba
Private Sub mover_bola(nombre_bola As String, desfasey As Long, desfasex As Long)
ActiveSheet.Shapes(nombre_bola).Top = ActiveSheet.Shapes(nombre_bola).Top + desfasey
ActiveSheet.Shapes(nombre_bola).Left = ActiveSheet.Shapes(nombre_bola).Left + desfasex
End Sub
Para poner la pelota donde debe de ininciar:
'Calcular posicion inicial de la bola y ponerla en el centro del tablero
'La velocidadx NUNCA puede ser 0 ya que entonces nunca podria llegar a las porterias
Private Sub calcular_posicion_velocidad(velocidad_bolax As Long, velocidad_bolay As Long)
velocidad_bolay = CInt(Rnd() * 11) - 5
velocidad_bolax = 0
While velocidad_bolax = 0
velocidad_bolax = CInt(Rnd() * 11) - 5
Wend
End Sub
Para mover la pelota con el teclado:
'para poder mover la bola con el teclado
Private Sub gestionar_posiciones_bola(nombre_bola As String)
If GetAsyncKeyState(119) Or GetAsyncKeyState(87) Then 'w
Call mover_bola(nombre_bola, -1, 0)
End If
If GetAsyncKeyState(115) Or GetAsyncKeyState(83) Then 's
Call mover_bola(nombre_bola, 1, 0)
End If
If GetAsyncKeyState(117) Or GetAsyncKeyState(85) Then 'u
Call mover_bola(nombre_bola, 0, -1)
End If
If GetAsyncKeyState(105) Or GetAsyncKeyState(73) Then 'i
Call mover_bola(nombre_bola, 0, 1)
End If
End Sub
Poner la bola en su lugar inicial:
'Poner la bola en la posicion inicial
Private Sub posicion_inicial_juego(nombre_bola As String, nombre_jug1 As String, nombre_jug2 As String)
ActiveSheet.Shapes(nombre_bola).Top = 186
ActiveSheet.Shapes(nombre_bola).Left = 317
ActiveSheet.Shapes(nombre_jug1).Top = 186
ActiveSheet.Shapes(nombre_jug2).Top = 186
End Sub
con esto se hara el juego asi que suerte por sierto "CUANDO ABRAS EL VISUAL BASIC HE INGRESES LOS CODIGOS TODOs DEBEN DE IR JUNTOS"