Numbers and Math 🔢¶
Give numbers names, and let the computer do the math
Naming a Number 🏷️¶
You've given names to shapes. You can give names to numbers too:
// Default canvas: 800 wide, 600 tall, white
number size = 80
draw circle at 150, 150 having radius size, fill "orange"
Now size means 80 everywhere you use it. This is called a variable.
Why is that useful? Say you want three circles that are all the same size:
// Default canvas: 800 wide, 600 tall, white
number size = 40
draw circle at 80, 100 having radius size, fill "red"
draw circle at 200, 100 having radius size, fill "gold"
draw circle at 320, 100 having radius size, fill "green"
To make all three bigger, you only change one number. Try changing 40 to 55!
Doing Math ➕¶
VeeLang can do math for you:
| Sign | What it does | Example | Answer |
|---|---|---|---|
+ |
add | 10 + 5 |
15 |
- |
take away | 10 - 5 |
5 |
* |
multiply (times) | 10 * 5 |
50 |
/ |
divide (share equally) | 10 / 5 |
2 |
% |
what's left over after dividing | 10 % 3 |
1 |
// Default canvas: 800 wide, 600 tall, white
number big = 100
number half = big / 2
draw circle at 150, 150 having radius big, fill "lightblue"
draw circle at 150, 150 having radius half, fill "blue"
You can do math right where you use a number, too:
// Default canvas: 800 wide, 600 tall, white
number x = 60
draw circle at x, 100 having radius 30, fill "pink"
draw circle at x + 100, 100 having radius 30, fill "violet"
draw circle at x + 200, 100 having radius 30, fill "purple"
Which Math Goes First? 🧮¶
Just like in school, * and / happen before + and -. Use brackets ( ) to choose what happens first:
// Default canvas: 800 wide, 600 tall, white
number a = 2 + 3 * 10
number b = (2 + 3) * 10
draw text at 30, 60 having content "2 + 3 * 10 is 32", size 24
draw text at 30, 110 having content "(2 + 3) * 10 is 50", size 24
draw rectangle at 30, 140 having width a * 5, length 30, fill "orange"
draw rectangle at 30, 190 having width b * 5, length 30, fill "teal"
Naming Words 💬¶
Variables can hold words too. Use text instead of number, and put the words in quotes:
// Default canvas: 800 wide, 600 tall, white
text name = "Sam"
draw text at 40, 80 having content name, size 48, color "purple"
draw text at 40, 150 having content name, size 48, color "orange", font "cursive"
Let's Build a Traffic Light 🚦¶
Everything is measured from one number, light. Change it, and the whole traffic light grows or shrinks!
// Default canvas: 800 wide, 600 tall, white
number light = 40
number gap = 10
number boxWidth = light * 2 + gap * 2
draw rectangle at 100, 30 having width boxWidth, length light * 6 + gap * 4, fill "black"
draw circle at 100 + boxWidth / 2, 30 + gap + light having radius light, fill "red"
draw circle at 100 + boxWidth / 2, 30 + gap * 2 + light * 3 having radius light, fill "gold"
draw circle at 100 + boxWidth / 2, 30 + gap * 3 + light * 5 having radius light, fill "green"
Numbers with a dot
Numbers can have a dot for "and a bit", like 2.5 (two and a half). Write a number on both sides of the dot: 0.5, not .5.
Challenge Time! 🏆¶
- 🌟 Challenge 1: Make a row of five squares, using a variable for the size and math for where each one goes.
- 🌟 Challenge 2: Make the traffic light twice as big by changing only one number.
- 🌟 Challenge 3: Use a
textvariable to write the same word three times in different colors.
What's Next? 🚀¶
You can now name numbers and words with number and text, and do math with +, -, *, / and %.
Next up: changing shapes after you've made them, and making copies! ✂️