; --------------------------------
; 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.
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.
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.
I updated the DrawLevel() procedure, it's 9 lines shorter now.
I also removed CloseConsole(), because PureBasic does this automatically when the program ends.
I like logic, hence I dislike humans but love computers.