Simple Little Console Game

Share your advanced PureBasic knowledge/code with the community.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Simple Little Console Game

Post by michaeled314 »

Code: Select all

;Originally written for the TI-83 Plus
;Adapted for PureBasic console

;-Variables
score.l ;player's score (how many 'X's you have caught)
Escape.l ;tells whether user has hit escape (exit the game)
enemyx.l ;the x console position of the enemy 'X' that is currently falling
enemyy.l ;the y console position of the enemy 'X' that is currently falling
XsMissed.l = 0;number of X's the player missed: 3=game over
playerx.l = 39 ;the x console position of the player
playery.l = 23;the y console position of the player
Dim enemyxpoints.l(3) ;an array of the x console positions of the fallen points at which the player did not catch the 'X'
Dim enemyypoints.l(3) ;an array of the y console positions of the fallen points at which the player did not catch the 'X'
RandomSeed(ElapsedMilliseconds()) ;Randomize the random seed

If OpenConsole() And InitKeyboard()
 EnableGraphicalConsole(#True)
 ConsoleCursor(0)
 ConsoleTitle("Catch The X's")
 
 For a = 0 To 79
  ConsoleColor(9,0)
  ConsoleLocate(a,0)
  Print("B")
  ConsoleLocate(a,24)
  Print("B")
 Next
 
 For a = 1 To 23
  ConsoleLocate(0,a)
  Print("B")
  ConsoleLocate(79,a)
  Print("B")
 Next
 
 a = 0
 
 ConsoleColor(10,0)
 ConsoleLocate(playerx,playery)
 Print("P")
 
 enemyx = Random(77)+1
 enemyy = 1
 
 Repeat
  a = a + 1
  Escape = GetAsyncKeyState_(#VK_ESCAPE)
  Pause = GetAsyncKeyState_(#VK_PAUSE)
  Left = GetAsyncKeyState_(#VK_LEFT)
  Right = GetAsyncKeyState_(#VK_RIGHT)
  
  If enemyy = 23
   ConsoleColor(10,0)
   ConsoleLocate(playerx,playery)
   Print("P")
   If enemyx = playerx And enemyy = playery
    score = score + 1
    ConsoleColor(13,0)
    ConsoleLocate(0,0)
    Print("Score: "+Str(score))
   Else
    XsMissed = XsMissed + 1
    enemyxpoints(XsMissed) = enemyx
    enemyypoints(XsMissed) = enemyy
   EndIf
   enemyx = Random(77)+1
   enemyy = 1
  ElseIf enemyy < 23 And Not a % 2
   ConsoleLocate(enemyx,enemyy)
   Print(" ")
   enemyy = enemyy + 1
   ConsoleColor(12,0)
   ConsoleLocate(enemyx,enemyy)
   Print("X")
  EndIf
  
  If XsMissed
   ConsoleColor(12,0)
   For x = 1 To XsMissed
    ConsoleLocate(enemyxpoints(x),enemyypoints(x))
    Print("X")
   Next
  EndIf
  
  If XsMissed = 3
   End
  EndIf
  
  
  If (Left <> 0 Or Right <> 0) And (Not (Left <> 0 And Right <> 0))
   ConsoleLocate(playerx,playery)
   Print(" ")
  EndIf
  
  If Left <> 0 And Not Right
   playerx = playerx-1
   If playerx = 0
    playerx = 78
   EndIf
   ConsoleColor(10,0)
   ConsoleLocate(playerx,playery)
   Print("P")
  EndIf
  
  If Right <> 0 And Not Left
   playerx = playerx+1
   If playerx = 79
    playerx = 1
   EndIf
   ConsoleColor(10,0)
   ConsoleLocate(playerx,playery)
   Print("P")
  EndIf
  
  Delay(50)
 Until Escape <> 0
EndIf
Enjoy!
Last edited by michaeled314 on Sun Jan 19, 2020 9:54 am, edited 2 times in total.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

The game was originally coded in TI-BASIC for the TI-83 Plus
Last edited by michaeled314 on Thu Oct 06, 2011 10:30 pm, edited 1 time in total.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Oh great, another way to waste my time instead of programming.

Easy when I adjust the delay :wink:

Thanks for sharing.

cheers
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

Try getting rid of the delay and see how fast it runs. P.S. Feel the Pure Power
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Nice little game :).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Neat. :)
Dare2 cut down to size
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Simple Little Console Game

Post by Joakim Christiansen »

Nice!

Reminds me of the one I wrote some years ago:

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
I like logic, hence I dislike humans but love computers.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Simple Little Console Game

Post by netmaestro »

SpawnApple()
What a totally cool name for a procedure! 8)

(doesn't take much to amuse me, does it? :lol: )
BERESHEIT
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Simple Little Console Game

Post by Joakim Christiansen »

netmaestro wrote:
SpawnApple()
What a totally cool name for a procedure! 8)

(doesn't take much to amuse me, does it? :lol: )
Thanks! :)
I could also have used the word "materialize" (I do enjoy Star Trek) but I ended up with "spawn" since I play too much online video games :lol:
I like logic, hence I dislike humans but love computers.
Post Reply