Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Document sans nom</title>
  6. <link rel="stylesheet" href="Jeu du Snake Javascript.css">
  7.  
  8. <script type = "text/javascript">
  9. var tableau = tab=[1,0,0,2,0,0,0,0,2,0,2,0,0,0,0,3];
  10. var ligne /*=tr*/ = 4/*document.getElementById('nbligne').value*/;
  11. var colonne /*=td*/ = 4 /*document.getElementById('nbcolonne').value*/;
  12. document.onkeydown = checkKey;
  13.  
  14. function checkKey(e) {
  15. e = e || window.event;
  16. currPos = tab.indexOf(1);
  17. nextPos = -1;
  18. if (e.keyCode == '38') {
  19. // up arrow
  20. nextPos = currPos-colonne;
  21. } else if (e.keyCode == '40') {
  22. // down arrow
  23. nextPos = currPos+colonne;
  24. } else if (e.keyCode == '37') {
  25. // left arrow
  26. nextPos = currPos-1;
  27. } else if (e.keyCode == '39') {
  28. // right arrow
  29. nextPos = currPos+1;
  30. }
  31. if (nextPos != -1) deplacer(currPos,nextPos);
  32. creationtab();
  33. }
  34.  
  35. function deplacer(currPos, nextPos){
  36. tab[nextPos]=1;
  37. tab[currPos]=0;
  38. console.log("déplacement effectué de "+currPos+" ("+tab[currPos]+") vers "+nextPos+" ("+tab[nextPos]+")");
  39. }
  40.  
  41. function creationtab(){
  42. code= "<table><tbody>";
  43. for(i = 0; i<ligne; i++){
  44. code += "<tr>";
  45. for(j = 0; j<colonne; j++){
  46. if(tab[i*colonne+j]==0){code += "<td class='vide'></td>";}
  47. else if(tab[i*colonne+j]==1){code += "<td class='pos'></td>";}
  48. else if(tab[i*colonne+j]==2){code += "<td class='bonbon'></td>";}
  49. else if(tab[i*colonne+j]==3){code += "<td class='arrivee'></td>";}
  50. }
  51. code += "</tr>";
  52. }
  53. code += "</tbody></table>";
  54. document.getElementById('plateau').innerHTML = code;
  55. }
  56. </script>
  57.  
  58. </head>
  59.  
  60. <body>
  61. <div id="plateau">
  62. <input type="button" value="Lancer le jeu" onclick="javascript:creationtab()">
  63. </div>
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement