Javascript makes 2048 games

Source: Internet
Author: User

Javascript makes 2048 games

I have been playing the 2048 game over the past few days. I suddenly tried my best to practice the idea of writing a program, so I just imitated it. The current position has not yet achieved dynamic movement, which is not very nice-looking, however, it's pretty cool to play a self-simulated game, haha

 

 

2048. html

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<! DOCTYPE>

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<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>

</Head>

 

<Body>

<Div id = "div2048">

<A id = "start"> tap to start :-) </a>

</Div>

</Body>

</Html>

2048. css

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

@ 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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

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 = 11; 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 = 14; 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 ');

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 );

}

}

The above is all the content in this article. I hope you will like it.

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.