Dig, Bunny, Dig!
This app sends a rabbit burrowing all over the screen. Her movements are random, and the pattern she draws with her tunnels is different every time.
1 fill('buried')
Line 1 puts a background image of an underground world on the screen.
- 'buried' is the label of the graphic we want the app to use.
2 r = stamp('rabbit3',150)
Line 2 stamps a rabbit on the screen and assigns it the name "r".
- 'rabbit3' is the label of the bunny graphic we want the app to use.
- 150 is the size of the bunny on the screen, in pixels.
3
Line 3 is left blank to create visual spacing in the code.
4 function loop() {
Line 4 creates a new block of code called loop() which tells the app to repeat everything inside it 20 times per second, forever.
- The loop() function is built into Bitsbox.
- A function is a block of code which only runs when it's called. In this case, the loop() function begins automatically as soon as the app starts running.
5 stamp('dig', r.x, r.y )
Line 5 stamps a black circle at the same screen location as the rabbit. Put together, hundreds of black circles combine to look like a tunnel.
- 'dig' is the label of the graphic we want the app to use.
- r.x and r.y refer to the current location of "r" (the rabbit) on the screen.
6 r.move(RIGHT,20)
Line 6 tells "r" (which is the rabbit) to move.
- RIGHT is the direction the rabbit should move.
- 20 is the distance, in pixels, that the rabbit should move.
7 r.wrap()
Line 7 tells "r" (which is the rabbit) to "wrap" around if it gets to the edge of the screen. If it goes off the top, it appears at the bottom, and so on.
8 r.front()
Line 8 tells "r" (which is the rabbit) to appear on top of the stamp 'dig'.
- Without this line of code, the rabbit would be hidden behind the tunnel.
9 if (random(50) == 1) {
Line 9 begins an if statement. If the expression between the parentheses is true, the code on line 10 should run. If the expression is false, the code doesn't run at all.
- This line of code picks a random number from 1 to 50 and checks to see if it's equal to 1.
- The command random(50) generates a random number from 1 to 50.
- The operator == means "is equal to".
- The point of this line of code is to make the rabbit turn about once every 50 steps. Sometimes it's more frequently and sometime it's less.
10 r.rotate(random(360),1000)
Line 10 tells "r" (which is the rabbit) to rotate by a random amount, and to take 1 second to do it.
- The command random(360) generates a random number between 1 and 360.
- The result of the command random(360) is the number of degrees it should rotate.
- 1000 milliseconds (1 second) is the length of time it should take to rotate.
11 }
Line 11 uses a curly bracket } to indicate that this is the end of the if statement which started on line 9.
12 }
Line 12 uses a curly bracket } to indicate that this is the end of the loop() function which started on line 4.