Guest User

Untitled

a guest
Sep 26th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Test</title>
  5.         <style>
  6.             #mario{
  7.             background-image:url("mario.png");
  8.             width: 46px;
  9.             height: 64px;
  10.             position: absolute;
  11.             top: 100px;
  12.             left: 100px;
  13.             background-position: 0px 64px;
  14.         }
  15. #cle{
  16.                 background-image:url("key.png");
  17.                 width: 60px;
  18.                 height: 43px;
  19.                 position: absolute;
  20.                 top: 420px;
  21.                 left: 1200px;
  22.             }
  23.  
  24.             body{
  25.                 background-image: url("herbe.jpg");
  26.             }
  27.  
  28.     </style>
  29.     </head>
  30.     <body>
  31.         <div id="cle"></div>
  32.         <div id="carre0"></div>
  33.         <div id="carre1"></div>
  34.         <div id="carre2"></div>
  35.         <div id="carre3"></div>
  36.         <div id="carre4"></div>
  37.         <div id="mario"></div>
  38.     <script>
  39.             var score = 0;
  40.             var star = 0;
  41.             var x=100;
  42.             var y=100;
  43.             var anim=0;
  44.             var toucheHaut = false, toucheDroite = false, toucheBas = false, toucheGauche = false;
  45.            
  46.             var coorX = [0,0,100,2000,2000,200,200,400,200,600,700,300,400,1100,1300,800,800,1200,1100,1300,1600,1800,1800,1300,1700,1500];
  47.             var coorY = [0,100,900,100,700,100,600,700,800,400,700,400,200,100,100,400,600,500,700,700,400,700,200,200,200,300];
  48.             var tailleX = [2080,100,1980,100,100,100,300,100,100,100,300,200,600,200,100,400,200,300,100,400,100,200,100,200,100,100];
  49.             var tailleY = [100,900,100,500,200,400,100,100,100,400,100,100,100,300,100,100,100,100,200,100,300,100,500,200,100,100];
  50.  
  51.  
  52.             window.onkeydown = function(event){
  53.                 if (event.keyCode==38){
  54.                     toucheHaut = true;
  55.                     document.getElementById("mario").style.backgroundPosition = anim * 46 + "px 256px";
  56.                 }
  57.                 else if (event.keyCode==40){
  58.                     toucheBas = true;
  59.                     document.getElementById("mario").style.backgroundPosition = anim * 46 + "px 128px";
  60.                 }
  61.                 else if (event.keyCode==39){
  62.                     toucheDroite = true;
  63.                     document.getElementById("mario").style.backgroundPosition = anim * 46 + "px 192px";
  64.                 }
  65.                 else if (event.keyCode==37){
  66.                     toucheGauche = true;
  67.                     document.getElementById("mario").style.backgroundPosition = anim * 46 + "px 64px";
  68.                 }
  69.                 moveMario();
  70.             }
  71.  
  72.             window.onkeyup = function(event){
  73.                 if (event.keyCode==38){
  74.                     toucheHaut = false;
  75.                 }
  76.                 else if (event.keyCode==40){
  77.                     toucheBas = false;
  78.                 }
  79.                 else if (event.keyCode==39){
  80.                     toucheDroite = false;
  81.                 }
  82.                 else if (event.keyCode==37){
  83.                     toucheGauche = false;
  84.                 }
  85.             }
  86.  
  87.             function moveMario(){
  88.                 animation();
  89.                 var deplacementX = x + (toucheDroite - toucheGauche) * 10
  90.                 var deplacementY = y + (toucheBas - toucheHaut) * 10;
  91.                
  92.                 var collision = false;
  93.  
  94.                
  95.                 if (deplacementX > 0 && deplacementX + 46 < window.innerWidth && deplacementY > 0 && deplacementY + 64 < window.innerHeight){
  96.                     for (var i = 0; i < coorX.length; i++){
  97.                         if (deplacementX + 46 > coorX[i] && deplacementX < coorX[i] + tailleX[i] && deplacementY +64 >= coorY[i] && deplacementY < coorY[i] + tailleY[i]){
  98.                         collision = true;
  99.                         break;
  100.                         }
  101.                
  102.                     }
  103.                     if (collision == false){
  104.                     x = deplacementX;
  105.                     y = deplacementY;
  106.                     }
  107.                     if (deplacementX > 1200 && deplacementX + 46 < 1240 && deplacementY > 420 && deplacementY + 64 < 463){
  108.                     suppCle();
  109.                 }
  110.                 }
  111.  
  112.                 document.getElementById("mario").style.left = x + "px";
  113.                 document.getElementById("mario").style.top = y + "px";
  114.  
  115.             }
  116.  
  117.             function suppCle(){
  118.                 document.removeChild("cle");
  119.             }
  120.  
  121.             function animation(){
  122.                 anim++;
  123.                 if (anim == 6){
  124.                     anim = 0;
  125.                 }
  126.             }
  127.  
  128.  
  129.             for (var i = 0; i < coorX.length; i++){
  130.                 var bloc = document.createElement("div");
  131.                 bloc.style.position = "absolute";
  132.                 bloc.style.top = coorY[i] + "px";
  133.                 bloc.style.left = coorX[i] + "px";
  134.                 bloc.style.width = tailleX[i] + "px";
  135.                 bloc.style.height = tailleY[i] + "px";
  136.                 bloc.style.backgroundImage = "url(file:///C:/Users/Camille/Documents/HTML5/TP1/arbre.png)";
  137.  
  138.                 document.body.appendChild(bloc);
  139.                 }
  140.         </script>
  141.     </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment