Workshop › Snake › Step 6 of 28
A game redraws its picture many times a second. We put the drawing lines in a function named draw, so one word
repaints everything.
```js
function draw() {
ctx.fillStyle = '#111'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'lime'
ctx.fillRect(head.x * CELL, head.y * CELL, CELL, CELL)
}
draw()
```function draw() { ... } defines the recipe; it does not run yet.{ and } are the body, indented by two spaces.draw() with parentheses runs it. Each run paints the background first, which wipes the old picture.Wrap the four drawing lines in function draw() { ... } (indent them by two spaces) and call draw() after it.
Skills: Functions
← Keep the position in a variable A function that moves →