Recipes¶
A recipe is a shape you invent. It says how to make something out of parts, and what can be different each time. A recipe draws nothing on its own: each time you use it, VeeLang makes a new shape from it.
The keyword is shape. A shape made from a recipe is called an instance, like kim below.
shape face having size 100, skin "pink" {
circle head at 0, 0 having radius size / 2, fill skin
circle leftEye at -size / 5, -size / 8 having radius size / 12, fill "black"
circle rightEye at size / 5, -size / 8 having radius size / 12, fill "black"
}
draw face kim at 100, 100 having size 60
draw face at 250, 100
Defining a recipe¶
shape name having setting default, setting default, ... {
parts
}
shape house having floors 2 {
rectangle walls at 0, -60 * floors having width 120, length 60 * floors, fill "wheat"
triangle roof having corner1 at -15, -60 * floors, corner2 at 135, -60 * floors, corner3 at 60, -60 * floors - 50, fill "firebrick"
}
draw house at 40, 250
draw house at 200, 250 having floors 3
| Name | any name that isn't already used. It can't be a shape VeeLang already knows, like circle. |
| Settings | written after having, each with its default. A recipe can also have no settings: shape face { ... }. |
| Where it goes | at the top level of the program, anywhere, even before Canvas. Not inside a loop or another shape. |
0, 0 |
where the instance's at goes. You choose what it is: the middle of a face, the bottom-left corner of a house. |
| Order | a recipe must be written above the first line that uses it. |
The parts are written without draw: they are drawn when the instance is drawn.
Lesson: Your Own Shapes: Recipes
Settings¶
A setting is something that can be different in each instance. Inside the recipe, a setting is a name you can use, like a variable.
| Kind | In the header | What it does |
|---|---|---|
| Number | size 100 |
a number the parts use, like radius size / 2 |
| Text | skin "gold" |
words the parts use, like fill skin |
| Sent to a part | skin is head.fill |
sets one property of one part (below) |
| With choices | hair "short" and when hair is ... blocks |
picks which parts to make (below) |
shape face having size 100, skin "gold" {
circle head at 0, 0 having radius size / 2, fill skin
circle leftEye at -size / 5, -size / 8 having radius size / 12, fill "black"
circle rightEye at size / 5, -size / 8 having radius size / 12, fill "black"
}
draw face at 80, 100
draw face at 220, 100 having skin "pink"
draw face at 360, 100 having skin "lightgreen", size 70
- The default says what kind of setting it is.
size 100is a number, sosize "big"is a mistake.skin "gold"is text. - A default is a number or text in quotes. Simple math is fine (
size 10 * 2), but not names,trueorfalse. angle,scale,x,yandcentercan't be settings, because every shape already has them.- A setting you don't give keeps its default.
Explaining a setting¶
A comment after a setting says what it is for. The playground shows it when you point at the recipe's name.
shape face having
size 100, // how wide the face is
skin "gold" // the colour of the face
{
circle head at 0, 0 having radius size / 2, fill skin
}
draw face at 150, 120
Settings sent to a part¶
shape name having setting is part.property { ... }
shape face having size 100, skin is head.fill {
circle head at 0, 0 having radius size / 2, fill "gold"
circle leftEye at -size / 5, -size / 8 having radius size / 12, fill "black"
}
draw face at 100, 100
draw face at 250, 100 having skin "tomato"
skin is head.fillmeans "skin is the head's fill".- Its default is whatever the recipe gives that part: here
"gold". - It can go to a number property too (
r is head.radius), and then it takes a number. - It can go to a setting of a part made from another recipe (
eyeColour is left.colour). - Inside the recipe, a setting sent to a part is not a name you can use. Use the part's property instead.
Choices with when¶
when setting is value {
parts
}
shape face having size 100, hair "short" {
circle head at 0, 0 having radius size / 2, fill "peachpuff"
when hair is "short" {
arc fringe from -size / 2, -size / 8 to size / 2, -size / 8 having radius size / 2, thickness size / 10, color "saddlebrown"
}
when hair is "curly" {
repeat 7 times with n {
circle at -size / 2 + size / 8 * n, -size / 2 having radius size / 10, fill "saddlebrown"
}
}
when hair is "bald" { }
}
draw face at 90, 110
draw face at 230, 110 having hair "curly"
draw face at 370, 110 having hair "bald"
- Only the parts in the block that matches are made.
- The
whenblocks decide which values the setting can have. Here hair can be"short","curly"or"bald", and anything else is a mistake. - The default must be one of them.
- To make nothing for a value, give it an empty block:
when hair is "bald" { }. whenworks for numbers too:when points is 6 { }. It matches the exact value.whenblocks go only in a recipe, and only for its own settings.
otherwise: any other value¶
A when can be followed by otherwise { }. Its parts are made for any value the when doesn't name, so an optional part doesn't need an empty block for "off":
shape flag having pole "yes" {
rectangle cloth at 0, 0 having width 60, length 40, fill "gold"
when pole is "yes" {
line stick from -30, -20 to -30, 80 having thickness 3
} otherwise { }
}
draw flag at 100, 60
draw flag at 260, 60 having pole "no"
- With an
otherwise, the setting can have any value of its kind, not only the ones awhennames, and its default doesn't need awhenof its own. - It works like
if … otherwise: it is made when the setting isn't the value in thewhenjust above it.
What a body can hold¶
| Example | |
|---|---|
Parts (shapes, without draw) |
circle head at 0, 0 having radius 50 |
| Parts made from recipes written above | eye left at -20, -10 having size 8 |
| Parts with parts of their own | circle cheek at 0, 0 having radius 20 { circle dimple ... } |
| Variables | half = size / 2 |
| Changes to its own parts | head.fill = "red", cheek.dimple.fill = "red" |
| Loops | repeat floors times with f { ... } |
| Choices | when hair is "curly" { ... } |
| Lines that take time | eye.radius = 1 in 0.2 seconds, wait 1 second, forever { ... } |
| Clicks on its parts | when eye is clicked { ... } |
if, draw, and animation, play and stop go outside the recipe, after the instance is drawn.
shape hat having size 10 {
half = size / 2
circle top at 0, 0 having radius half
top.fill = "red"
repeat 3 times {
square at count * 10, 0 having size 3
}
}
draw hat at 100, 100 having size 30
A recipe that moves on its own¶
A recipe can take time, like a program: every face made from this one blinks by itself, and turns its eye red when you click it.
shape face having size 100 {
circle head having radius size / 2, fill "gold"
circle eye at -size / 5, -size / 8 having radius size / 12, fill "black"
forever {
eye.radius = 1 in 0.2 seconds
eye.radius = size / 12 in 0.2 seconds
wait 2 seconds
}
when eye is clicked {
eye.fill = "red"
}
}
draw face at 120, 150
draw face at 300, 150 having size 60
- Each face has its own
foreverand its ownwhen eye is clicked. - A
foreverruns alongside: drawing the face doesn't wait for it. Other lines that take time do: the next line afterdraw faceruns once they are done, as everywhere else. - When a setting changes, the face is made again, and only what the new setting changes is changed: a blink that's running carries on. A
foreverinside awhenstops when itswhenno longer applies, and starts when it applies again. - A copy (
face sam = kim) carries on from where the original has got to, so the two blink together.
Telling an instance what to do¶
A recipe can hold animations of its own, called actions. Write an animation at the top of the recipe's { }, beside its parts. Then each instance can be told to do it by name:
shape face having size 100 {
circle head at 0, 0 having radius size / 2, fill "gold"
circle eye at -15, -10 having radius 8, fill "black"
animation blink having amount 1 {
eye.radius = amount in 0.3 seconds
eye.radius = 8 in 0.3 seconds
}
}
draw face kim at 80, 100
draw face sam at 200, 100
play kim.blink having amount 2
kim.blink(amount=2)
sam.blink()
| English | Call form |
|---|---|
play kim.blink |
kim.blink() |
play kim.blink having amount 2 |
kim.blink(amount=2) |
stop kim.blink |
kim.blink.stop() |
if kim.blink is playing { } |
if kim.blink.is_playing() { } |
- Each instance has its own:
kim.blink()doesn't makesamblink. - Inside the recipe you don't write the instance.
blink()andplay blinkmean this face's blink, aseyemeans this face's eye. - An action reads the recipe's settings when each line starts, so
kim.size = 200changes the lines that haven't started yet. An action's own setting (amount) keeps the value it was called with. - If a setting change takes away a part that a running action uses, the action stops where it is and keeps what it had already changed.
- If a setting change sets a property that a running action is changing over time (the eye's radius is
size / 20, andsizechanges while the action grows the eye), the setting's value takes effect at once and the action's change to that property ends. The last thing written wins, as when you writeeye.radius = 30while the eye is growing. The action carries on with its other lines.
Variables of its own¶
A variable made at the top of a recipe's body belongs to each instance, which keeps it as long as it is around. Its actions can read it and change it, and so can the recipe's forever and when blocks. The program can't: it is the face's own.
shape face having size 100 {
circle eye at 0, 0 having radius 10, fill "black"
number step = 4
animation grow_eye {
eye.radius = eye.radius + step in 1 second
}
animation bigger_steps {
step = step + 1
}
}
draw face kim at 100, 100
draw face sam at 250, 100
kim.bigger_steps()
kim.grow_eye()
sam.grow_eye()
kimandsameach have their ownstep, sokimgrows the eye by 5 andsamby 4.- Changing a setting doesn't reset a variable. It only gives it a new start if its start is worked out from the setting (
number step = size / 10) and that now comes to something different. face sam = kimcopies the variables as they are, and from then on they are separate.- A bunch of faces takes the call for each one, all at once:
faces.blink(). The next line waits until the last has finished. One face of a bunch:faces[2].blink(). - An action can't be called
move,turn,grow,shrink,show,hide,beat,stop,waitoris_playing, the same as a part or a setting. Actions go at the top of the recipe, not inside awhen, and only recipes have them: ananimationinside a plain{ }is still a mistake.
A recipe uses only what's inside it¶
A recipe can use:
- its own settings,
- its own parts,
- its own variables,
- other recipes written above it.
It can't use the drawing's shapes or variables. Each line that does is a mistake (vee.recipe.outsideName). If a recipe needs something from the drawing, give it a setting, and set it where you draw the instance:
draw circle sun at 60, 60 having radius 20, fill "gold"
shape face having size 100 {
circle head at 0, 0 having radius sun.radius * 2
}
draw circle sun at 60, 60 having radius 20, fill "gold"
shape face having size 100 {
circle head at 0, 0 having radius size / 2
}
draw face at 200, 150 having size sun.radius * 4
This means an instance depends only on its recipe and its settings.
Using a recipe¶
Use the recipe's name like a shape's name.
draw face kim at 100, 100 having size 60 |
makes a face called kim, with size 60, and draws it |
draw face at 100, 100 |
makes a face with no name and draws it |
face kim at 100, 100 |
makes kim without drawing it; draw kim draws it later |
face sam = kim |
makes sam, a copy of kim with the same settings and place |
shape face having size 100, skin "gold" {
circle head at 0, 0 having radius size / 2, fill skin
circle leftEye at -size / 5, -size / 8 having radius size / 12, fill "black"
}
face kim at 100, 100 having size 60
kim.skin = "pink"
face sam = kim
sam.at = 250, 100
sam.size = 120
draw kim
draw sam
atis where the recipe's0, 0goes. Afterhavingyou can give the recipe's settings (andangleandscale). Anything else is a mistake that lists the settings.- An instance gets its parts from its recipe, so it can't have a
{ }of its own. - Like any shape, an instance is drawn once. To draw another one, copy it or make a new one.
Recipes inside recipes¶
A recipe can use recipes written above it, as parts.
shape eye having size 10, colour "black" {
circle white at 0, 0 having radius size, fill "white", stroke "gray"
circle pupil at 0, 0 having radius size / 2, fill colour
}
shape face having size 100 {
circle head at 0, 0 having radius size / 2, fill "gold"
eye left at -size / 5, -size / 8 having size size / 10
eye right at size / 5, -size / 8 having size size / 10, colour "green"
}
draw face at 150, 120 having size 160
- A recipe can't use itself, or a recipe written below it.
- A recipe uses a part made from another recipe the same way the drawing does: it can change that part's settings (
left.size = 20), but not that part's parts.
Reading settings and parts¶
Read a setting or a named part with a dot.
kim.size |
kim's setting size (its default if it wasn't given) |
kim.head |
kim's part head |
kim.head.center |
where kim's head is, on the whole drawing |
kim.at, kim.center, kim.angle, kim.scale |
like any shape |
shape face having size 100, skin "pink" {
circle head at 0, 0 having radius size / 2, fill skin
circle eye at size / 5, -size / 8 having radius size / 12, fill "black"
}
draw face kim at 100, 100 having size 80
draw circle at kim.eye.center having radius 3, fill "white"
draw circle at 250, 100 having radius kim.size / 2, fill kim.skin
Parts without a name can't be reached.
Changing an instance¶
You change an instance only through its settings. Its parts belong to its recipe.
| You can | Example |
|---|---|
| Change a setting | kim.size = 140, kim.skin = "pink" |
| Move, turn or grow it as a whole | kim.at = 200, 200, move kim by 10, 0, kim.angle = 45, kim.scale = 2 |
| Show or hide it | hide kim, show kim |
| Read its parts | kim.head.center |
A shape with parts keeps the thickness its lines were given when it grows or shrinks: a line of thickness 4 is 4 thick at any scale, only longer or shorter.
| You can't | Why |
|---|---|
kim.head.fill = "blue" |
it changes a part (vee.recipe.partChange) |
move kim.head by 10, 0 |
the same |
This is true everywhere: in the picture, in the animation, in a click, and inside another recipe. If you want to change something about a face, give its recipe a setting for it, like skin is head.fill.
A recipe can change its own parts in its body, including the parts of a part (cheek.dimple.fill = "red").
shape face having size 100, skin is head.fill {
circle head at 0, 0 having radius size / 2, fill "gold"
circle cheek at size / 4, size / 8 having radius size / 10 {
circle dimple at 0, 0 having radius 2
}
cheek.dimple.fill = "red"
}
draw face kim at 120, 120
kim.skin = "pink"
move kim by 50, 0
What a setting change does¶
When a setting changes, VeeLang makes the instance's parts again from its recipe, with the new value. Everything worked out from the setting changes too: the eyes move and grow with the head, a loop makes more or fewer parts, and a when setting picks its other parts.
shape face having size 100, eyes "black" {
circle head at 0, 0 having radius size / 2, fill "gold"
circle leftEye at -size / 5, -size / 8 having radius size / 12, fill eyes
circle rightEye at size / 5, -size / 8 having radius size / 12, fill eyes
}
face kim at 150, 120
kim.size = 140
kim.eyes = "blue"
draw kim
Changing settings in the animation¶
A setting can change slowly, with in, and in a click.
Canvas paper having width 400, length 300
shape face having size 60, skin "gold" {
circle head having radius size / 2, fill skin
circle eye1 at -size / 6, -size / 10 having radius size / 15, fill "black"
circle eye2 at size / 6, -size / 10 having radius size / 15, fill "black"
}
draw face sam at 200, 150
sam.size = 160 in 2 seconds
sam.skin = "pink" in 2 seconds
when sam is clicked {
sam.size = sam.size - 20
}
| The setting is | With in N seconds |
|---|---|
| a number | changes smoothly, and the parts are made again all the way |
| a colour | slowly mixes into the new colour |
other text (like hair "curly") |
changes when the time is up |
Lesson: Recipes That Change · Lesson: Clicking a Recipe
Settings that follow¶
Put @ before a value to make a setting follow it. When the value changes, the setting changes too, and the parts are made again.
Canvas paper having width 400, length 300
draw circle sun at 60, 60 having radius 20, fill "gold"
shape face having size 60 {
circle head having radius size / 2, fill "pink"
}
draw face sam at 250, 170 having size @sun.radius * 4
forever {
sun.radius = 40 in 2 seconds
sun.radius = 20 in 2 seconds
}
Here the face is always twice as wide as the sun.
@ between parts¶
Inside a recipe, a part's property is read once, where it is written. To make a part follow another part of the same recipe, use @:
shape face having size 100 {
circle head at 0, 0 having radius size / 2, fill "gold"
circle nose at @head.center having radius 5, fill "red"
head.at = 20, 0
}
draw face at 150, 120
The nose follows the head's center, so it moves with the head. Without @, the nose would stay at 0, 0.
- A part can follow only parts of its own shape. Following one of the drawing's shapes is a mistake.
Turning and growing¶
An instance turns and grows as one piece, around the middle of all its parts. Give it angle and scale when you draw it, or change them later.
shape arrow having size 100 {
rectangle body at 0, -size / 20 having width size, length size / 10, fill "navy"
triangle tip having corner1 at size, -size / 8, corner2 at size, size / 8, corner3 at size * 1.3, 0, fill "navy"
}
draw arrow a at 60, 100 having angle 30
draw arrow b at 60, 220 having scale 0.5
turn b by 90 in 2 seconds
Settings that hold a shape¶
A setting can hold another recipe. Its starting value is the recipe's name. The recipe uses the setting's name wherever it would write a shape, and each drawing chooses which shape that is.
shape dot having size 10 {
circle d at 0, 0 having radius size, fill "tomato"
}
shape box having size 10 {
square b at 0, 0 having size size * 2, fill "royalblue"
}
shape row having times 3, inner dot {
repeat times times with n {
inner at 40 * n, 0 having size 8
}
}
draw row a at 20, 60
draw row b at 20, 160 having inner box, times 4
- The shape is always written in the drawing, so it is the same every time the drawing runs. A drawing can't change it later (
b.inner = dotis a mistake). - Give a shape settings of its own in brackets:
having inner (box having size 20). They become that shape's starting values, and the recipe can still change them. - Make a shape first without drawing it, then use its name:
row tall having inner box, times 2and thendraw row c at 20, 260 having inner tall. - A shape inside a shape works to any depth, because each one is a recipe.
timesis a good name for a count.countis the loop's own counter.
Settings that hold a bunch¶
A setting can hold a bunch, for a recipe that draws as many things as it is given. Its starting value is empty, a bunch with nothing in it. The bunch is made on a line of its own, and the drawing gives its name.
shape stack having heights empty {
repeat heights.count times with n {
repeat heights[n] times with h {
square b at 40 * n, 160 - 30 * h having size 28
}
}
}
bunch tall = 1, 3, 2
bunch low = 2, 1
draw stack a at 20, 0 having heights tall
draw stack b at 200, 0 having heights low
- Inside the recipe it is an ordinary bunch:
heights[n],heights.count,heights.first,heights.last, andrepeat for each h in heights. - Until a drawing gives it a bunch, it is empty, so the recipe draws nothing from it.
- An instance keeps its own copy of the bunch as it was when it was given. Adding to
tallafterwards changes no stack. To change what a stack shows, give it a bunch again:a.heights = low. - The recipe can read the bunch but can't add to it or take from it, the same as its other settings.
- One bunch holds one kind of thing. The first bunch a recipe is given decides: numbers, words, places or true and false (not shapes), and every stack after it is given the same kind.
Recipes in their own files¶
A recipe can live in a file of its own, a library, and many drawings can use it. A drawing asks for the recipes it wants with use, on its first lines:
use face, tree from "Garden"
draw face sam at 100, 100
draw tree at 250, 150
use everything from "Garden"brings every recipe of the file.- A recipe brings the recipes it needs, so
use treealso brings theleafthattreeuses. - A library file holds only recipes (and its own
uselines). Its recipes are checked in the file, so a mistake in one is shown on youruseline, with the file's name and the line in it. - The same recipe, with the same words, can come from two files, or be written in the drawing too: it is one recipe. Two different recipes with one name are a mistake that names both files.
- Which files a drawing can reach is up to the place it runs. The playground has no libraries yet, so
usethere says so.
Common mistakes¶
| You wrote | VeeLang says |
|---|---|
kim.head.fill = "blue" |
'kim' is made by the recipe 'face', so you change it through its settings, not its parts. |
circle c at sun.center inside a recipe |
'sun' is part of the drawing, and a recipe can only use its own settings and parts. |
draw face ... above shape face ... |
The recipe 'face' is further down, on line 5, so I don't know it yet here. |
having size "big" when size is a number |
'size' needs a number, like size 100. |
having hair "spiky" |
A face's hair can be "short", "curly" or "bald". |
having fill "red" on a face |
A face doesn't have a 'fill'. |
shape face having angle 30 |
Every shape already has an 'angle', so it can't be a setting of 'face'. |
shape face having skin is head.fil |
'head' doesn't have a 'fil' that a setting can change. |
face inside shape face { } |
A recipe can't use itself: 'face' would never finish. |
draw face kim at 0, 0 { ... } |
A face's parts come from its recipe, so I can't add parts here. |
draw circle c inside a recipe |
Inside a shape's { }, leave out 'draw': its parts are drawn when the shape is drawn. |
having inner 5 for a setting that holds a shape |
'inner' needs one of your own shapes, and '5' isn't one. |
having inner (box having wobble 3) |
'box' doesn't have a setting called 'wobble'. |
use face from "Faces" where there is no 'Faces' |
I can't find "Faces". Check the name in the quotes. |
use faec from "Faces" |
I can't find 'faec' in "Faces". It lists the recipes the file has. |