beautypg.com

Appendix – example script - snake – BrightSign BrightScript 2 Reference Guide User Manual

Page 54

background image

54

Appendix

– Example Script - Snake

The following code will run on any BrightSign and uses GPIO buttons 1,2,3,4 for
controls.

REM
REM The game of Snake
REM demonstrates BrightScript programming concepts
REM June 22, 2008

REM
REM Every BrightScript program must have a single Main()
REM

Sub

Main()


game_board=newGameBoard()

While

true

game_board.SetSnake(newSnake(game_board.StartX(), game_board.StartY()))
game_board.Draw()
game_board.EventLoop()

if

game_board.GameOver()

then

ExitWhile

End

While

End

Sub


REM *******************************************************
REM *******************************************************
REM *************** *********************
REM *************** GAME BOARD OBJECT *********************
REM *************** *********************
REM *******************************************************
REM *******************************************************


REM
REM An example BrightScript constructor. "newGameBoard()" is regular Function of module scope
REM BrightScript Objects are "dynamic" and created at runtime. They have no "class".
REM The object container is a Roku Object of type roAssocitiveArray (AA).
REM The AA is used to hold member data and member functions.
REM

Function

newGameBoard()

As

Object

game_board=CreateObject(

"roAssociativeArray"

)

' Create a Roku Object of type/class

roAssociativeArray

game_board.Init=gbInit

' Add an entry to the AA of type roBrSub with value

gbDraw (a sub defined in this module)

game_board.Draw=gbDraw
game_board.SetSnake=gbSetSnake
game_board.EventLoop=gbEventLoop
game_board.GameOver=gbGameOver
game_board.StartX=gbStartX
game_board.StartY=gbStartY
game_board.Init()

' Call the Init member function (which is

gbInit)

return

game_board

End

Function


REM
REM gbInit() is a member function of the game_board BrightScript Object.
REM When it is called, the "this" pointer "m" is set to the appropriate instance by
REM the BrightScript bytecode interpreter
REM