JavaScript implementation 2048 Game Example _ basics

Source: Internet
Author: User

Native JavaScript code written in 2048 games. It is recommended to run under Google Browser.

2048.html

Copy Code code as follows:

<! Doctype>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>2048</title>
<link rel= "stylesheet" type= "Text/css" href= "Css/2048.css"/>
<!--<script type= "Text/javascript" src= "Http://code.jquery.com/jquery-latest.js" ></script>-->
<script type= "Text/javascript" src= "Js/2048.js" ></script>

<body>
<div id= "div2048" >
<a id= "Start" >tap to start:-) </a>
</div>
</body>

2048.css

Copy Code code as follows:

@charset "Utf-8";

#div2048
{
width:500px;
height:500px;
Background-color: #b8af9e;
margin:0 Auto;
position:relative;
}
#start
{
width:500px;
height:500px;
line-height:500px;
Display:block;
Text-align:center;
font-size:30px;
Background: #f2b179;
Color: #FFFFFF;
}
#div2048 Div.tile
{
margin:20px 0px 0px 20px;
width:100px;
height:40px;
padding:30px 0;
font-size:40px;
line-height:40px;
Text-align:center;
Float:left;
}
#div2048 div.tile0{
Background: #ccc0b2;
}
#div2048 Div.tile2
{
Color: #7c736a;
Background: #eee4da;
}
#div2048 div.tile4
{
Color: #7c736a;
Background: #ece0c8;
}
#div2048 Div.tile8
{
Color: #fff7eb;
Background: #f2b179;
}
#div2048 Div.tile16
{
Color: #fff7eb;
Background: #f59563;
}
#div2048 div.tile32
{
Color: #fff7eb;
Background: #f57c5f;
}
#div2048 div.tile64
{
Color: #fff7eb;
Background: #f65d3b;
}
#div2048 div.tile128
{
Color: #fff7eb;
Background: #edce71;
}
#div2048 div.tile256
{
Color: #fff7eb;
Background: #edcc61;
}
#div2048 div.tile512
{
Color: #fff7eb;
Background: #ecc850;
}
#div2048 div.tile1024
{
Color: #fff7eb;
Background: #edc53f;
}
#div2048 div.tile2048
{
Color: #fff7eb;
Background: #eec22e;
}

2048.js

Copy Code code as follows:

function game2048 (Container)
{
This.container = container;
This.tiles = new Array (16);
}

Game2048.prototype = {
Init:function () {
for (var i = 0, len = this.tiles.length i < len; i++) {
var tile = this.newtile (0);
Tile.setattribute (' index ', i);
This.container.appendChild (tile);
This.tiles[i] = tile;
}
This.randomtile ();
This.randomtile ();
},
Newtile:function (val) {
var tile = document.createelement (' div ');
This.settileval (Tile, Val)
return tile;
},
Settileval:function (Tile, val) {
Tile.classname = ' Tile tile ' + val;
Tile.setattribute (' Val ', Val);
tile.innerhtml = val > 0? Val: ';
},
Randomtile:function () {
var zerotiles = [];
for (var i = 0, len = this.tiles.length i < len; i++) {
if (This.tiles[i].getattribute (' val ') = = 0) {
Zerotiles.push (This.tiles[i]);
}
}
var rtile = Zerotiles[math.floor (Math.random () * zerotiles.length)];
This.settileval (Rtile, Math.random () < 0.8? 2:4);
},
Move:function (direction) {
var J;
switch (direction) {
Case ' W ':
for (var i = 4, Len = This.tiles.length i < len; i++) {
j = i;
while (J >= 4) {
This.merge (This.tiles[j-4], this.tiles[j]);
J-= 4;
}
}
Break
Case ' S ':
for (var i = one; I >= 0; i--) {
j = i;
while (J <= 11) {
This.merge (This.tiles[j + 4], this.tiles[j]);
j + + 4;
}
}
Break
Case ' A ':
for (var i = 1, len = This.tiles.length i < len; i++) {
j = i;
while (j% 4!= 0) {
This.merge (This.tiles[j-1], this.tiles[j]);
J-= 1;
}
}
Break
Case ' D ':
for (var i = i >= 0; i--) {
j = i;
while (j% 4!= 3) {
This.merge (This.tiles[j + 1], this.tiles[j]);
j + + 1;
}
}
Break
}
This.randomtile ();
},
Merge:function (Prevtile, Currtile) {
var prevval = Prevtile.getattribute (' Val ');
var currval = Currtile.getattribute (' Val ');
if (currval!= 0) {
if (Prevval = = 0) {
This.settileval (Prevtile, currval);
This.settileval (currtile, 0);
}
else if (Prevval = = Currval) {
This.settileval (Prevtile, Prevval * 2);
This.settileval (currtile, 0);
}
}
},
Equal:function (Tile1, Tile2) {
Return Tile1.getattribute (' val ') = = Tile2.getattribute (' Val ');
},
Max:function () {
for (var i = 0, len = this.tiles.length i < len; i++) {
if (This.tiles[i].getattribute (' val ') = = 2048) {
return true;
}
}
},
Over:function () {
for (var i = 0, len = this.tiles.length i < len; i++) {
if (This.tiles[i].getattribute (' val ') = = 0) {
return false;
}
if (i% 4!= 3) {
if (This.equal (This.tiles[i], this.tiles[i + 1])) {
return false;
}
}
if (I < 12) {
if (This.equal (This.tiles[i], This.tiles[i + 4])) {
return false;
}
}
}
return true;
},
Clean:function () {
for (var i = 0, len = this.tiles.length i < len; i++) {
This.container.removeChild (This.tiles[i]);
}
This.tiles = new Array (16);
}
}

var game, Startbtn;

Window.onload = function () {
 var container = document.getElementById (' div2048 ');
&NBSP;STARTBTN = document.getElementById (' start ');
 startbtn.onclick = function () {
  this.style.display = ' none ';
  game = Game | | New game2048 (container);
  game.init ();
 }
}

Window.onkeydown = function (e) {
var keynum, Keychar;
if (window.event) {//IE
Keynum = E.keycode;
}
else if (E.which) {//Netscape/firefox/opera
Keynum = E.which;
}
Keychar = String.fromCharCode (keynum);
if ([' W ', ' S ', ' A ', ' D '].indexof (Keychar) >-1) {
if (Game.over ()) {
Game.clean ();
StartBtn.style.display = ' block ';
startbtn.innerhtml = ' Game over, replay? '
Return
}
Game.move (Keychar);
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.