code.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Variables
  2. var posY, pos1, pos2, posX;
  3. var wSize, hSize;
  4. var dim;
  5. var bulletX, bulletY, bulletAcc = 0;
  6. var text = "Esquive !!!";
  7. var nbVague = 0;
  8. var division = 1;
  9. //Fonctions de redimensionnement
  10. function dimensionner() {
  11. if( typeof( window.innerHeight ) == 'number' )
  12. hSize = window.innerHeight;
  13. else if( document.documentElement && document.documentElement.clientHeight )
  14. hSize = document.documentElement.clientHeight;
  15. if( typeof( window.innerWidth ) == 'number' )
  16. wSize = window.innerWidth;
  17. else if( document.documentElement && document.documentElement.clientWidth )
  18. wSize = document.documentElement.clientHeight;
  19. document.getElementById("canvas").height = hSize;
  20. document.getElementById("canvas").width = wSize;
  21. // Etalonnage
  22. pos1 = hSize/4;
  23. pos2 = 3*hSize/4;
  24. dim = hSize / 8;
  25. posX = wSize / 4;
  26. division = wSize / 2000;
  27. // Réinitialisation
  28. posY = pos2;
  29. bulletX = wSize-dim/2;
  30. bulletY = pos1;
  31. }
  32. // Fonctions
  33. function clear() {
  34. context.fillStyle = "black";
  35. context.clearRect(0, 0, wSize, hSize);
  36. context.fillRect(0, 0, wSize-dim/2, hSize-dim/2);
  37. }
  38. function drawPic(x, y) {
  39. context.fillStyle = "rgb(23, 167, 67)";
  40. context.beginPath();
  41. context.moveTo(x, y);// 1er point
  42. context.lineTo(x-dim, y+dim);// 2e point
  43. context.lineTo(x-2*dim, y+dim);// 3e
  44. context.lineTo(x-2*dim, y-dim);// 4e
  45. context.lineTo(x-dim, y-dim);// 5e
  46. context.closePath();
  47. context.fill();
  48. }
  49. function drawBullet(x, y) {
  50. if (bulletY == pos1) context.fillStyle = "rgb(223, 0, 182)";
  51. else context.fillStyle = "rgb(223, 182, 0)";
  52. context.beginPath();
  53. context.moveTo(x, y-dim);// 1er point
  54. context.lineTo(x+dim, y);// 2e point
  55. context.lineTo(x, y+dim);// 3e
  56. context.lineTo(x-dim, y);// 4e
  57. context.closePath();
  58. context.fill();
  59. }
  60. function drawText(x, y, text) {
  61. context.font = "bold 32pt Calibri, Geneva, Arial";
  62. context.fillStyle = "#fff";
  63. context.fillText(text, x, y);
  64. }
  65. // Gestion des évènements
  66. // Variation de tailles
  67. window.onload = function(){ dimensionner() };
  68. window.onresize = function(){ dimensionner() };
  69. // Actions
  70. function jump() {
  71. posY = pos1;
  72. }
  73. function down() {
  74. posY = pos2;
  75. }
  76. // Ordi
  77. document.addEventListener('mousedown', function(e) {
  78. jump();
  79. }, false);
  80. document.addEventListener('mouseup', function(e) {
  81. down();
  82. }, false);
  83. // Smartphone
  84. document.addEventListener('touchstart', function(e) {
  85. jump();
  86. }, false);
  87. document.addEventListener('touchend', function(e) {
  88. down();
  89. }, false);
  90. // Propriétés
  91. var canvas = document.querySelector('#canvas');
  92. var context = canvas.getContext('2d');
  93. // Main
  94. function mainLoop() {
  95. //Nettoyage
  96. clear();
  97. //Ennemi
  98. bulletX -= bulletAcc;
  99. bulletAcc *= 0.99;
  100. if (bulletX < posX) {
  101. if (bulletY == posY) { //Touché !
  102. text = "Game Over";
  103. }
  104. else if (text != "Game Over") { //Retire
  105. nbVague++;
  106. bulletX = wSize-dim/2;
  107. if (Math.floor(Math.random()*2) == 1) bulletY = pos1;
  108. else bulletY = pos2;
  109. }
  110. }
  111. else {
  112. bulletAcc += division;
  113. }
  114. //Affichage
  115. drawPic(posX, posY);
  116. drawBullet(Math.ceil(bulletX), bulletY);
  117. drawText(2, 34, text + " Vague n°" + nbVague);
  118. }
  119. //Loop
  120. setInterval(mainLoop, 20);