Week 9 - Task 2c
Moving the "pixel"
Lets add some stuff to 2b
This is the final part.
Lets make code so that pressing left/right arrows move the pixel!
Javascript
1.
Add the event listener onkeyup
to the body of the document.
Hint: Use the build-in reference to the body element: document.body
2. Add two conditions.
- When left arrow key is pressed: move the pixel left
- When right arrow key is pressed: move the pixel right
Hint: What does "moving a pixel" really mean? Think of it as removing the black color of current "pixel" and adding the black color to the next. You might want to make a function for each.
Hint:
Use console.log(event.key)
to get the name of the arrow keys (which you must use in the conditions)
3. Add special conditions for when we are moving out of boundary.
- When we try to move right of the last "pixel", we should jump to the first
- When we try to move left of the first "pixel", we should jump to the last
Example of how it might look like