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

Advanced game related topics
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

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

Post 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
Last edited by Joakim Christiansen on Mon Sep 13, 2010 11:16 pm, edited 14 times in total.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

lol, nice! :twisted:
--Kale

Image
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

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

Post 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)
Good programmers don't comment their code. It was hard to write, should be hard to read.
gpetersz
User
User
Posts: 39
Joined: Fri May 20, 2005 11:24 am
Location: Budapest, Hungary
Contact:

Post by gpetersz »

Great!
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Nice little demo, can we include it in the PB package as example ?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

That would be great Fred :)
I like logic, hence I dislike humans but love computers.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post 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
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Thanks GeoTrail :D
Last edited by Joakim Christiansen on Thu May 26, 2005 7:08 pm, edited 1 time in total.
I like logic, hence I dislike humans but love computers.
Sektor_Z
New User
New User
Posts: 2
Joined: Wed May 25, 2005 8:18 pm
Location: Bulgaria
Contact:

Post by Sektor_Z »

Great. :)
Not all idiots are annoying... some are dead.
Image
Hatonastick
Enthusiast
Enthusiast
Posts: 149
Joined: Wed Apr 27, 2005 11:50 am
Location: Adelaide, Australia
Contact:

Post 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. :)
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Very good 8)
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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.
I like logic, hence I dislike humans but love computers.
Hatonastick
Enthusiast
Enthusiast
Posts: 149
Joined: Wed Apr 27, 2005 11:50 am
Location: Adelaide, Australia
Contact:

Post by Hatonastick »

You are welcome. :) I like this game a lot btw although I think I already said that.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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.
I like logic, hence I dislike humans but love computers.
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Post by merendo »

Very cool game, I like it :D Well done!!
The truth is never confined to a single number - especially scientific truth!
Post Reply