Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. var myGameArea = {
  2. canvas : document.createElement("canvas"),
  3. start : function() {
  4. this.canvas.width = 600;
  5. this.canvas.height = 480;
  6. //this.canvas.style.cursor = "none"; //hide the original cursor
  7. this.context = this.canvas.getContext("2d");
  8. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  9. this.interval = setInterval(updateGameArea, 1);
  10. window.addEventListener('mousedown', function (e) {
  11. myGameArea.x = e.pageX;
  12. myGameArea.y = e.pageY;
  13. console.log("hello");
  14. });
  15. window.addEventListener('mouseup', function (e) {
  16. myGameArea.x = false;
  17. myGameArea.y = false;
  18. });
  19. },
  20. clear : function(){
  21. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  22. }
  23. }
  24. function component(width, height, color, x, y, type) {
  25. this.type = type;
  26. this.width = width;
  27. this.height = height;
  28. this.speedX = 0;
  29. this.speedY = 0;
  30. this.x = x;
  31. this.y = y;
  32. this.update = function() {
  33. ctx = myGameArea.context;
  34. if (this.type == "text") {
  35. ctx.font = this.width + " " + this.height;
  36. ctx.fillStyle = color;
  37. ctx.fillText(this.text, this.x, this.y);
  38.  
  39. } else {
  40. ctx.fillStyle = color;
  41. ctx.fillRect(this.x, this.y, this.width, this.height);
  42. }
  43.  
  44. }
  45. this.clicked = function() {
  46. var myleft = this.x;
  47. var myright = this.x + (this.width);
  48. var mytop = this.y;
  49. var mybottom = this.y + (this.height);
  50. var clicked = true;
  51. if ((mybottom < myGameArea.y) || (mytop > myGameArea.y) || (myright < myGameArea.x) || (myleft > myGameArea.x)) {
  52. clicked = false;
  53. }
  54. return clicked;
  55. }
  56. }
  57.  
  58. function updateGameArea() {
  59. myGameArea.clear();
  60. if (myGameArea.x && myGameArea.y) {
  61. if (item1.clicked()) {
  62. console.log("red square says hi");
  63. myScore = myScore + 1;
  64. }
  65. if (item2.clicked()) {
  66. console.log("orange square says hi");
  67. }
  68. if (item3.clicked()) {
  69. console.log("yellow square says hi");
  70. }
  71. if (item4.clicked()) {
  72. console.log("green square says hi");
  73. }
  74. if (item5.clicked()) {
  75. console.log("blue square says hi");
  76. }
  77. if (item6.clicked()) {
  78. console.log("purple square says hi");
  79. }
  80. }
  81.  
  82. scoreDisplay.text = "Score: " + myScore;
  83. scoreDisplay.update();
  84. myGamePiece.update();
  85. item1.update();
  86. item2.update();
  87. item3.update();
  88. item4.update();
  89. item5.update();
  90. item6.update();
  91. itemArea.update();
  92. orderScreen.update();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement