Advertisement
Guest User

Untitled

a guest
May 28th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 9.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.     <title>PAKMAN</title>
  6.     <meta charset="UTF-8">
  7.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.     <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
  9.     <style>
  10.         body {
  11.             margin: 0;
  12.             min-height: 100%;
  13.             font-family: 'Press Start 2P';
  14.             font-weight: bold;
  15.             justify-content: center;
  16.             text-align: center;
  17.             display: flex;
  18.             background: #333;
  19.             color: #fff;
  20.         }
  21.  
  22.         h2 {
  23.             margin-top: 5px;
  24.         }
  25.  
  26.         #container {
  27.             width: 960px;
  28.         }
  29.  
  30.         #game {
  31.             position: relative;
  32.         }
  33.  
  34.         #game div {
  35.             position: absolute;
  36.             height: 48px;
  37.             width: 48px;
  38.         }
  39.  
  40.         .vie {
  41.             float: left;
  42.         }
  43.  
  44.         #scoreDiv {
  45.             float: right;
  46.         }
  47.  
  48.         /* COURS 2 */
  49.  
  50.         .mur {
  51.             background: url("mur.png") no-repeat left top;
  52.         }
  53.  
  54.         .sol {
  55.             background: url('sol.png') no-repeat left top;
  56.             z-index: -2;
  57.         }
  58.  
  59.         .gum {
  60.             background: url('gum.png') no-repeat left top;
  61.             z-index: -1;
  62.         }
  63.  
  64.         .diams {
  65.             background: url('diamant.png') no-repeat left top;
  66.             animation: sprite2 .3s steps(2) infinite;
  67.             z-index: -1;
  68.         }
  69.  
  70.         #pac {
  71.             background: url('pac.png') no-repeat left top;
  72.             animation: sprite4 .3s steps(4) infinite;
  73.         }
  74.  
  75.         .fantome {
  76.             animation: sprite3 .3s steps(3) infinite;
  77.  
  78.             background: url('f5.png') no-repeat left top;
  79.             /* important! cela permet d'avoir quoi qu'il arrive une image sur tout les fantômes et d'eviter le bug des "fantomes" invisibles*/
  80.         }
  81.  
  82.         /* OPTION : si on veut que les fantômes aient leur propre apparence, on peut mettre le CSS suivant */
  83.  
  84.         .f1 {
  85.             background: url('f1.png') no-repeat left top;
  86.         }
  87.  
  88.         .f2 {
  89.             background: url('f2.png') no-repeat left top;
  90.         }
  91.  
  92.         .f3 {
  93.             background: url('f3.png') no-repeat left top;
  94.         }
  95.  
  96.         .f4 {
  97.             background: url('f3.png') no-repeat left top;
  98.         }
  99.  
  100.         .fantome.freeze {
  101.             background: url('f-freeze.png') no-repeat left top;
  102.             animation: sprite3 .3s steps(3) infinite;
  103.         }
  104.  
  105.         /*
  106.         Etapes des animations. Mode d'emploi:
  107.         pr une spriteSheet à 2 frames (comme 'diamant.png') utiliser sprite2 dans animations et mettre step(2)
  108.         pr 3 frames comme 'f1.png' utiliser sprite3 et step(3).....etc.
  109.         */
  110.  
  111.         @keyframes sprite2 {
  112.             100% {
  113.                 /* (tailleCase * -2) - tailleCase (le dernier n'apparait que de 100 à 100% ;) */
  114.                 background-position-x: -96px;
  115.             }
  116.         }
  117.  
  118.         @keyframes sprite3 {
  119.             100% {
  120.                 /* -tailleCase * 3*/
  121.                 background-position-x: -144px;
  122.             }
  123.         }
  124.  
  125.         @keyframes sprite4 {
  126.             100% {
  127.                 /* -tailleCase * 4 */
  128.                 background-position-x: -192px;
  129.             }
  130.         }
  131.     </style>
  132.     <script src="jquery.js"></script>
  133.     <script src="fonctions.js"></script>
  134. </head>
  135.  
  136. <body>
  137.     <div id="container">
  138.         <!-- POINTS DE VIE -->
  139.         <img src="vie.png" class="vie">
  140.         <img src="vie.png" class="vie">
  141.         <img src="vie.png" class="vie">
  142.  
  143.         <!-- SCORE -->
  144.         <div id="scoreDiv">SCORE:
  145.             <span id="score">0</span>
  146.         </div>
  147.  
  148.         <!-- TITRE -->
  149.         <h2>PAC MAN</h2>
  150.  
  151.         <!-- MAIN GAME -->
  152.         <div id="game"></div>
  153.     </div>
  154.  
  155.     <script>
  156.         /* Code couleurs pour la map (8 possibilités):
  157.          *
  158.          * 0 == noir   == 000000 == mur
  159.          * 1 == bleu   == 0000ff == chemin
  160.          * 2 == vert   == 00ff00 == gum
  161.          * 3 == jaune  == 00ffff == diams
  162.          * 4 == rouge  == ff0000 == fantome
  163.          * 5 == violet == ff00ff == posDepartPac (pacMan est sur un chemin libre)
  164.          * 6 == orange == ffff00 == custom
  165.          * 7 == blanc  == ffffff == custom
  166.          * + 2 fois plus de pixels peuvent être débloqués avec les pixels transparents
  167.          *
  168.          * */
  169.  
  170.         /* Directions
  171.             h: haut
  172.             b: bas
  173.             g: gauche
  174.             d: droite
  175.             s: stop
  176.         */
  177.  
  178.         // VARIABLES GLOBALES
  179.  
  180.         var fps = 30; // 30fps est pas mal pour un ordinateur peu puissant
  181.         var tailleCase = 48;
  182.  
  183.         // Tableau de fantômes qu'on remplit plus tard
  184.         var fantomes = [];
  185.  
  186.         var speedP = 6;
  187.         var speedF = 6;
  188.  
  189.         var pac = {
  190.             position: [0, 0],
  191.             positionDepart: [0, 0],
  192.             direction: 's',
  193.             nextDirection: 's'
  194.         };
  195.  
  196.         var directions = {
  197.             h: [0, -1], // haut
  198.             d: [1, 0], // droite
  199.             b: [0, 1], // bas
  200.             g: [-1, 0], // gauche
  201.             s: [0, 0] // stop
  202.         };
  203.  
  204.         var directionsInverses = {
  205.             h: 'b',
  206.             d: 'g',
  207.             b: 'h',
  208.             g: 'd',
  209.             s: 's'
  210.         };
  211.  
  212.         var bg = {
  213.             h: 0,
  214.             d: -96,
  215.             b: -144,
  216.             g: -48
  217.         };
  218.  
  219.         // Interpréteur de map en pixels
  220.         // Lire tous les pixels et les transformer en array
  221.         imageToMap('map.png', onLoad);
  222.  
  223.         function onLoad(map) {
  224.  
  225.             var fId = 1;
  226.  
  227.             for (var x = 0; x < map.length; x++) {
  228.                for (var y = 0; y < map[x].length; y++) {
  229.  
  230.                    var n = map[x][y];
  231.  
  232.                    var pos = {
  233.                        left: x * tailleCase,
  234.                        top: y * tailleCase
  235.                    };
  236.  
  237.                    // mur ou sol ?
  238.                    if (n == 0) {
  239.                        $('#game').append($('<div class="mur"></div>').css(pos));
  240.                     } else {
  241.                         $('#game').append($('<div class="sol x' + x + ' y' + y + '"></div>').css(pos));
  242.                     }
  243.  
  244.                     // gum
  245.                     if (n == 2 || n == 4) {
  246.                         $('#game').append($('<div class="gum"></div>').css(pos));
  247.                     }
  248.  
  249.                     // diams
  250.                     if (n == 3) {
  251.                         $('#game').append($('<div class="diams"></div>').css(pos));
  252.                     }
  253.  
  254.                     // fantomes
  255.                     if (n == 4) {
  256.                         $('#game').append($('<div class="fantome f' + fId + ' "></div>').css(pos));
  257.                         // array des fantomes
  258.                         fantomes.push({
  259.                             id: fId,
  260.                             position: [x * tailleCase, y * tailleCase],
  261.                             direction: 's'
  262.                         });
  263.                         fId++; // incrémente le compteur pr les ID de fantomes
  264.                     }
  265.  
  266.                     // Pac
  267.                     if (n == 5) {
  268.                         $('#game').append($('<div id="pac"></div>').css(pos));
  269.                         pac.position = [x * tailleCase, y * tailleCase];
  270.                         pac.positionDepart = [x * tailleCase, y * tailleCase];
  271.                     }
  272.                 }
  273.             }
  274.        
  275.        
  276.             setInterval( function() {
  277.  
  278.                 if (pause) return;
  279.                
  280.                 if (surCase(pac.position)) {
  281.  
  282.                     var casesLibres = checkCases(pac.position);
  283.  
  284.                     if (casesLibres.includes(pac.nextDirection)) {
  285.                         pac.direction = pac.nextDirection;
  286.                         pac.nextDirection = 's';
  287.  
  288.                         // Pour changer la direction (sprite)
  289.                         $('#pac').css({ backgroundPositionY: bg[ pac.direction ]});
  290.                     } else if (!casesLibres.includes(pac.direction)) {
  291.                         pac.direction = 's';
  292.                     }
  293.                 }
  294.  
  295.                 if ( pac.nextDirection == directionsInverses[ pac.direction ]) {
  296.                    
  297.                     pac.direction = pac.nextDirection;
  298.                     $('#pac').css( { backgroundPositionY: bg[ pac.direction ] } );
  299.                     pac.nextDirection = 's';
  300.                 }
  301.  
  302.                 // On calcule la distance parcourue en pixels
  303.                 pac.position[0] += directions[ pac.direction ][ 0 ] * speedP;
  304.                 pac.position[1] += directions[ pac.direction ][ 1 ] * speedP;
  305.  
  306.                 move( $('#pac') , pac.position );
  307.  
  308.             }, 1000 / fps);
  309.         }
  310.    
  311.         function move(elm, pos) {
  312.             var x = pos[0];
  313.             var y = pos[1];
  314.  
  315.             elm.css({
  316.                 left: x,
  317.                 top: y
  318.             });
  319.         }
  320.  
  321.     </script>
  322. </body>
  323. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement