Robot
JavaScript
Compiler
Language
Videos
Simulator
Games
Examples
Download
Cheat sheet

Snow Falling

Example: Snow Falling
/*****************************************
 SnowFalling.js

Step 1: Create a two-dimensional array of snowflakes
  Each element in the array will be either 0, 1, or 2
   0 = no snowflake
   1 = small snowflake
   2 = big snowflake
******************************************/
plane = []                       // Initialize an empty array
plane.push([0,0,0,0,2,0,0,0,1])  // Push other arrays onto it
plane.push([0,0,1,0,0,0,0,1,0])  // Each array represents one row of snowflakes
plane.push([1,0,0,0,0,2,0,0,0])  // There are 9 possible positions in each row
plane.push([0,0,0,1,0,0,0,0,1])
plane.push([0,0,0,0,0,0,1,0,0])
plane.push([0,1,0,0,0,1,0,0,0])
plane.push([0,0,2,0,0,0,0,0,1])
plane.push([0,0,0,0,1,0,0,0,0])
plane.push([1,0,0,0,0,0,0,1,0])
plane.push([0,0,0,2,0,0,0,0,0])
plane.push([0,0,0,0,0,0,1,0,0])

// Step 2: Initialize parameters for the window
borderWidth = 'thin'   // The borderWidth will be either thick or thin
currRow=0         // This variable keeps track of which row is being shown at the bottom of the window
topOffset=10      // in pixels.  This is the top offset of the window
leftOffset=6      // in pixels.  This is the left offset of the window
width=145         // in pixels.  This is the width of the window
height=90         // in pixels.  This is the height of the window
rowHeight = 14    // in pixels.  This number should be no more than the height of the window divided by the number of rows being shown
flakeSpacing = 17 // in pixels.  This number should be no more than the width of the window divided by the number of flakes per row
circleRadius=10
windowLeft=leftOffset+circleRadius

// Step 3: Draw the window border
switch(borderWidth) {
case 'thick':
  fillRect(0, 0, 180, 128)
  clearCircle(windowLeft,topOffset+circleRadius, circleRadius)
  clearCircle(windowLeft+width,topOffset+circleRadius, circleRadius)
  clearCircle(windowLeft,topOffset+circleRadius+height, circleRadius)
  clearCircle(windowLeft+width,topOffset+circleRadius+height, circleRadius)
  clearRect(windowLeft,topOffset,width,topOffset+circleRadius+height+1)
  clearRect(leftOffset,topOffset+circleRadius,width+circleRadius*2+1,height)
  break
case 'thin':
  clearScreen()
  circle(windowLeft,topOffset+circleRadius, circleRadius)
  circle(windowLeft+width,topOffset+circleRadius, circleRadius)
  circle(windowLeft,topOffset+circleRadius+height, circleRadius)
  circle(windowLeft+width,topOffset+circleRadius+height, circleRadius)
  clearRect(windowLeft,topOffset,width,topOffset+circleRadius+height+1)
  clearRect(leftOffset,topOffset+circleRadius,width+circleRadius*2+1,height)
  line(windowLeft,topOffset,windowLeft+width,topOffset)                                             // Top Line
  line(leftOffset,topOffset+circleRadius, leftOffset, topOffset+circleRadius+height)                // Left side Line
  line(windowLeft,topOffset+circleRadius*2+height,windowLeft+width,topOffset+circleRadius*2+height) // Bottom Line
  line(leftOffset+width+circleRadius*2,topOffset+circleRadius, leftOffset+width+circleRadius*2, topOffset+circleRadius+height) // Right side Line
  break
}

// Step 4: Draw the scene
while(true) {
    for(j=0;j<6; j++) {  // Only show 6 rows in the window
      mk = (currRow+j) % plane.length
      yCoordinate = topOffset+circleRadius+height-(j+1)*rowHeight
      clearRect(windowLeft+1, yCoordinate, width, rowHeight )  // Erase any snowflakes from this row
      for(n=0; n < plane[mk].length; n++) {
        xCoordinate=n*flakeSpacing+windowLeft+1
        iconOfFlake = (plane[mk][n] >=1 ) ? '*': ''            // Though this statement uses a ternary operator, a more simpler expression may be: iconOfFlake = '*'
        sizeOfFlake = plane[mk][n]                             // Text in the EV3 has 3 font sizes: 0, 1 and 2
        if(plane[mk][n]>=1) drawText(xCoordinate, yCoordinate, iconOfFlake, sizeOfFlake)
      }
      sleep(50)
    }
    currRow++    // Increment the current row pointer
}
waitHereWhile(true)

// End of the program