Advertisement
Guest User

space invaders

a guest
Feb 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. (function(){
  2. let Game = function(canvasId){
  3. let canvas = document.getElementById(canvasId);
  4. let screen = canvas.getContext('2d');
  5. const gameSize = {
  6. x: canvas.width,
  7. y: canvas.height
  8. };
  9.  
  10. this.bodies = createInvaders(this).concat([new Player(this,gameSize)]);
  11.  
  12. let self = this;
  13.  
  14. loadSound("shoot.wav", function(shootSound){
  15. self.shootSound = shootSound;
  16. let tick = function(){
  17. self.update(gameSize);
  18. self.draw(screen,gameSize);
  19. requestAnimationFrame(tick);
  20. }
  21. tick();
  22. });
  23. }
  24.  
  25. Game.prototype = {
  26. update: function(gameSize){
  27. let bodies = this.bodies;
  28. let notCollidingWithAnything = function(b1){
  29. return bodies.filter(function(b2){
  30. return colliding(b1,b2);
  31. }).length == 0;
  32. }
  33. this.bodies = this.bodies.filter(notCollidingWithAnything);
  34. for(let i = 0; i < this.bodies.length; i++){
  35. if(this.bodies[i].position.y < 0){
  36. this.bodies.splice(i,1);
  37. }
  38. }
  39. for(let i = 0; i < this.bodies.length; i++){
  40. this.bodies[i].update();
  41. }
  42. },
  43. draw: function(screen,gameSize){
  44. clearCanvas(screen,gameSize)
  45. for(let i = 0; i < this.bodies.length; i++){
  46. drawRect(screen,this.bodies[i])
  47. }
  48. },
  49. addBody: function(body){
  50. this.bodies.push(body);
  51. },
  52. invadersBelow: function(invader){
  53. return this.bodies.filter(function(b){
  54. return b instanceof Invader &&
  55. b.position.y > invader.position.y &&
  56. b.position.x - invader.position.x < invader.size.width;
  57. }).length > 0;
  58. }
  59. }
  60.  
  61. let Invader = function(game,position){
  62. this.game = game;
  63. this.size = {width: 16, height:16};
  64. this.position = position;
  65. this.patrolX = 0;
  66. this.speedX = 1;
  67. }
  68.  
  69. Invader.prototype = {
  70. update: function(){
  71. if(this.patrolX < 0 || this.patrolX > 500){
  72. this.speedX = -this.speedX;
  73. }
  74. this.position.x += this.speedX;
  75. this.patrolX += this.speedX;
  76.  
  77. if(Math.random()<0.01 && !this.game.invadersBelow(this)){
  78. let bullet = new Bullet({x:this.position.x+this.size.width/2 - 3/2, y:this.position.y + this.size.height/2},
  79. {x: Math.random()-0.5 , y:2});
  80. this.game.addBody(bullet);
  81. this.bullets++;
  82. }
  83. }
  84. }
  85.  
  86.  
  87. let Player = function(game,gameSize){
  88. this.bullets = 0;
  89. this.timer = 0;
  90. this.game = game;
  91. this.size = {width:16, height:16}
  92. this.position = {x: gameSize.x/2-this.size.width/2, y:gameSize.y/2-this.size.height/2};
  93. this.keyboarder = new Keyboarder();
  94. }
  95.  
  96. Player.prototype = {
  97. update: function(){
  98. if(this.keyboarder.isDown(this.keyboarder.KEYS.LEFT)){
  99. this.position.x -= 2;
  100. }
  101. if(this.keyboarder.isDown(this.keyboarder.KEYS.RIGHT)){
  102. this.position.x += 2;
  103. }
  104. if(this.keyboarder.isDown(this.keyboarder.KEYS.SPACE)){
  105. if(this.bullets < 5){
  106. let bullet = new Bullet({x:this.position.x+this.size.width/2 - 3/2, y:this.position.y - 4},
  107. {x:0,y:-6});
  108. this.game.addBody(bullet);
  109. this.bullets++;
  110. this.game.shootSound.load();
  111. this.game.shootSound.play();
  112. }
  113. }
  114. this.timer++;
  115. if(this.timer % 12 == 0){
  116. this.bullets = 0;
  117. }
  118. }
  119.  
  120. }
  121. let Bullet = function(position,velocity){
  122. this.size = {width:3, height:3}
  123. this.position = position;
  124. this.velocity = velocity;
  125. }
  126.  
  127. Bullet.prototype = {
  128. update: function(){
  129. this.position.x += this.velocity.x;
  130. this.position.y += this.velocity.y;
  131. }
  132. }
  133.  
  134.  
  135. let Keyboarder = function(){
  136. const keyState = {};
  137.  
  138. window.onkeydown = function(e){
  139. keyState[e.keyCode] = true;
  140. }
  141. window.onkeyup = function(e){
  142. keyState[e.keyCode] = false;
  143. }
  144. this.isDown = function(keyCode){
  145. return keyState[keyCode] === true;
  146. }
  147. this.KEYS = {LEFT: 37, RIGHT: 39, SPACE: 32};
  148. }
  149.  
  150. let createInvaders = function(game){
  151. const invaders = [];
  152. for(let i = 0; i < 24; i++){
  153. let x = 30 + (i%8) * 30;
  154. let y = 30 + (i%3) * 30;
  155. invaders.push(new Invader(game,{x:x,y:y}));
  156. }
  157. return invaders;
  158. }
  159.  
  160. let colliding = function(b1, b2) {
  161. return !(b1 == b2 ||
  162. b1.position.x + b1.size.width/2 < b2.position.x - b2.size.width / 2 ||
  163. b1.position.y + b1.size.height/2 < b2.position.y - b2.size.height / 2 ||
  164. b1.position.x - b1.size.width/2 > b2.position.x + b2.size.width /2 ||
  165. b1.position.y - b1.size.height/2 > b2.position.y + b2.size.height/2);
  166. }
  167.  
  168. let loadSound = function(url, callback){
  169. let loaded = function(){
  170. callback(sound);
  171. sound.removeEventListener("canplaythrough",loaded);
  172. }
  173. let sound = new Audio(url);
  174. sound.addEventListener("canplaythrough",loaded);
  175. sound.load();
  176. }
  177.  
  178.  
  179. // return (b1 != b2 &&
  180. // b1.position.x < b2.position.x + b2.size.width
  181. // && b1.position.x + b1.size.width > b2.position.x
  182. // && b1.position.y < b2.position.y + b2.size.height
  183. // && b1.position.y + b1.size.height > b2.position.y);
  184.  
  185.  
  186. let drawRect = function(screen,body){
  187. screen.fillRect(body.position.x, body.position.y, body.size.width, body.size.height);
  188. }
  189.  
  190. let clearCanvas = function(screen,gameSize){
  191. screen.clearRect(0,0,gameSize.x,gameSize.y)
  192. }
  193.  
  194. window.onload = function(){
  195. new Game("screen")
  196. }
  197. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement