Player APIs (BrightScript & Ja...
...
BrightScript
Language Reference
Example Script
1min
the following code uses gpio buttons 1, 2, 3, 4 for controls it will work on any brightsign model that has a video output and a gpio port 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 brightscript component 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 brightscript component of type/class roassociativearray game board init=gbinit ' add an entry to the aa of type rofunction 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 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 brightscript component 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 if tick count=6 then tick count=0 if m snake makelonger(m text field) then return else if m snake moveforward(m text field) then return endif endif end while end function sub gbdraw() rem rem given a rotextfield object in "m text field", draw a box around its edge rem solid=191 ' use asc(" ") if graphics not enabled m text field cls() for w=0 to m text field getwidth() 1 print #m text field,@w,chr(solid); print #m text field,@m text field getwidth() (m text field getheight() 1)+w,chr(solid); end for for h=1 to m text field getheight() 2 print #m text field,@h m text field getwidth(),chr(solid); print #m text field,@h m text field getwidth()+m text field getwidth() 1,chr(solid); end for m snake draw(m text field) end sub function gbgameover() as boolean msg$= " g a m e o v e r " msg0$=" " width = m text field getwidth() height = m text field getheight() while true print #m text field,@width (height/2 1)+(width len(msg$))/2,msg$; sleep(300) print #m text field,@width (height/2 1)+(width len(msg$))/2,msg0$; sleep(150) rem getmessage returns the message object, or an int 0 if no message available if m buttons getmessage() <> invalid then return false endwhile end function rem rem rem rem snake object rem rem rem rem rem construct a new snake brightscript object rem function newsnake(x as integer, y as integer) as object ' create aa brightscript component; the container for a "brightscript object" snake=createobject("roassociativearray") snake draw=snkdraw snake turnnorth=snkturnnorth snake turnsouth=snkturnsouth snake turneast=snkturneast snake turnwest=snkturnwest snake moveforward=snkmoveforward snake makelonger=snkmakelonger snake addsegment=snkaddsegment snake eraseendbit=snkeraseendbit rem rem a "snake" is a list of line segments rem a line segment is an roassociativearray that conains a length and direction (given by the x,y delta needed to move as it is drawn) rem snake seg list = createobject("rolist") snake addsegment(1,0,3) rem rem the x,y pos is the position of the head of the snake rem snake snake x=x snake snake y=y snake body=191 ' use asc(" ") if graphics not enabled snake dx=1 ' default snake direction / move offset snake dy=0 ' default snake direction / move offset return snake end function sub snkdraw(text field as object) x=m snake x y=m snake y for each seg in m seg list xdelta=seg xdelta ydelta=seg ydelta for j=1 to seg len text field setcursorpos(x, y) text field sendbyte(m body) x=x+xdelta y=y+ydelta end for end for end sub sub snkeraseendbit(text field as object) x=m snake x y=m snake y for each seg in m seg list x=x+seg len seg xdelta y=y+seg len seg ydelta end for text field setcursorpos(x, y) text field sendbyte(32) ' 32 is ascii space, could use asc(" ") end sub function snkmoveforward(text field as object)as boolean m eraseendbit(text field) tail=m seg list gettail() rem rem the following shows how you can use an aa's member functions to perform the same rem functions the brightscript operator does behind the scenes for you (when used on an aa) rem there is not point to this longer method other than illustration rem len=tail lookup("len") ' same as len = tail len (or tail len, brightscript syntax is not case sensative) len = len 1 if len=0 then m seg list removetail() else tail addreplace("len",len) ' same as tail len=len endif return m makelonger(text field) end function function snkmakelonger(text field as object) as boolean m snake x=m snake x+m dx m snake y=m snake y+m dy text field setcursorpos(m snake x, m snake y) if text field getvalue()=m body then return true text field sendbyte(m body) head = m seg list gethead() head len=head len+1 return false end function sub snkaddsegment(dx as integer, dy as integer, len as integer) aa=createobject("roassociativearray") aa addreplace("xdelta", dx) ' line segments draw from head to tail aa addreplace("ydelta", dy) aa addreplace("len",len) m seg list addhead(aa) end sub sub snkturnnorth() if m dx<>0 or m dy<> 1 then m dx=0\ m dy= 1\ m addsegment(m dx, m dy, 0) 'north end sub sub snkturnsouth() if m dx<>0 or m dy<>1 then m dx=0\ m dy=1\ m addsegment(m dx, m dy, 0) 'south end sub sub snkturneast() if m dx<> 1 or m dy<>0 then m dx= 1\ m dy=0\ m addsegment(m dx, m dy, 0) 'east end sub sub snkturnwest() if m dx<>1 or m dy<>0 then m dx=1\ m dy=0\ m addsegment(m dx, m dy, 0) 'west end sub