Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. var colors = ['blue', 'red'];
  2.  
  3. var grid = [];
  4. var heroX = 2, posX = 225;
  5. var whereInGrid = 993, offsetY = 0;
  6.  
  7. for (let i=0; i<1000; ++i){
  8. grid[i] = [];
  9. for (let j=0; j<5; ++j){
  10. grid[i][j] = colors[Math.floor(Math.random()*2)]
  11. }
  12. }
  13.  
  14. function update() {
  15. if (posX < heroX*100 + 25){
  16. posX += 5;
  17. }
  18. if (posX > heroX*100 + 25){
  19. posX -= 5;
  20. }
  21. if (offsetY > 0){offsetY -= 5;}
  22. if (offsetY < 0){offsetY += 5;}
  23. }
  24.  
  25. function draw() {
  26. for (let i=-1; i<7; ++i){
  27. for (let j=0; j<5; ++j){
  28. context.fillStyle = grid[whereInGrid + i][j];
  29. context.fillRect(j*100, i*100+offsetY, 99, 99);
  30. }
  31. }
  32. context.fillStyle = 'green';
  33. context.fillRect(posX, 325 , 50, 50);
  34. // This is how you draw a rectangle
  35. };
  36.  
  37. function keyup(key) {
  38. if (posX != 100*heroX+25 || offsetY!=0){return;}
  39. // Show the pressed keycode in the console
  40. if (key == key_left){--heroX;}
  41. if (key == key_right){++heroX;}
  42. if (key == key_up){--whereInGrid; offsetY=-100;}
  43. if (key == key_down){++whereInGrid; offsetY=100;}
  44. console.log("Pressed", key);
  45. };
  46.  
  47. function mouseup() {
  48. // Show coordinates of mouse on click
  49. console.log("Mouse clicked at", mouseX, mouseY);
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement