PDA

Ver la versión completa : Juego de "Tenis"



Arielo
03/10/2007, 12:37
Hola...
Aquí les traigo el código de un juego de "tenis" que acabo de hacer...
Es muy simple, también... Sólo hay que ponerle el ángulo y la velocidad inicial del tiro, para tratar de que pase por sobre la red, y no se salga de la cancha...

Podrán nota, que hay un par de variables definidas y con un valor inicial:
Viento (comienza valiendo cero)
Gravedad (comienza valiendo 9.8)
El valor de Gravedad, es el de la gravedad terrestre a nivel del mar...
El valor de Viento, puede ser cambiado para que afecte las trayectorias... Incluso, se le puede agregar un valor aleatorio en cada disparo, para que sea siempre variable, y sea más complicado para los dos jugadores. En el código no cambia de valor, pero pueden hacer que lo haga....

Este es el código:


VERSION 5.00
Begin VB.Form Form1
BorderStyle = 4 'Fixed ToolWindow
Caption = "Form1"
ClientHeight = 7170
ClientLeft = 45
ClientTop = 285
ClientWidth = 12150
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 478
ScaleMode = 3 'Pixel
ScaleWidth = 810
ShowInTaskbar = 0 'False
StartUpPosition = 2 'CenterScreen
Begin VB.TextBox txtVelocidad
Alignment = 2 'Center
BeginProperty Font
Name = "Arial"
Size = 14.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 450
Left = 6570
TabIndex = 2
Text = "90"
Top = 6412
Width = 975
End
Begin VB.TextBox txtAngulo
Alignment = 2 'Center
BeginProperty Font
Name = "Arial"
Size = 14.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 450
Left = 3630
TabIndex = 1
Text = "30"
Top = 6412
Width = 975
End
Begin VB.CommandButton cmdLanzar
Caption = "LANZAR"
Height = 465
Left = 8070
TabIndex = 0
Tag = "2"
Top = 6405
Width = 1725
End
Begin VB.Shape Shape5
BackColor = &H00E0E0E0&
BackStyle = 1 'Opaque
Height = 3135
Left = 7530
Top = -720
Width = 90
End
Begin VB.Shape Shape1
BackColor = &H0000FFFF&
BackStyle = 1 'Opaque
Height = 150
Left = 720
Shape = 3 'Circle
Top = 6060
Width = 150
End
Begin VB.Label Label3
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "MARCADOR"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 5280
TabIndex = 7
Top = 2460
Width = 1425
End
Begin VB.Label lblPtsIzq
Alignment = 2 'Center
BackColor = &H00000000&
Caption = "0"
BeginProperty Font
Name = "Arial"
Size = 48
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00C0FFC0&
Height = 1155
Left = 4200
TabIndex = 6
Top = 2760
Width = 1740
End
Begin VB.Label lblPtsDer
Alignment = 2 'Center
BackColor = &H00000000&
Caption = "0"
BeginProperty Font
Name = "Arial"
Size = 48
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00C0FFC0&
Height = 1155
Left = 6060
TabIndex = 5
Top = 2760
Width = 1740
End
Begin VB.Shape shpRed
BackColor = &H0000FFFF&
BackStyle = 1 'Opaque
Height = 1275
Left = 5955
Top = 4950
Width = 75
End
Begin VB.Label Label2
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "Velocidad:"
Height = 195
Left = 5790
TabIndex = 4
Top = 6540
Width = 750
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "Angulo:"
Height = 195
Left = 3060
TabIndex = 3
Top = 6540
Width = 540
End
Begin VB.Shape Shape2
BackColor = &H00FFC0C0&
BackStyle = 1 'Opaque
Height = 945
Left = 0
Top = 6210
Width = 12135
End
Begin VB.Shape Shape3
BackColor = &H00E0E0E0&
BackStyle = 1 'Opaque
Height = 1545
Left = 4170
Top = 2400
Width = 3660
End
Begin VB.Shape Shape4
BackColor = &H00E0E0E0&
BackStyle = 1 'Opaque
Height = 3135
Left = 4410
Top = -720
Width = 90
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdLanzar_Click()

If txtAngulo.Text > 90 Then txtAngulo.Text = 90
If txtVelocidad.Text > 150 Then txtVelocidad.Text = 150

cmdLanzar.Enabled = False
Angulo = Val(txtAngulo)
Veloc = Val(txtVelocidad)
Pi = 3.141592
Viento = 0
Gravedad = 9.8
xInicial = Shape1.Left
yInicial = Shape1.Top

Direc = cmdLanzar.Tag
Direc = 3 - Direc
cmdLanzar.Tag = Direc

angleChk = angle#: If PlayerNum = 2 Then angleChk = 180 - angleChk

Angulo = Angulo / 180 * Pi ' Convierte grados a radianes

VelXIni = Cos(Angulo) * Veloc
VelYIni = Sin(Angulo) * Veloc
AntX = Shape1.Left
AntY = Shape1.Top

Do
If Direc = 1 Then
x = Int(xInicial + (VelXIni * t) + (0.5 * (Viento / 5) * t ^ 2))
Else
x = Int(xInicial - (VelXIni * t) + (0.5 * (Viento / 5) * t ^ 2))
End If
y = yInicial + ((-1 * (VelYIni * t)) + (0.5 * Gravedad * t ^ 2)) ' * (ScrHeight / 350)

If x < 0 Then x = 0: Fuera = 1
If x > 800 Then x = 800: Fuera = 2
If (((x >= 387 And x <= 403) And y >= 330) And Direc = 1) Then
x = 387: y = 404: GoTo Red
End If
If (((x >= 397 And x <= 404) And y >= 330) And Direc = 2) Then
x = 403: y = 404: GoTo Red
End If

Shape1.Left = x: Shape1.Top = y
t = t + 0.0001

Loop Until (y >= yInicial) And (x <> xInicial)

'
' Analiza si pasó al otro lado de la cancha...
' O, si la envió afuera...
'
Red:
Shape1.Left = x: Shape1.Top = y
If Direc = 1 Then
If x < 400 Then
MsgBox "No cruzó la red... Punto para tu rival..."
lblPtsDer = Val(lblPtsDer) + 1
Recomienza = 1
End If
If Fuera = 2 Then
MsgBox "Fuera de la cancha... Punto para tu rival..."
lblPtsDer = Val(lblPtsDer) + 1
Recomienza = 1
End If
Else
If x > 402 Then
MsgBox "No cruzó la red... Punto para tu rival..."
lblPtsIzq = Val(lblPtsIzq) + 1
Recomienza = 2
End If
If Fuera = 1 Then
MsgBox "Fuera de la cancha... Punto para tu rival..."
lblPtsIzq = Val(lblPtsIzq) + 1
Recomienza = 2
End If
End If

If Recomienza = 1 Then
Shape1.Left = 750
Shape1.Top = 404
ElseIf Recomienza = 2 Then
Shape1.Left = 50
Shape1.Top = 404
End If

txtAngulo.SetFocus
cmdLanzar.Enabled = True

End Sub



Espero que les guste...
Si tienen alguna sugerencia para hacer, bienvenida será...

Saludos