

 This page uses content from Wikipedia. The original article was at Snake_(video_game). The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)


Snake is a game where the player maneuvers a line which grows in length every time the snake reaches a food source.



Task
Implement a variant of the Snake game, in any interactive environment, in which a sole player attempts to eat items by running into them with the head of the snake.

Each item eaten makes the snake longer and a new item is randomly generated somewhere else on the plane.

The game ends when the snake attempts to eat himself.




 
REM Snake
 
#define EXTCHAR Chr(255)
 
Dim Shared As Integer puntos, contar, longitud, posX, posY, oldhi = 0
Dim Shared As Integer x(500), y(500)
For contar = 1 To 500
    x(contar) = 0 : y(contar) = 0
Next contar
Dim Shared As Byte fruitX, fruitY, convida
Dim Shared As Single delay
Dim Shared As String direccion, usuario
 
Sub Intro
    Cls
    Color 15, 0
    Print "              _    _    _    _               "
    Print "             / \  / \  / \  / \              "
    Print " ___________/  _\/  _\/  _\/  _\____________ "
    Print " __________/  /_/  /_/  /_/  /______________ "
    Print "           | /\   /\   /\   / \ \___         "
    Print "           |/  \_/  \_/  \_/   \   "+Chr(248)+"\        "
    Print "                                \___/--<     "
    Color 14, 0
    Locate 10, 28: Print "---SNAKE---"
    Locate 12, 4: Print "Para jugar, usa las teclas de flecha para moverte."
    Locate 13, 4: Print "Pulsa <1-3> para velocidad, o <Esc> para salir."
    If puntos > oldhi Then oldhi = puntos
    Locate 14, 4: Print "M xima puntuaci¢n: "; oldhi
    usuario = ""
    While usuario = ""
        usuario = Inkey
    Wend
    If usuario = Chr(27) Then End 'ESC
    delay = .14
    If usuario = "1" Then delay = .26
    If usuario = "3" Then delay = .08
    Cls
    longitud = 9
    puntos = 0
    posX = 40 : posY = 10
    direccion = "derecha"
    convida = true
    fruitX = Int(Rnd * 79) + 1
    fruitY = Int(Rnd * 20) + 1
    Locate 22, 1
    For contar = 1 To 80
        Print Chr(196); ''"-";
    Next contar
End Sub
 
Sub MenuPrincipal
    Dim As Integer num, oldX, oldY
    Dim As Single tm, tm2
 
    Color 10
    Locate 23, 2: Print "SNAKE - <Esc> salir - Puntos: "; puntos
    If posX = fruitX And posY = fruitY Then
        longitud += 1
        puntos += 1
        fruitX = Int(Rnd * 79) + 1
        fruitY = Int(Rnd * 21) + 1
    End If
    Locate fruitY, fruitX : Color 12: Print Chr(01) : Color 10'"@"
    x(0) = posX : y(0) = posY
 
    For contar = 1 To 499
        num = 500 - contar
        x(num) = x(num - 1)
        y(num) = y(num - 1)
    Next contar
 
    oldX = x(longitud) : oldY = y(longitud)
    If oldX > 0 And oldY > 0 Then Locate oldY, oldX : Print " "
 
    Locate posY, posX: Print Chr(219) '"Û"
    tm = Timer
    tm2 = tm + delay
    While tm < tm2
        tm = Timer
        usuario = Inkey
        If usuario = EXTCHAR & "H" Then direccion = "arriba"
        If usuario = EXTCHAR & "P" Then direccion = "abajo"
        If usuario = EXTCHAR & "K" Then direccion = "izquierda"
        If usuario = EXTCHAR & "M" Then direccion = "derecha"
        If usuario = Chr(27) Then Intro
    Wend
    If direccion = "derecha" Then posX += 1
    If posX > 80 Then convida = false
    If direccion = "izquierda" Then posX -= 1
    If posX < 1 Then convida = false
    If direccion = "arriba" Then posY -= 1
    If posY < 1 Then convida = false
    If direccion = "abajo" Then posY += 1
    If posY > 21 Then convida = false
 
    For contar = 0 To longitud
        If posX = x(contar) And posY = y(contar) Then convida = false
    Next contar
 
    If convida = false Then
        Cls : Locate 11, 19: Print "Pulsa <space>..."
        Locate 10, 18: Print "Has muerto! Con"; puntos; " puntos." : Sleep
        While Inkey = "": Wend
        Intro
    End If
    MenuPrincipal
End Sub
 
'--- Programa Principal ---
Randomize Timer
Intro
MenuPrincipal
End
'--------------------------
 