Snake project

Completed example (https://codepen.io/arkev/pen/pDdoL?editors=0010)

https://codepen.io/davmarksman/pen/YzVvmBL

Instructions

1. Create canvas

<canvas id="canvas" width="400" height="400"></canvas>

2. Check created correctly. Explain variables. Case is important

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = 400;
const height = 400;
// other way of declaring variable is let. will explain later

ctx.fillStyle = "white";
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "black";
ctx.strokeRect(0, 0, width, height);

3. Function

4. Paint a cell. Demo in Paint what will do

  • Resize canvas

  • Draw 2d shape. Line type and fill

Explain Coordinate system and how it matches up to pixels

4.5 Checkpoint

5. Create a snake

Explain for loop (instead of manually typing, computer count) and arrays

6. Draw snake

7. Lets make the snake move

8. Lets make the snake move in different directions

9. Lets End the game if you hit a boundary

There are two cases in which the game can end:

  • The head of the snake collides with its body.

  • The head of the snake collides with the canvas boundary.

10. Lets add food

Last updated

Was this helpful?