Skip to content

ironhack-dev-squad-127/lab-canvas-treasure-hunt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo_ironhack_blue 7

JS | Canvas Treasure Hunt

Introduciton

You would like to implement a simple treasure hunt.

You can find a demo here: https://ironhack-dev-squad-127.github.io/lab-canvas-treasure-hunt/solution-code/

Iteration 1

The goal is to reproduce the following grid.

For this, you will need to create a file index.html with the following code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>JS | Canvas Treasure Hunt</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <canvas width="500" height="500"></canvas>
  <script src="main.js"></script>
</body>
</html>

You will also have to create a file main.js like the following one:

// main.js
var canvas = document.querySelector('canvas')
var ctx = canvas.getContext('2d')
var width = canvas.width
var height = canvas.height

// Iteration 1
function drawGrid() {
  // TODO: write the code of the function
}

function drawEverything() {
  drawGrid()
  // drawPlayer()
  // drawTreasure()
}

drawEverything()

To finish this iteration, you have to code the code of the function drawGrid.

Iteration 2

Now, you have to create a class Character. You should define at least:

  • a col property
  • a row property
  • a moveUp() method
  • a moveRight() method
  • a moveDown() method
  • a moveLeft() method

You have below an example of what you should happen.

var player = new Character(0,0) // (0,0) = Initial position
player.moveDown() // Increase by 1 the value of player.row
player.moveDown() // Increase by 1 the value of player.row
player.moveRight() // Increase by 1 the value of player.col
console.log(player.col, player.row) // => 1,2

Iteration 3

Create a function drawPlayer that displays the player on the canvas based on its col and row values.

You can simply rely on images/character-down.png for this iteration.

Iteration 4

  • Create a class Treasure with a method setRandomPosition() and a property col and row
  • Create a function drawTreasure() that displays the treasure on the canvas. The picture is images/treasure.png

Iteration 5

Listen for keydown events to:

  • Update the player's coordinates
  • Draw everything again (drawEverything())

For this you take inspiration of the following code.

document.onkeydown = function(e) {
  e.preventDefault() // Stop the default behavior (moving the screen to the left/up/right/down)
  switch (e.keyCode) {
    case 37: 
      console.log('left')
      break
    case 38: 
      console.log('up')  
      break
    case 39: 
      console.log('right')
      break
    case 40: 
      console.log('down')
      break
  }
}

Iteration 6

Do what you want to make this game awesome!

Here are some ideas:

  • Add a property direction to the player and display a different image based on the direction
  • Stop the player at the edges of the board
  • Add another player that can be controlled with different keys (WASD)
  • Add a score property to the player and create a function drawScores

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published