gameScript.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var myGamePiece;
  2. var myObstacles = [];
  3. var myScore;
  4. function startGame() {
  5. myGamePiece = new component(30, 30, "red", 10, 120);
  6. myGamePiece.gravity = 0.05;
  7. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  8. myGameArea.start();
  9. }
  10. var myGameArea = {
  11. canvas : document.createElement("canvas"),
  12. start : function() {
  13. this.canvas.width = 480;
  14. this.canvas.height = 270;
  15. this.context = this.canvas.getContext("2d");
  16. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  17. this.frameNo = 0;
  18. this.interval = setInterval(updateGameArea, 20);
  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.score = 0;
  27. this.width = width;
  28. this.height = height;
  29. this.speedX = 0;
  30. this.speedY = 0;
  31. this.x = x;
  32. this.y = y;
  33. this.gravity = 0;
  34. this.gravitySpeed = 0;
  35. this.update = function() {
  36. ctx = myGameArea.context;
  37. if (this.type == "text") {
  38. ctx.font = this.width + " " + this.height;
  39. ctx.fillStyle = color;
  40. ctx.fillText(this.text, this.x, this.y);
  41. } else {
  42. ctx.fillStyle = color;
  43. ctx.fillRect(this.x, this.y, this.width, this.height);
  44. }
  45. }
  46. this.newPos = function() {
  47. this.gravitySpeed += this.gravity;
  48. this.x += this.speedX;
  49. this.y += this.speedY + this.gravitySpeed;
  50. this.hitBottom();
  51. }
  52. this.hitBottom = function() {
  53. var rockbottom = myGameArea.canvas.height - this.height;
  54. if (this.y > rockbottom) {
  55. this.y = rockbottom;
  56. this.gravitySpeed = 0;
  57. }
  58. }
  59. this.crashWith = function(otherobj) {
  60. var myleft = this.x;
  61. var myright = this.x + (this.width);
  62. var mytop = this.y;
  63. var mybottom = this.y + (this.height);
  64. var otherleft = otherobj.x;
  65. var otherright = otherobj.x + (otherobj.width);
  66. var othertop = otherobj.y;
  67. var otherbottom = otherobj.y + (otherobj.height);
  68. var crash = true;
  69. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  70. crash = false;
  71. }
  72. return crash;
  73. }
  74. }
  75. function updateGameArea() {
  76. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  77. for (i = 0; i < myObstacles.length; i += 1) {
  78. if (myGamePiece.crashWith(myObstacles[i])) {
  79. return;
  80. }
  81. }
  82. myGameArea.clear();
  83. myGameArea.frameNo += 1;
  84. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  85. x = myGameArea.canvas.width;
  86. minHeight = 20;
  87. maxHeight = 200;
  88. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  89. minGap = 50;
  90. maxGap = 200;
  91. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  92. myObstacles.push(new component(10, height, "green", x, 0));
  93. myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  94. }
  95. for (i = 0; i < myObstacles.length; i += 1) {
  96. myObstacles[i].x += -1;
  97. myObstacles[i].update();
  98. }
  99. myScore.text="SCORE: " + myGameArea.frameNo;
  100. myScore.update();
  101. myGamePiece.newPos();
  102. myGamePiece.update();
  103. }
  104. function everyinterval(n) {
  105. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  106. return false;
  107. }
  108. function accelerate(n) {
  109. myGamePiece.gravity = n;
  110. }