Page 1 of 2

Snake example in a console window (Updated for PB 4.50)

Posted: Mon May 23, 2005 8:32 pm
by Joakim Christiansen
Snake example in a console window.
Copy, paste and play! :D

Code: Select all

; --------------------------------
; Title:    Snake Example
; Version:  1.0
; Author:   Joakim L. Christiansen
; --------------------------------

EnableExplicit
EnableGraphicalConsole(1)

Structure Body
  X.b
  Y.b
EndStructure
Global NewList Body.Body()
Global Lenght.w   : Lenght = 5
Global Direction.b: Direction = 3
Global X.b        : X = 10
Global Y.b        : Y = 12
Global AppleX.b, AppleY.b, Score.w

Procedure DrawLevel()
  Protected X.b, Y.b
  ;Draw score
  ConsoleColor(7,0)
  ConsoleLocate(0,0)
  Print(" Score: 0")
  ;Draw wall
  ConsoleColor(1,1)
  For X = 2 To 77
    ConsoleLocate(X,1)
    Print(" ")
    ConsoleLocate(X,23)
    Print(" ")
  Next
  For Y = 1 To 23
    ConsoleLocate(1,Y)
    Print(" ")
    ConsoleLocate(78,Y)
    Print(" ")
  Next
EndProcedure
Procedure SpawnApple()
  Protected CollisionWithBody.b
  Repeat
    AppleX = Random(74)+3
    AppleY = Random(19)+3
    CollisionWithBody = #False
    ForEach Body()
      If AppleX = Body()\X And AppleY = Body()\Y
        CollisionWithBody = #True
        Break
      EndIf
    Next
  Until CollisionWithBody = #False
  ;Draw apple
  ConsoleColor(2,2)
  ConsoleLocate(AppleX,AppleY)
  Print(" ")
EndProcedure
Procedure GameRestart()
  Score = 0
  Lenght = 5
  Direction = 3
  X = 10
  Y = 12
  ClearList(Body())
  ClearConsole()
  DrawLevel()
  SpawnApple()
EndProcedure
Procedure.b GameOver()
  Protected Input.s
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Beep_(800,200)
    Beep_(600,200)
    Beep_(400,100)
  CompilerEndIf
  ConsoleColor(7,0)
  ClearConsole()
  PrintN("Game Over!")
  PrintN("Your score was: "+Str(Score))
  PrintN("Press ENTER to quit or R to restart.")
  Input = Input()
  If Input = "R" Or Input = "r"
    ProcedureReturn #False
  Else
    ProcedureReturn #True
  EndIf
EndProcedure

OpenConsole()
ConsoleCursor(0)
ConsoleTitle("Snake Example")
DrawLevel()
SpawnApple()

Repeat
  Delay(100)
  
  ;-User input
  Inkey()
  Select RawKey()
    Case 38: If Direction<>1: Direction=0: EndIf
    Case 40: If Direction<>0: Direction=1: EndIf
    Case 37: If Direction<>3: Direction=2: EndIf
    Case 39: If Direction<>2: Direction=3: EndIf
  EndSelect
  
  ;-Collision with apple
  If X = AppleX And Y = AppleY
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      Beep_(800,10)
    CompilerEndIf
    Score + 1
    Lenght + 1
    SpawnApple()
    ;Draw score
    ConsoleColor(7,0)
    ConsoleLocate(8,0)
    Print(Str(Score))
  EndIf
  
  ;-Add body
  ResetList(Body())
  AddElement(Body())
  Body()\X = X
  Body()\Y = Y
  ;Draw body
  ConsoleColor(4,4)
  ConsoleLocate(Body()\X,Body()\Y)
  Print(" ")
  
  ;-Delete body
  If ListSize(Body())-1 = Lenght
    SelectElement(Body(),Lenght)
    ConsoleColor(12,0)
    ConsoleLocate(Body()\X,Body()\Y)
    Print(" ")
    DeleteElement(Body())
  EndIf
  
  ;-Move in direction
  Select Direction
    Case 0: Y - 1
    Case 1: Y + 1
    Case 2: X - 1
    Case 3: X + 1
  EndSelect
  ;Draw head
  ConsoleColor(12,12)
  ConsoleLocate(X,Y)
  Print(" ")
  
  ;-Collision with wall
  If X < 2 Or X > 77 Or Y < 2 Or Y > 22
    If GameOver()
      Break
    Else
      GameRestart()
    EndIf
  EndIf
  
  ;-Collision with body
  ForEach Body()
    If Body()\X = X And Body()\Y = Y
      If GameOver()
        Break 2
      Else
        GameRestart()
      EndIf
    EndIf
  Next
Until RawKey() = 27

Posted: Mon May 23, 2005 8:44 pm
by Kale
lol, nice! :twisted:

Re: Snake clone that runs in a console window. :lol:

Posted: Mon May 23, 2005 9:51 pm
by traumatic
Joakim Christiansen wrote:How can I make a beep in a console window without using windows api?
AFAIK that's not possible.

There's the ASCII-bell (chr 7) but that doesn't work with PB's
console-mode and Print().

Here's what I mean, I'm sorry API is still required...

Code: Select all

hCon = OpenConsole()
WriteConsole_(hCon, Chr(7), 1, #NULL, #NULL)

Posted: Wed May 25, 2005 11:50 am
by gpetersz
Great!

Posted: Wed May 25, 2005 12:37 pm
by Fred
Nice little demo, can we include it in the PB package as example ?

Posted: Wed May 25, 2005 1:06 pm
by Joakim Christiansen
That would be great Fred :)

Posted: Wed May 25, 2005 1:28 pm
by GeoTrail
Very impressive, runs smoothly here. Fun little game, well worth getting a place in PB's example package ;)
btw, I got 43 on my first test hehe

Posted: Wed May 25, 2005 1:52 pm
by Joakim Christiansen
Thanks GeoTrail :D

Posted: Thu May 26, 2005 11:38 am
by Sektor_Z
Great. :)

Posted: Tue Jun 07, 2005 1:04 pm
by Hatonastick
Oh I kept forgetting to mention that you really should put:
ConsoleCursor(0)
After your OpenConsole() command to get rid of the cursor flickering on and off down the bottom left of the console. :)

Posted: Tue Jun 07, 2005 6:57 pm
by Droopy
Very good 8)

Posted: Wed Jun 08, 2005 2:42 am
by Joakim Christiansen
Hatonastick wrote:Oh I kept forgetting to mention that you really should put:
ConsoleCursor(0)
After your OpenConsole() command to get rid of the cursor flickering on and off down the bottom left of the console. :)
Thanks for the tip, I updated it now.

Posted: Wed Jun 08, 2005 8:48 am
by Hatonastick
You are welcome. :) I like this game a lot btw although I think I already said that.

Posted: Sun Aug 07, 2005 6:34 pm
by Joakim Christiansen
I updated the DrawLevel() procedure, it's 9 lines shorter now. :lol:
I also removed CloseConsole(), because PureBasic does this automatically when the program ends.

Posted: Thu Aug 11, 2005 8:56 pm
by merendo
Very cool game, I like it :D Well done!!