Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Document sans nom</title>
- <link rel="stylesheet" href="Jeu du Snake Javascript.css">
- <script type = "text/javascript">
- var tableau = tab=[1,0,0,2,0,0,0,0,2,0,2,0,0,0,0,3];
- var ligne /*=tr*/ = 4/*document.getElementById('nbligne').value*/;
- var colonne /*=td*/ = 4 /*document.getElementById('nbcolonne').value*/;
- document.onkeydown = checkKey;
- function checkKey(e) {
- e = e || window.event;
- currPos = tab.indexOf(1);
- nextPos = -1;
- if (e.keyCode == '38') {
- // up arrow
- nextPos = currPos-colonne;
- } else if (e.keyCode == '40') {
- // down arrow
- nextPos = currPos+colonne;
- } else if (e.keyCode == '37') {
- // left arrow
- nextPos = currPos-1;
- } else if (e.keyCode == '39') {
- // right arrow
- nextPos = currPos+1;
- }
- if (nextPos != -1) deplacer(currPos,nextPos);
- creationtab();
- }
- function deplacer(currPos, nextPos){
- tab[nextPos]=1;
- tab[currPos]=0;
- console.log("déplacement effectué de "+currPos+" ("+tab[currPos]+") vers "+nextPos+" ("+tab[nextPos]+")");
- }
- function creationtab(){
- code= "<table><tbody>";
- for(i = 0; i<ligne; i++){
- code += "<tr>";
- for(j = 0; j<colonne; j++){
- if(tab[i*colonne+j]==0){code += "<td class='vide'></td>";}
- else if(tab[i*colonne+j]==1){code += "<td class='pos'></td>";}
- else if(tab[i*colonne+j]==2){code += "<td class='bonbon'></td>";}
- else if(tab[i*colonne+j]==3){code += "<td class='arrivee'></td>";}
- }
- code += "</tr>";
- }
- code += "</tbody></table>";
- document.getElementById('plateau').innerHTML = code;
- }
- </script>
- </head>
- <body>
- <div id="plateau">
- <input type="button" value="Lancer le jeu" onclick="javascript:creationtab()">
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement