Creating Shapes in L2

Adding a background

If you've been following along, you may have noticed that creating a shape alone doesn't do very much. Now we have to give it some properties. So let's give our L2 app a background.

size... to

So, first, we create the background and assign it a size. The default width of an L2 canvas is a 640x640 square. So let's make our background object fill the entire space

        object background = new rectangle
        size background to 640, 640
    

So here we used the size method, then gave it the name of the object we wanted to size, the keyword to means we want to set it to exactly 640 wide and 640 high.

paint

Maybe we don't want a black background (which is the default color). Let's paint that object a different color.

        object background = new rectangle
        size background to 640, 640
        paint background blue
    

We should now see our background blue!

Creating a circle

Creating a cricle (or any other shape) is not much different than the rectangle we used for the background. So let's add it, size it, and paint it.

        object sun = new circle
        size sun to 150, 150
        paint sun orange
    

move... to

Cool! We've got our sun, but maybe we don't want it at the top left. Its default location is 0, 0. When we position an object in L2, we are using x/y coordinates like that Pre-Algebra class we mentioned earlier. However, the origin is the top-left corner. Our x value goes to the right like we would expect, but our y coordinate is moving us down from the top. So 50, 70, for example, would be 50 pixels from the left and then 70 down from the top.

So let's position that sun.

        object sun = new circle
        size sun to 150, 150
        paint sun orange
        move sun to 50, 70
    

We can also set negative values for our object, so that it can appear partially (or even fully) off screen. So let's move our sun to be only partly in the picture.

        object sun = new circle
        size sun to 150, 150
        paint sun orange
        move sun to -40, -50
    

Cut down on repetition

It may not have bothered you yet, but (after a while) repeating the variable name over and over can get quite old. We can avoid this repetition in one of two ways.

On creation of the object

By indenting after we create our object, we tell L2 that our command should take action on the previously created object. Then we can skip typing sun again.

        object sun = new circle
          size to 150, 150
          paint orange
          move to -40, -50
    

with

Alternately, we can use the with command to tell L2 that the following lines will relate to that object. We use our intent again to indicate this.

        with sun
          size to 150, 150
          paint orange
          move to -40, -50