Workshop › Flappy-style Bird › Step 4 of 30
A function that draws
What we are doing
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 = '#70c5ce'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'gold'
ctx.beginPath()
ctx.arc(bird.x, bird.y, bird.r, 0, Math.PI * 2)
ctx.fill()
}
draw()
```
What it means
function draw() { ... } defines the recipe; it does not run yet.
- The body is indented by two spaces.
draw() with parentheses runs it. It paints the sky first, which wipes the old picture.
Your turn
Wrap the six drawing lines in function draw() { ... } (indent them by two spaces) and call draw() after it.
Skills: Functions
← Draw the bird A function that moves →
All steps
- Get the canvas and the pen
- Paint the sky
- Draw the bird
- A function that draws
- A function that moves
- The game loop
- Gravity
- A flap
- Space, Up arrow or a tap
- Wait for the first flap
- The ground and the sky
- Tell the player
- A pipe as data
- Draw the pipes
- The world moves, not the bird
- A new pipe at a random height
- A pipe every 90 frames
- Remove the pipes you passed
- Is the bird level with a pipe?
- Through the opening
- Score each pipe once
- Show the score
- A function for a fresh start
- Try again
- One place where the game ends
- Remember the best score
- Show the best score
- The same speed on every screen
- No catching up after a pause
- Build it yourself: a medal