Advertisement
Guest User

Untitled

a guest
May 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. var canvas= document.getElementById("monCanvas");
  2. var ctx = canvas.getContext("2d");
  3.  
  4. var hero = {
  5. x:40,
  6. y:canvas.height/2,
  7. life:5
  8. }
  9.  
  10. var game = {
  11. gravity:0.8,
  12. up:-0.8,
  13. sy:0,
  14. dt:0.5,
  15. }
  16.  
  17. var keysDown = {};
  18.  
  19. var missilesArray = [];
  20. var ennemisArray= [];
  21.  
  22. addEventListener("keydown", function (e) {
  23. keysDown[e.keyCode] = true;
  24. }, false);
  25.  
  26. addEventListener("keyup", function (e) {
  27. delete keysDown[e.keyCode];
  28. }, false);
  29.  
  30.  
  31.  
  32.  
  33. console.log(ennemisArray);
  34.  
  35. animate();
  36.  
  37.  
  38. function Missiles(posX, posY, w, h){
  39. this.x = posX,
  40. this.y = posY,
  41. this.w = w,
  42. this.h = h
  43. }
  44.  
  45. function Ennemis(posX, posY, w, h){
  46. this.x = posX,
  47. this.y = posY,
  48. this.w = w,
  49. this.h = h
  50. }
  51.  
  52. function animate(){
  53. ctx.clearRect(0, 0, canvas.width, canvas.height);
  54. draw();
  55. update();
  56. requestAnimationFrame(animate);
  57. }
  58.  
  59. function drawBalle(){
  60.  
  61. ctx.fillStyle="#0AE6E9";
  62. ctx.beginPath();
  63. ctx.arc(hero.x, hero.y, 20, 0, 2*Math.PI);
  64. ctx.fill();
  65.  
  66.  
  67. }
  68.  
  69. function draw(){
  70.  
  71. drawBalle();
  72.  
  73. for(i=0;i<missilesArray.length;i++){
  74. ctx.fillStyle="#0AE6E9";
  75. ctx.fillRect(missilesArray[i].x+=5,missilesArray[i].y,missilesArray[i].w,missilesArray[i].h);}
  76.  
  77. for(i=0;i<ennemisArray.length;i++){
  78. ctx.fillStyle="pink";
  79. ctx.fillRect(ennemisArray[i].x+=5,ennemisArray[i].y,ennemisArray[i].w,ennemisArray[i].h);}
  80.  
  81. }
  82.  
  83. function update(){
  84.  
  85. game.up=-0.8;
  86. game.sy += game.gravity* game.dt;
  87. hero.y+=game.sy*game.dt;
  88.  
  89. if(hero.y>canvas.height+50){respawn();}
  90.  
  91. if(hero.y<0){hero.y=0; game.up=5;}
  92.  
  93. if(32 in keysDown){game.sy += game.up;}
  94.  
  95. window.setInterval(createEnnemis(), 5000);
  96.  
  97. }
  98.  
  99. function respawn(){
  100.  
  101. hero.y=canvas.height/2;
  102. hero.life=hero.life-1;
  103. game.sy=0;
  104.  
  105.  
  106. }
  107.  
  108.  
  109. canvas.addEventListener("click", souris);
  110.  
  111. function souris(e){
  112. missilesArray.push(new Missiles(hero.x, hero.y, 15, 5));
  113. //console.log(sourisX, sourisY)
  114. }
  115.  
  116.  
  117. function createEnnemis(){
  118. ennemisArray.push(new Ennemis(0, canvas.height/2, 15, 5));
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement