.

Oct 03 2007

El juego de la serpiente en vb

Published by Webmaster at 12:27 pm under vb

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Necesitamos un Shape y un Timer.

serpiente-vb.jpg

Primero Algunas variables

Visual Basic:
  1. Public a As Boolean
  2. Public b As Boolean
  3. Public c As Boolean
  4. Public d As Boolean
  5. Public q As Integer
  6. Public z As Integer
  7. Public y As Integer
  8. Public x As Integer
  9. Public t As Integer
  10. Public Val As Boolean

Configuramos las teclas con las que se mueve.

Visual Basic:
  1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  2. Select Case KeyCode
  3.         Case vbKeyLeft
  4.             If b = True Then
  5.                 Exit Sub
  6.             End If
  7.             a = True
  8.             b = False
  9.             c = False
  10.             d = False
  11.         Case vbKeyRight
  12.             If a = True Then
  13.                 Exit Sub
  14.             End If
  15.             b = True
  16.             a = False
  17.             c = False
  18.             d = False
  19.         Case vbKeyDown
  20.             If d = True Then
  21.                 Exit Sub
  22.             End If
  23.             c = True
  24.             b = False
  25.             a = False
  26.             d = False
  27.         Case vbKeyUp
  28.             If c = True Then
  29.                 Exit Sub
  30.             End If
  31.             d = True
  32.             b = False
  33.             c = False
  34.             a = False
  35.         Case Else
  36.         Exit Sub
  37. End Select
  38. End Sub

Configuramos el movimiento

Visual Basic:
  1. Private Sub Timer1_Timer()
  2. If Val = True Then
  3.     z = q
  4.     Do Until q = 0
  5.         Shape1(q).Top = Shape1(q - 1).Top
  6.         Shape1(q).Left = Shape1(q - 1).Left
  7.         q = q - 1
  8.     Loop
  9.     q = z
  10. End If
  11. If a = True Then
  12.     Shape1(0).Left = Shape1(0).Left - 240
  13. End If
  14. If b = True Then
  15.     Shape1(0).Left = Shape1(0).Left + 240
  16. End If
  17. If c = True Then
  18.     Shape1(0).Top = Shape1(0).Top + 240
  19. End If
  20. If d = True Then
  21.     Shape1(0).Top = Shape1(0).Top - 240
  22. End If
  23. If Val = True Then
  24.     For k = 1 To q
  25.         If Shape1(0).Top = Shape1(k).Top And Shape1(0).Left = Shape1(k).Left Then
  26.             End
  27.         End If
  28.     Next
  29. End If
  30. If Shape1(0).Left = Shape1(t).Left And Shape1(0).Top = Shape1(t).Top Then
  31.     Val = True
  32.     t = t + 1
  33.     q = t
  34.     q = q - 1
  35.     Call cargar
  36. End If
  37. If Shape1(0).Top> (Me.ScaleHeight - 255) Or Shape1(0).Top <0 Or Shape1(0).Left> (Me.ScaleWidth - 255) Or Shape1(0).Left <0 Then
  38.     End
  39. End If
  40. End Sub

Colocamos todo en su lugar

Visual Basic:
  1. Private Sub cargar()
  2.     Shape1(0).Width = 255
  3.     Shape1(0).Height = 255
  4.     Randomize
  5.     y = 1
  6.     x = 1
  7.     Do Until (y Mod 240) = 0 And y> 0 And y <(Me.ScaleHeight - 255) And (x Mod 240) = 0 And x> 0 And x <(Me.ScaleWidth - 255)
  8.     y = Int(Rnd() * (Me.ScaleHeight - 1) + 1)
  9.     x = Int(Rnd() * (Me.ScaleWidth - 1) + 1)
  10.     Loop
  11.     Load Shape1(t)
  12.     Shape1(t).Top = y
  13.     Shape1(t).Left = x
  14.     Shape1(t).Visible = True
  15.     Me.Refresh
  16. End Sub

Finalmente Cargamos

Visual Basic:
  1. Private Sub Form_Load()
  2.     t = 1
  3.     Call cargar
  4. End Sub


 

    Share This     Te lleva a una entrada aleatoria

 

Trackback URI | Comments RSS

Deja un comentario