hammer+lodash实现2048小游戏代码

代码语言:html

所属分类:游戏

代码描述:hammer+lodash实现2048小游戏代码

代码标签: hammerl odash 2048 小游戏 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">

  <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">

<link href="https://fonts.googleapis.com/css?family=Rubik:300,400,700" rel="stylesheet">
<style>
    /*
  Variables:
*/
/**/
*,
*:before,
*:after {
  box-sizing: border-box;
}

button:hover,
a:hover {
  cursor: pointer;
}

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

html {
  min-height: 100%;
  width: 100%;
  font-size: 16px;
  font-family: "Rubik", sans-serif;
  line-height: 1.5em;
  color: #fff;
  background: #160140;
  background: linear-gradient(to top, #160140, #261535);
}

.wrapper {
  max-width: 500px;
  margin: 0 auto;
  padding: 15px;
}

h2 {
  font-style: italic;
}

/* Introduction */
.intro {
  margin-bottom: 60px;
}
.intro_title {
  text-align: center;
  color: #f9d49a;
  font-size: 3rem;
}

/**/
/* Guide instructions */
.guide {
  border-bottom: 1px solid grey;
}
.guide:first-of-type {
  margin-top: 4rem;
  border-top: 1px solid gray;
}
.guide_arrow {
  display: inline-block;
  margin: 15px;
  font-size: 3rem;
  color: #fff;
}

.controls_game, .controls_score {
  display: inline-block;
  width: 50%;
  float: left;
}
@media all and (max-width: 767px) {
  .controls_game, .controls_score {
    width: 100%;
  }
}
.controls_game-btn {
  margin-bottom: 1rem;
  padding: 0.5em 0.75em;
  background: transparent;
  color: #f9d49a;
  outline: 2px solid #f9d49a;
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  border: 5px solid transparent;
  box-shadow: inset 0 0 0px 2px #d4a8cf;
  letter-spacing: 0.1em;
  font-weight: bold;
  text-transform: lowercase;
}
.controls_score {
  display: inline-block;
  min-width: 4em;
  margin-bottom: 4rem;
  padding: 0.5em 0.75em;
  background: #0000003b;
  text-align: center;
  background: linear-gradient(90deg, #f9d49a, #d4a8cf);
}
.controls_score-label, .controls_score-text {
  display: inline-block;
}
.controls_score-label {
  color: initial;
}
.controls_score-text {
  color: #4a3647;
  font-size: 2rem;
}

/**/
/*
  Gameboard:
  the container for the static grid background; and generated tiles/numbers;
*/
.gameboard {
  /* Position: relative; set for tile-container, which absolutely positions over it to match grid's dimensions; */
  position: relative;
  width: 100%;
  max-width: 500px;
  height: 100%;
  max-height: 500px;
  margin: auto;
  padding: 8px;
  background: #ffffff08;
  border-radius: 5px;
  box-shadow: 0 0 8px 0px #f9d49a;
}
.gameboard::before {
  content: "";
  display: block;
  padding-bottom: 100%;
}

/**/
/*
  Grid:
  Creates the static grid background and individual grid cells;
*/
.grid {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  margin: auto;
}
.grid_cell, .tile {
  display: inline-block;
  height: 25%;
  width: 25%;
  padding: 8px;
  float: left;
  background: rgba(238, 228, 218, 0.35);
  background-clip: content-box;
}

/**/
/*
  Tile container:
  Contains the dynamically-generated tiles;
  absolutely positioned over gameboard to match grid dimensions;
*/
.tile-container {
  /* absolutely positioned over gameboard to match dimensions */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border-radius: 5px;
}

.tile {
  display: table;
  background: #eee4da;
  background-clip: content-box;
  position: absolute;
  z-index: 2;
  will-change: top, left;
  transition-property: top, left;
  transition-duration: 0.175s;
  transition-timing-function: ease-out;
}
.tile.initialize {
  -webkit-animation-name: newTile;
          animation-name: newTile;
  -webkit-animation-duration: 0.175s;
          animation-duration: 0.175s;
  -webkit-animation-timing-function: linear;
          animation-timing-function: linear;
  -webkit-animation-fill-mode: forwards;
          animation-fill-mode: forwards;
}
@-webkit-keyframes newTile {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0;
    transform: scale(0);
  }
  75% {
    opacity: 1;
    transform: scale(0.5);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}
@keyframes newTile {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0;
    transform: scale(0);
  }
  75% {
    opacity: 1;
    transform: scale(0.5);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}
.tile_number {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  font-size: 2rem;
  font-weight: bold;
  color: white;
}

.tile:nth-of-type(1) {
  z-index: 2;
}

.tile:nth-of-type(2) {
  z-index: 3;
}

.tile:nth-of-type(3) {
  z-index: 4;
}

.tile:nth-of-type(4) {
  z-index: 5;
}

.tile:nth-of-type(5) {
  z-index: 6;
}

.tile:nth-of-type(6) {
  z-index: 7;
}

.tile:nth-of-type(7) {
  z-index: 8;
}

.tile:nth-of-type(8) {
  z-index: 9;
}

.tile:nth-of-type(9) {
  z-index: 10;
}

.tile:nth-of-type(10) {
  z-index: 11;
}

.tile:nth-of-type(11) {
  z-index: 12;
}

.tile:nth-of-type(12) {
  z-index: 13;
}

.tile:nth-of-type(13) {
  z-index: 14;
}

.tile:nth-of-type(14) {
  z-index: 15;
}

.tile:nth-of-type(15) {
  z-index: 16;
}

.tile:nth-of-type(16) {
  z-index: 17;
}

.tile[data-x="0"][data-y="0"] {
  top: 0%;
  left: 0%;
}

.tile[data-x="0"][data-y="25"] {
  top: 25%;
  left: 0%;
}

.tile[data-x="0"][data-y="50"] {
  top: 50%;
  left: 0%;
}

.tile[data-x="0"][data-y="75"] {
  top: 75%;
  left: 0%;
}

.tile[data-x="0"][data-y="100"] {
  top: 100%;
  left: 0%;
}

.tile[data-x="25"][data-y="0"] {
  top: 0%;
  left: 25%;
}

.tile[data-x="25"][data-y="25"] {
  top: 25%;
  left: 25%;
}

.tile[data-x="25"][data-y="50"] {
  top: 50%;
  left: 25%;
}

.tile[data-x="25"][data-y="75"] {
  top: 75%;
  left: 25%;
}

.tile[data-x="25"][data-y="100"] {
  top: 100%;
  left: 25%;
}

.tile[data-x="50"][data-y="0"] {
  top: 0%;
  left: 50%;
}

.tile[data-x="50"][data-y="25"] {
  top: 25%;
  left: 50%;
}

.tile[data-x="50"][data-y="50"] {
  top: 50%;
  left: 50%;
}

.tile[data-x="50"][data-y="75"] {
  top: 75%;
  left: 50%;
}

.tile[data-x="50"][data-y="100"] {
  top: 100%;
  left: 50%;
}

.tile[data-x="75"][data-y="0"] {
  top: 0%;
  left: 75%;
}

.tile[data-x="75"][data-y="25"] {
  top: 25%;
  left: 75%;
}

.tile[data-x="75"][data-y="50"] {
  top: 50%;
  left: 75%;
}

.tile[data-x="75"][data-y="75"] {
  top: 75%;
  left: 75%;
}

.tile[data-x="75"][data-y="100"] {
  top: 100%;
  left: 75%;
}

.tile[data-x="100"][data-y="0"] {
  top: 0%;
  left: 100%;
}

.tile[data-x="100"][data-y="25"] {
  top: 25%;
  left: 100%;
}

.tile[data-x="100"][data-y="50"] {
  top: 50%;
  left: 100%;
}

.tile[data-x="100"][data-y="75"] {
  top: 75%;
  left: 100%;
}

.tile[data-x="100"][data-y="100"] {
  top: 100%;
  left: 100%;
}

.tile_number[data-value="2"] {
  background: #00d0a4;
  color: #fff;
  box-shadow: 0 0 1px 1px #00d0a4;
}

.tile_number[data-value="4"] {
  background: #dd7373;
  color: #fff;
  box-shadow: 0 0 1px 1px #dd7373;
}

.tile_number[data-value="8"] {
  background: #7d53de;
  color: #fff;
  box-shadow: 0 0 1px 1px #7d53de;
}

.tile_number[data-value="16"] {
  background: #6622cc;
  color: #fff;
  box-shadow: 0 0 1px 1px #6622cc;
}

.tile_number[data-value="32"] {
  background: #00bfb2;
  color: #fff;
  box-shadow: 0 0 1px 1px #00bfb2;
}

.tile_number[data-value="64"] {
  background: #c06ff2;
  color: #fff;
  box-shadow: 0 0 1px 1px #c06ff2;
}

.tile_number[data-value="128"] {
  background: #340068;
  color: #fff;
  box-shadow: 0 0 1px 1px #340068;
}

.tile_number[data-value="256"] {
  background: #3e92cc;
  color: #fff;
  box-shadow: 0 0 1px 1px #3e92cc;
}

.tile_number[data-value="512"] {
  background: #d8315b;
  color: #fff;
  box-shadow: 0 0 1px 1px #d8315b;
}

.tile_number[data-value="1024"] {
  background: #1c0b19;
  color: #fff;
  box-shadow: 0 0 1px 1px #1c0b19;
}

.tile_number[data-value="2048"] {
  background: #1c0b19;
  color: #fff;
  box-shadow: 0 0 1px 1px #1c0b19;
}
</style>

</head>
<body>

<main class="wrapper">
 <!-- intro -->
 <div class="intro clearfix">
  <h1 class="intro_title"> 2048 </h1>
 </div>
 <!-- /end intro -->
 <!-- controls -->
 <div class="controls clearfix">
  <div class="controls_game">
   <button data-js="newGame" class="controls_game-btn"> New Game </button>
  </div>
  <div class="controls_score">
   <span class="controls_score-label">Score </span>
   <br>
   <span class="controls_score-text" data-js="score"> </span>
  </div>
 </div>
 <!-- gameboard -->
 <div id="touchGameboard" class="gameboard">
  <div class="grid">
  </div>
  <div class="tile-container">
  </div>
 </div>
 <!-- /end gameboard -->
 <!-- guide     -->
 <section class="guide clearfix ">
  <h2> What is this? </h2>
  <p> Although coded entirely from scratch, this game is a (lackluster) copy of Gabriele Cirulli's 2048, http://2048game.com/.
  </p>
 </section>
 <section class="guide clearfix">
  <h2> How do I play? </h2>
  <p> Tiles with matching number values can be merged into a single tile, which receives the values' sum.
  </p>
  <p> To move the board, use the directional arrows - or swipe.</p>
  <div class="guide_arrow-wrap">
   <span class="guide_arrow"> &uHar; </span>
   <span class="guide_arrow"> &lHar; </span>
   <span class="guide_arrow"> &rHar; </span>
   <span class="guide_arrow"> &dHar; </span>
  </div>
  <p> To win, get a 2048 tile.
  </p>
 </section>
 <!-- /end guide  -->
</main>
<!-- /end main  -->

<!-- templates -->
<script type="text/html" id="template_grid_cell">
 <div class="grid_cell"></div>
</script>

<script type="text/html" id="template_tile">
 <div class="tile">
  <span class="tile_number"> </span>
 </div>
</script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.3.4.1.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/lodash.4.17.21.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/hammer.min.js"></script>

<script  >
    /**
 * TODO:
 * - Style win/lose, move out of "alert"
 * - Add in previous high score via localstorage
 * - Update footer
 */

/*
* Dependencies:
* Lodash, jQuery, hammerjs
*/

function gameStart() {
  window.game = new Game(4);
  window.game.initialize();
}
$(document).ready(gameStart);

/*
   * Game Board
   */
function Game(size) {
  this.rows = size;
  this.columns = size;
  // board is set as 2d array, with grid cell object for each position
  this.board = [];
  this.boardFlatten = function () {
    return _.flatten(this.board);
  };
  //
  // score setup
  this.score = 0;
  $('[data-js="score"]').html(this.score.toString());
  //
  // flag to check whether any tile movement is in progress;
  this.moveInProgress = false;
  //
}

/**
 * Run all initializations
 */
Game.prototype.initialize = function () {
  // clea.........完整代码请登录后点击上方下载按钮下载查看

网友评论0