Now that we understand how to use variables and expressions, let's turn back to the house we drew in the first chapter. Remember the coordinates were (-100,-50), (-100,-170), (100,-170), (100,-50), (0,0). First, write a script that draws the house with these coordinates and run it. Remember to place the pen down or nothing will be drawn.
Now create variables XPos and YPos. Set XPos to the "x position" property in Motion and YPos to the "y position" property.
Now create the following base expressions in which we add XPos and YPos to something:
Using right-click and duplicate can be very useful for the next step. Duplicate an x coordinate and y coordinate for each coordinate pair and fill in the appropriate numbers. Place a "set pen color to" block at the top just for fun and set the argument to "pick random" 0 to 200.
If you run your script and it draws a house appropriately, move the Sprite around with the mouse and hit the Green Flag again so that you can create multiple houses at different positions.
We will use this script again so you should save it by going to File -> Save As and saving it in a location and with a name that you will remember (like HouseDraw).
Excercise 2-3. Why would it have been incorrect to use the "x position" and "y position" properties as shown below instead of assigning them to variables?
"x position" and "y position" are updated each time the x and y positions of the Sprite change. In the house-drawing script, we wanted to use them as a new origin for drawing the house. If we had not assigned them to variables, they would have changed each time the Sprite moved and the house would be completely distorted.