Author Topic: Sample Programs for BrowserBasic  (Read 9074 times)

Rick3137

  • Guest
Sample Programs for BrowserBasic
« on: July 09, 2013, 08:13:58 PM »
     This first program is a simple Game Board Demo.

 
Code: [Select]
'    Game Board Demo by Rick3137    http://rb23.yolasite.com/


Var btn as string
Var mx as number
Var my as number
Var column as number
Var row as number


Function OnLoad()
setBackgroundColor( 0, 0, 40)
EndFunction

Function OnDraw()

setColor( 255, 255, 255 )
Print( Str$( mx ) , 20, 100)
Print( Str$( my ) , 20, 120)
Print( Str$( column ) , 20, 140)
Print( Str$( row ) , 20, 160)
    setColor ( 50 , 50 , 100 )
FillRectangle ( 90 , 0, 520, 520)
    setColor ( 90 , 90 , 150 )
FillRectangle ( 100 , 10, 500, 500)
Squares()

EndFunction

Function Squares()
Var x as number
    Var y as number
Var x2 as number
    Var y2 as number
Var x3 as number
    Var y3 as number
setColor ( 255 , 255 , 255 )
    x=100
y=10   
x2 = x+2
y2 = y+3
x3 = x+48
y3 = y+48
For a = 1 to 100     
StrokeRectangle ( x , y, 50, 50)
for b = 1 to 2
setColor ( 0 , 0 , 0 )
Line(x2,y2,x3,y2)
Line(x3,y2,x3,y3)
setColor ( 255 , 255 , 255 )
Next
x=x+50
if x > 590 then
x=100
y = y + 50
endif
x2 = x+2
    y2 = y+3
    x3 = x+48
    y3 = y+48
    Next
endfunction



Function OnMouseMoved(x as number, y as number)

mx = x
my = y
mx = Int ( mx )
my = Int ( my )
    x = mx -50
    y = my - 10
column = Int ( x/50 )
row = Int ( y/50 + 1 )
EndFunction


Function OnMouseReleased(x as number ,y as number, button as string)

Select button
Case "l"
btn = "Left"
break
Case "r"
btn = "Right"
break
EndSelect

EndFunction









 By Rick3137---  http://rb23.yolasite.com/

Guilect

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #1 on: July 09, 2013, 11:31:19 PM »
Hi Rick3137,

Thanks for giving BrowserBasic a go.
I had to add variable declarations for the 2 for loops at line 44 and 47 to get it run.
Not sure which version of BB your were using.
It looks like you are getting ready to program a game a chess or checkers or draughts perhaps?

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #2 on: July 10, 2013, 12:44:58 AM »
  OK. I upgraded my copy. I'm always looking for something new to tinker with. Then I try to do a small right-up for it and put some samples on my website. I need to get me another website that actually does HTML and JavaScript.
 The first program is just a template for building board games. I'm not sure what game, but I might do something like Othello. I hope some other users find it useful.

 Here's the fixed version:
     
Code: [Select]
'    Game Board Demo by Rick3137    http://rb23.yolasite.com/


Var btn as string
Var mx as number
Var my as number
Var column as number
Var row as number


Function OnLoad()
setBackgroundColor( 0, 0, 40)
EndFunction

Function OnDraw()

setColor( 255, 255, 255 )
Print( Str$( mx ) , 20, 100)
Print( Str$( my ) , 20, 120)
Print( Str$( column ) , 20, 140)
Print( Str$( row ) , 20, 160)
    setColor ( 50 , 50 , 100 )
FillRectangle ( 90 , 0, 520, 520)
    setColor ( 90 , 90 , 150 )
FillRectangle ( 100 , 10, 500, 500)
Squares()

EndFunction

Function Squares()
Var x as number
    Var y as number
Var x2 as number
    Var y2 as number
Var x3 as number
    Var y3 as number
Var a as number
Var b as number
setColor ( 255 , 255 , 255 )
    x=100
y=10   
x2 = x+2
y2 = y+3
x3 = x+48
y3 = y+48
For a = 1 to 100     
StrokeRectangle ( x , y, 50, 50)
for b = 1 to 2
setColor ( 0 , 0 , 0 )
Line(x2,y2,x3,y2)
Line(x3,y2,x3,y3)
setColor ( 255 , 255 , 255 )
Next
x=x+50
if x > 590 then
x=100
y = y + 50
endif
x2 = x+2
    y2 = y+3
    x3 = x+48
    y3 = y+48
    Next
endfunction



Function OnMouseMoved(x as number, y as number)

mx = x
my = y
mx = Int ( mx )
my = Int ( my )
    x = mx -50
    y = my - 10
column = Int ( x/50 )
row = Int ( y/50 + 1 )
EndFunction


Function OnMouseReleased(x as number ,y as number, button as string)

Select button
Case "l"
btn = "Left"
break
Case "r"
btn = "Right"
break
EndSelect

EndFunction

SteveOW

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #3 on: July 10, 2013, 07:30:50 AM »
Has a nice clean look to it. :)

If you are looking for a challenge maybe you could do a "Langton's Ant" program.
see http://en.wikipedia.org/wiki/Langton%27s_ant

Not exactly a game but quite good fun all the same.

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #4 on: July 10, 2013, 11:59:54 AM »
 Thanks Steve, that's a good one.

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #5 on: July 10, 2013, 04:56:22 PM »
                                   GAME BOARD TEMPLATE 2

  This template has a grid of 20x20.

   
Code: [Select]
'    Game Board Demo2  by Rick3137    http://rb23.yolasite.com/


Var btn as string
Var mx as number
Var my as number
Var column as number
Var row as number


Function OnLoad()
setBackgroundColor( 0, 0, 40)
EndFunction

Function OnDraw()

setColor( 255, 255, 255 )
Print( Str$( mx ) , 20, 100)
Print( Str$( my ) , 20, 120)
Print( Str$( column ) , 20, 140)
Print( Str$( row ) , 20, 160)
    setColor ( 50 , 50 , 100 )
FillRectangle ( 90 , 0, 520, 520)
    setColor ( 90 , 90 , 150 )
FillRectangle ( 100 , 10, 500, 500)
Squares()

EndFunction

Function Squares()
Var x as number
    Var y as number
    Var a as number

setColor ( 255 , 255 , 255 )
    x=100
y=10   

For a = 1 to 400     
StrokeRectangle ( x , y, 25, 25)

x=x+25
if x > 590 then
x=100
y = y + 25
endif

    Next
endfunction



Function OnMouseMoved(x as number, y as number)

mx = x
my = y
mx = Int ( mx )
my = Int ( my )
    x = mx - 75
    y = my - 10
column = Int ( x/25  )
row = Int ( y/25 + 1 )
EndFunction


Function OnMouseReleased(x as number ,y as number, button as string)

Select button
Case "l"
btn = "Left"
break
Case "r"
btn = "Right"
break
EndSelect

EndFunction

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #6 on: July 10, 2013, 05:02:13 PM »
                                    BOARD TEMPLATE3

 This board template has a 30 x 20 grid. Use it as a starter program.

 
Code: [Select]
'    Game Board Demo3  by Rick3137    http://rb23.yolasite.com/


Var btn as string
Var mx as number
Var my as number
Var column as number
Var row as number


Function OnLoad()
setBackgroundColor( 0, 0, 40)
EndFunction

Function OnDraw()

setColor( 255, 255, 255 )
Print( Str$( mx ) , 20, 100)
Print( Str$( my ) , 20, 120)
Print( Str$( column ) , 20, 140)
Print( Str$( row ) , 20, 160)
    setColor ( 50 , 50 , 100 )
FillRectangle ( 90 , 0,  770, 520)
    setColor ( 90 , 90 , 150 )
FillRectangle ( 100 , 10, 750, 500)
Squares()

EndFunction

Function Squares()
Var x as number
    Var y as number
    Var a as number

setColor ( 255 , 255 , 255 )
    x=100
y=10   

For a = 1 to 600     
StrokeRectangle ( x , y, 25, 25)

x=x+25
if x > 840 then
x=100
y = y + 25
endif

    Next
endfunction



Function OnMouseMoved(x as number, y as number)

mx = x
my = y
mx = Int ( mx )
my = Int ( my )
    x = mx - 75
    y = my - 10
column = Int ( x/25  )
row = Int ( y/25 + 1 )
EndFunction


Function OnMouseReleased(x as number ,y as number, button as string)

Select button
Case "l"
btn = "Left"
break
Case "r"
btn = "Right"
break
EndSelect

EndFunction

SteveOW

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #7 on: July 10, 2013, 06:33:48 PM »
Hi Rick

I need to get me another website that actually does HTML and JavaScript.
I had similar problem.
Then I remembered that my Broadband ISP provides me with a few Gb of webspace for free.
Maybe yours does too.

cheers, SteveOW.
PS I like your Yola website.


Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #8 on: July 13, 2013, 06:03:16 PM »
                            LANGTON'S ANT

  I think it needs more squares. The pattern looks perfectly random to me.

         
Code: [Select]
'    Langtons Ant  by Rick3137    http://rb23.yolasite.com/


Var btn as string
Var mx as number
Var my as number
Var column as number
Var row as number
Var Sqr[800] as array
Var square as number
Var c2 as number
Var r2 as number
Var x2 as number
Var y2 as number
Var ant as number
Var t1 as number
Var time as number
Var direction as number  '1=up 2=right 3=down 4=left
Var test as string
 
Function OnLoad()
    Var a as number
setBackgroundColor( 0, 0, 40)
ant = 285
direction = 1
t1 = 1
for a = 1 to 600
       Sqr[a] = 1
next

EndFunction

Function OnUpdate( dt as number )

UpdateAnt()
endfunction

Function OnDraw()

Var a as number

Print( Str$( direction ) , 20, 100)

    setColor ( 50 , 50 , 100 )
FillRectangle ( 90 , 0,  770, 520)
 
Squares()
PaintAnt()


EndFunction

Function PaintAnt()
    r2 = Int ( (ant - 1 )/30 )
c2 = Int ( ant - r2 * 30 )
r2 = r2 + 1
x2 = 75 + 25 * c2
y2 = 25 * r2 - 15
    setColor ( 255 , 90 , 50 )
FillRectangle ( x2+8 , y2+7, 10, 10)
Endfunction
 
Function UpdateAnt()
 
   
RotateAnt()
FlipColor()
MoveAnt()
time = 0
   

r2 = Int ( (ant - 1 )/30 )
c2 = Int ( ant - r2 * 30 )
r2 = r2 + 1
x2 = 75 + 25 * c2
y2 = 25 * r2 - 15
   
endfunction

Function Squares()
Var x as number
    Var y as number
    Var a as number
Var b as number


    x=100
y=10   

For a = 1 to 600
    setColor ( 90 , 90 , 255 )
    b = Sqr[a]
        if b < 2 then setColor ( 90 , 90 , 255 ) endif
        if b = 2 then setColor ( 90 , 255 , 50 ) endif
FillRectangle ( x , y, 25, 25)
setColor ( 255 , 255 , 255 )
StrokeRectangle ( x , y, 25, 25)
x=x+25
if x > 840 then
x=100
y = y + 25
endif

    Next
endfunction

Function RotateAnt()
   Var a as number
   Var b as number
   a = direction
   b = Sqr[ant]
   if b < 2 then
      a = a + 1
  if a > 4 then a = 1 endif
   endif
   if b > 1 then
      a = a - 1
  if a < 1 then a = 4 endif
   endif
   direction = a
   
endfunction

Function FlipColor()
Var a as number
Var b as number

   b = Sqr[ant]
   if b > 1 then
     a=1
else
a=2
   endif
   Sqr[ant] = a
endfunction

Function MoveAnt()
Var a as number
Var b as number
Var c as number
Var d as number
   b = Sqr[ant]
   a = direction
      if ant < 1 then
         ant = 285
     direction = 1
  endif
      if ant > 600 then
         ant = 285
     direction = 1
  endif
 
   
   if a = 1 then
     ant = ant - 30
if ant < 1 then
   ant = 285
       direction = 1
endif
   endif
   if a = 2 then
     ant = ant + 1
   endif
   if a = 3 then
     ant = ant + 30
if ant > 600 then
   ant = 285
       direction = 1
endif
   endif
   if a = 4 then
     ant = ant - 1

   endif
   
 
   
endfunction





SteveOW

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #9 on: July 14, 2013, 11:12:57 AM »
Hi Rick,
It looks good, but as you say, the grid needs to have more cells.
I think you need about  200x200  = 40,000 cells in order to see the "Ant Highway" being built.
From memory I think it takes about 10,000 steps before the highway building behavior pattern emerges.
Your code is a little too cryptic for me to adjust easilly.
I might have a go at it myself in BB.

Here is a link to a little competition some people had in writing a Langton's Ant Program some years back.
http://ubuntuforums.org/archive/index.php/t-812784.html
It might be interesing to see how Langton's Ant could be done in NaaLaa and EGSL.

cheers, SteveOW.

Tomaaz

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #10 on: July 14, 2013, 12:20:38 PM »
It might be interesing to see how Langton's Ant could be done in NaaLaa and EGSL.

Here is EGSL version.

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #11 on: July 16, 2013, 01:22:40 PM »
                                   LANGTON'S ANT 2
 
  This time I used enough squares to show the ant highway.

     
Code: [Select]
'    Langtons Ant  by Rick3137    http://rb23.yolasite.com/




Var Sqr[60000] as array
Var square as number
Var ant as number
Var direction as number  '1=up 2=right 3=down 4=left
Var cnt as number

 
Function OnLoad()
    Var a as number
ant = 18090
direction = 1
for a = 1 to 599990
       Sqr[a] = 1
next
cnt = 1
EndFunction


Function OnUpdate( dt as number )
Var a as number
cnt = cnt + 1
if cnt < 28 then
   for a = 1 to 500
     UpdateAnt()
   next
endif
endfunction


Function OnDraw()
   
DrawSquares()

EndFunction


 
Function UpdateAnt()

RotateAnt()
FlipColor()
MoveAnt()
 
endfunction

Function DrawSquares()
Var x as number
    Var y as number
    Var a as number
Var b as number
Var cnt as number
cnt = 1
    x=10
y=10   

For a = 1 to 40000
    setColor ( 90 , 90 , 255 )
    b = Sqr[a]
        if b < 2 then setColor ( 90 , 90 , 255 ) endif
        if b = 2 then setColor ( 90 , 255 , 50 ) endif
if a = ant then setColor ( 255 , 90 , 90 ) endif
FillRectangle ( x-1 , y-1, 3, 3 )
cnt = cnt + 1
x=x+3
if cnt = 201 then
cnt = 1
x=10
y = y + 3
endif

    Next
endfunction

Function RotateAnt()
   Var a as number
   Var b as number
   a = direction
   b = Sqr[ant]
   if b = 1 then
      a = a + 1
  if a > 4 then a = 1 endif
   endif
   if b = 2 then
      a = a - 1
  if a < 1 then a = 4 endif
   endif
   direction = a
   
endfunction

Function FlipColor()
Var a as number
Var b as number

   b = Sqr[ant]
   if b = 2 then
     a=1
else
a=2
   endif
   Sqr[ant] = a
endfunction


Function MoveAnt()
Var a as number
   a = direction

   if a = 1 then
     ant = ant - 200

   endif
   
   if a = 2 then
     ant = ant + 1
   endif
   if a = 3 then
     ant = ant + 200

   endif
   
   if a = 4 then
     ant = ant - 1

   endif
   
 
   
endfunction





SteveOW

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #12 on: July 16, 2013, 03:21:49 PM »
Hi Rick, it looks good. :)

I took your code and made a few changes, mainly cosmetic.
Now programmer can easilly switch between different number of hidden steps.
The way you use a 1D array to represent a 2D grid is interesting.

Code: [Select]
'    LangtonsAnt  by Rick3137    http://rb23.yolasite.com/

'... Define Global Variables
var NumHiddenSteps as number
'NumHiddenSteps = 1
NumHiddenSteps = 50
'NumHiddenSteps = 500

Var CellVal[60000] as array
Var square as number
Var antCell as number
Var direction as number  '...values 0=up 90=right 180=down 270=left

Var plotCount as number

 
Function OnLoad()

    Var Cnum as number
antCell = 18090
direction = 0 '1
    for Cnum = 1 to 599990 '...why not 599999?
       CellVal[Cnum] = 0
next
plotCount = 1

EndFunction


Function OnUpdate( dt as number )
Var CellNum as number

var numReplots as number
numReplots = 14000/NumHiddenSteps
plotCount = plotCount + 1

if plotCount < numReplots then
   for CellNum = 1 to NumHiddenSteps '...when plotCount > numReplots, the prog updatesantCell position NumHiddenSteps times before plotting
     UpdateAnt()
   next
endif
endfunction


Function OnDraw()
   
DrawSquares()

EndFunction


 
Function UpdateAnt()

RotateAnt()
FlipColor()
MoveAnt()
 
endfunction

Function DrawSquares()

Var x as number
    Var y as number
    Var Cnum as number
Var plotCount as number

plotCount = 1
    x=10
y=10   

For Cnum = 1 to 40000 '...200 x 200 = 40,000

    setColor ( 90 , 90 , 255 )'...blue, default, for cells with value = 0

        if CellVal[Cnum] = 1 then setColor ( 90 , 255 , 50 ) endif '...green
if Cnum = antCell    then setColor ( 255 , 90 , 90 ) endif '...red 

FillRectangle ( x-1 , y-1, 3, 3 )
plotCount = plotCount + 1
x = x+3
if plotCount = 201 then '... begin new row
plotCount = 1
x = 10
y = y + 3
endif

    Next
endfunction

Function RotateAnt()
 
   if CellVal[antCell] = 0 then
      direction = direction + 90
  if direction > 270 then direction = 0 endif
   elseif CellVal[antCell] = 1 then
      direction = direction - 90
  if direction < 0 then direction = 270 endif
   endif
   
endfunction

Function FlipColor() 

   if CellVal[antCell] = 0 then 
     CellVal[antCell] = 1
   else                     
CellVal[antCell] = 0
   endif
   
endfunction


Function MoveAnt()

   if direction= 0 then
    antCell = antCell - 200 '...move up to prev row
   elseif direction = 90 then
    antCell = antCell + 1
   elseif direction = 180 then
    antCell = antCell + 200 '...move down to next row
   elseif direction = 270 then
    antCell = antCell - 1
   endif
     
endfunction

« Last Edit: July 16, 2013, 06:30:08 PM by SteveOW »

Guilect

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #13 on: July 16, 2013, 10:36:49 PM »
I have heard of having bugs in code, but never to code bugs.

 ;D

Rick3137

  • Guest
Re: Sample Programs for BrowserBasic
« Reply #14 on: July 17, 2013, 01:37:15 PM »
  Nice remix Steve. Now all it needs is some bug spray.  8)