beautypg.com

BrightSign BrightScript 2 Reference Guide User Manual

Page 55

background image

55

Function

gbInit()

As

Void

REM

REM button presses go to this message port

REM

m.buttons = CreateObject(

"roMessagePort"

)

m.gpio = CreateObject(

"roGpioControlPort"

)

m.gpio.SetPort(m.buttons)

REM

REM determine optimal size and position for the snake gameboard

REM

CELLWID=16

' each cell on game in pixels width

CELLHI=16

' each cell in pix height

MAXWIDE=30

' max width (in cells) of game board

MAXHI=30

' max height (in cells) of game board

vidmode=CreateObject(

"roVideoMode"

)

w=

cint

(vidmode.GetResX()/CELLWID)

if

w>MAXWIDE

then

w = MAXWIDE

h=

cint

(vidmode.GetResY()/CELLHI)

if

h>MAXHI

then

h=MAXHI


xpix =

cint

((vidmode.GetResX() - w*CELLWID)/2)

' center game board on screen

ypix =

cint

((vidmode.GetResY() - h*CELLHI)/2)

' center game board on screen

REM

REM Create Text Field with square char cell size

REM

meta=CreateObject(

"roAssociativeArray"

)

meta.AddReplace(

"CharWidth"

,CELLWID)

meta.AddReplace(

"CharHeight"

,CELLHI)

meta.AddReplace(

"BackgroundColor"

,&H202020)

'very dark grey

meta.AddReplace(

"TextColor"

,&H00FF00)

' Green

m.text_field=CreateObject(

"roTextField"

,xpix,ypix,w,h,meta)

if

type(m.text_field)<>

"roTextField"

then

print

"unable to create roTextField 1"

stop

endif

End

Function

REM
REM As Object refers to type Roku Object
REM m the "this" pointer
REM

Sub

gbSetSnake(snake

As

Object

)

m.snake=snake

End

Sub



Function

gbStartX()

As

Integer

return

cint

(m.text_field.GetWidth()/2)

End

Function



Function

gbStartY()

As

Integer

return

cint

(m.text_field.GetHeight()/2)

End

Function



Function

gbEventLoop()

As

Void


tick_count=0

while

true

msg=wait(250, m.buttons)

' wait for a button, or 250ms (1/4 a second) timeout

if

type(msg)=

"roGpioButton"

then

if

msg.GetInt()=1 m.snake.TurnNorth()

if

msg.GetInt()=2 m.snake.TurnSouth()

if

msg.GetInt()=3 m.snake.TurnEast()

if

msg.GetInt()=4 m.snake.TurnWest()

else

'here if time out happened, move snake forward

tick_count=tick_count+1