Advertisement
Guest User

script.js

a guest
May 5th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. class Game {
  2.  
  3. constructor(t,w,h){
  4.  
  5. this.taille = t;
  6. this.case = {w : w, h : h};
  7. this.max = {x : w*t, y : h*t};
  8.  
  9. this.data_mur = [];
  10. this.data_bonus = [];
  11.  
  12. this.translate = { x : this.max.x/2, y : this.max.y/2 };
  13. this.state = 'Game';
  14.  
  15. this.color = {
  16. white : 'rgb(255,255,255)',
  17. green : 'rgb(0,255,0)',
  18. blue : 'rgb(0,255,255)',
  19. red : 'rgb(255,0,0)'
  20. }
  21.  
  22. }
  23.  
  24. // fonctions qui gèrent les éléments du jeu :
  25.  
  26. update(){
  27.  
  28. switch (this.state) {
  29. case 'Game':
  30. for (var i = 0; i < this.data_mur.length; i++) {
  31. if (snake.x == this.data_mur[i].x && snake.y == this.data_mur[i].y) this.data_mur[i].collision();
  32. }
  33.  
  34. for (var i = 0; i < this.data_bonus.length; i++) {
  35. if (snake.x == this.data_bonus[i].x && snake.y == this.data_bonus[i].y) this.data_bonus[i].collision();
  36. }
  37.  
  38. for (var i = 2; i < snake.tail.length; i++) {
  39. if (snake.x == snake.tail[i].x && snake.y == snake.tail[i].y) snake.collision();
  40. }
  41.  
  42. this.draw();
  43. if (Date.now() - snake.time > snake.vitesse) {
  44. snake.time = Date.now();
  45. snake.moove()
  46. }
  47. break;
  48. case 'GameOver':
  49. console.log('Perdu !');
  50. break;
  51. }
  52.  
  53. this.clavier();
  54.  
  55. }
  56.  
  57. draw(){
  58. canvas.width = window.innerWidth;
  59. canvas.height = window.innerHeight;
  60.  
  61. ctx.translate(window.innerWidth/2-this.max.x/2,window.innerHeight/2-this.max.y/2);
  62.  
  63. ctx.fillStyle = '#7F7650';
  64. ctx.fillRect(0, 0, game.max.x,game.max.y);
  65.  
  66. switch (this.state) {
  67. case 'Game':
  68. ctx.font = '30px Verdana';
  69. ctx.textAlign = 'left';
  70. ctx.fillStyle = game.color.white;
  71. ctx.fillText(snake.point,0,this.max.y+30);
  72. // murs
  73. for (var i = 0; i < this.data_mur.length; i++) {
  74. this.data_mur[i].draw();
  75. }
  76. // bonus
  77. for (var i = 0; i < this.data_bonus.length; i++) {
  78. this.data_bonus[i].draw();
  79. }
  80.  
  81. snake.draw();
  82. break;
  83. case 'GameOver':
  84. ctx.font = '30px Verdana';
  85. ctx.textAlign = 'center';
  86. ctx.fillStyle = game.color.white;
  87. ctx.fillText('Game Over !',this.max.x/2,this.max.y/2 - 20);
  88. ctx.font = '20px Verdana';
  89. ctx.fillText('Appuyez sur espace pour rejouer !',this.max.x/2,this.max.y/2 + 20);
  90.  
  91. break;
  92. }
  93.  
  94. }
  95.  
  96. clavier(){
  97.  
  98. switch (this.state) {
  99. case 'Game':
  100.  
  101. if ((keyState[37] || keyState[81]) && snake.dir !== 'd'){
  102. snake.dir = 'g';
  103. }
  104.  
  105. if ((keyState[39] || keyState[68]) && snake.dir !== 'g'){
  106. snake.dir = 'd';
  107. }
  108.  
  109. if ((keyState[38] || keyState[90]) && snake.dir !== 'b') {
  110. snake.dir = 'h';
  111. }
  112.  
  113. if ((keyState[40] || keyState[83]) && snake.dir !== 'h') {
  114. snake.dir = 'b';
  115. }
  116.  
  117. // triche
  118. if (keyState[32]) {
  119. snake.add_tail();
  120. keyState[32] = false;
  121. }
  122.  
  123. break;
  124. case 'GameOver':
  125. if (keyState[32]) {
  126. game.reset();
  127. game.ini();
  128. }
  129. break;
  130. }
  131.  
  132. }
  133.  
  134. // fonctions pour ajouter des éléments au jeu :
  135.  
  136. add_Snake(x,y,color,v,dir){
  137. new Snake(x,y,color,v,dir);
  138. }
  139.  
  140. add_Bonus(x,y,color,name,b){
  141. this.data_bonus.push(new Bonus(x,y,color,name,b));
  142. }
  143.  
  144. add_Mur(x,y,color){
  145. this.data_mur.push(new Murs(x,y,color));
  146. }
  147.  
  148. // fonctions qui génere ou reset les éléments du jeu :
  149.  
  150. reset(){
  151. this.data_mur = [];
  152. this.data_bonus = [];
  153. snake.tail = [];
  154.  
  155. snake.x = 2;
  156. snake.y = 2;
  157. snake.dir = 'd';
  158.  
  159. snake.point = 0;
  160. }
  161.  
  162. ini(){
  163.  
  164. for (var x = 2; x < 13; x++) {
  165. this.add_Mur(x,7,game.color.white);
  166. }
  167.  
  168. this.generate_bonus()
  169.  
  170. snake.tail.push({x : snake.x, y : snake.y});
  171.  
  172. this.state = 'Game';
  173.  
  174. }
  175.  
  176. generate_bonus(){
  177. var x = this.Random(0,game.case.w-1);
  178. var y = this.Random(0,game.case.h-1);
  179.  
  180. var test = false;
  181.  
  182. // pour que le bonus ne se coince pas dans un mur
  183. for (var i = 0; i < this.data_mur.length; i++) {
  184. if (this.data_mur[i].x == x && this.data_mur[i].y == y) {
  185. this.generate_bonus();
  186. test = true;
  187. break;
  188. }
  189. }
  190.  
  191. if (!test) this.add_Bonus(x,y,game.color.red,'Pomme',1);
  192.  
  193. }
  194.  
  195. // fonctions utiles :
  196.  
  197. Random(min, max) {
  198. min = Math.ceil(min);
  199. max = Math.floor(max);
  200. return Math.floor(Math.random() * (max - min +1)) + min;
  201. }
  202.  
  203. }
  204.  
  205. // classe mère des éléments affichés
  206. class Assets {
  207.  
  208. constructor(x,y,color) {
  209. this.x = x;
  210. this.y = y;
  211. this.color = color || 'rgb(255,255,255)';
  212. }
  213.  
  214. draw() {
  215. ctx.fillStyle = this.color;
  216. ctx.fillRect(this.x*game.taille,this.y*game.taille,game.taille,game.taille)
  217. }
  218.  
  219. }
  220.  
  221. // classe snake
  222. class Snake extends Assets {
  223.  
  224. constructor(x,y,color,v,dir) {
  225. super(x,y,color);
  226. this.vitesse = v || 200;
  227. this.point = 0;
  228. this.dir = dir || 'd';
  229. this.tail = [];
  230. this.time = 0;
  231. }
  232.  
  233. draw(){
  234. super.draw();
  235.  
  236. for (var i = 0; i < this.tail.length; i++) {
  237. ctx.fillStyle = game.color.green;
  238. ctx.fillRect(this.tail[i].x*game.taille,this.tail[i].y*game.taille,game.taille,game.taille)
  239. }
  240.  
  241. }
  242.  
  243. moove(){
  244.  
  245. this.moove_tail();
  246.  
  247. switch (this.dir) {
  248. case 'd':
  249. this.x += 1;
  250. break;
  251. case 'g':
  252. this.x -= 1;
  253. break;
  254. case 'b':
  255. this.y += 1;
  256. break;
  257. case 'h':
  258. this.y -= 1;
  259. break;
  260. }
  261.  
  262. this.test_tp();
  263.  
  264. }
  265.  
  266. test_tp(){
  267.  
  268. if (this.x == game.case.w) {
  269. this.x = 0;
  270. }
  271. else if (this.x == -1) {
  272. this.x = game.case.w-1;
  273. }
  274. else if (this.y == game.case.h) {
  275. this.y = 0;
  276. }
  277. else if (this.y == -1) {
  278. this.y = game.case.h-1;
  279. }
  280.  
  281. }
  282.  
  283. moove_tail(){
  284.  
  285. for (var i = this.tail.length-1; i > 0; i--) {
  286. this.tail[i].x = this.tail[i-1].x;
  287. this.tail[i].y = this.tail[i-1].y;
  288. }
  289. this.tail[0].x = snake.x;
  290. this.tail[0].y = snake.y;
  291.  
  292. }
  293.  
  294. add_tail(){
  295. if (this.tail.length > 0) { this.tail.push({x : this.tail[this.tail.length-1].x, y : this.tail[this.tail.length-1].y}); }
  296. }
  297.  
  298. collision(){
  299. game.state = 'GameOver';
  300. }
  301.  
  302. }
  303.  
  304. // classe des bonus
  305. class Bonus extends Assets {
  306.  
  307. constructor(x,y,color,name,b) {
  308. super(x,y,color);
  309. this.name = name;
  310. this.bonus = b || 1;
  311. }
  312.  
  313. collision(){
  314. snake.point += this.bonus;
  315. snake.add_tail();
  316.  
  317. for (var i = 0; i < game.data_bonus.length; i++) {
  318. game.data_bonus.splice(i,1);
  319. }
  320.  
  321. game.generate_bonus();
  322. }
  323.  
  324. }
  325.  
  326. // classe des murs
  327. class Murs extends Assets {
  328.  
  329. constructor(x,y,color) {
  330. super(x,y,color);
  331. }
  332.  
  333. collision(){
  334. game.state = 'GameOver';
  335. }
  336.  
  337. }
  338.  
  339. // key controle (pas réussi à intégré cette fonction dans la classe game)
  340. let keyState = [];
  341. document.addEventListener(
  342. 'keydown',
  343. (event)=>{
  344. keyState[event.keyCode || event.which] = true;
  345. }
  346. );
  347.  
  348. document.addEventListener(
  349. 'keyup',
  350. (event)=>{
  351. keyState[event.keyCode || event.which] = false;
  352. }
  353. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement