mkknight970 commited on
Commit
2d276da
·
verified ·
1 Parent(s): f49af3a

Upload 4 files

Browse files
Files changed (4) hide show
  1. contact.html +18 -0
  2. index.html +23 -0
  3. script.js +143 -0
  4. style.css +99 -0
contact.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Contact</title>
5
+ <link rel="stylesheet" href="style.css">
6
+ </head>
7
+ <body>
8
+ <div class="container">
9
+ <h1>Contact</h1>
10
+ <p>You can contact me at:</p>
11
+ <ul>
12
+ <li>Email: example@example.com</li>
13
+ <li>Phone: 123-456-7890</li>
14
+ </ul>
15
+ <a href="index.html">Back to Game</a>
16
+ </div>
17
+ </body>
18
+ </html>
index.html ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>2048 Game</title>
5
+ <link rel="stylesheet" href="style.css">
6
+ </head>
7
+ <body>
8
+ <div class="container">
9
+ <h1>2048</h1>
10
+ <h2>Created By M kazi</h2>
11
+ <div class="score-container">
12
+ Score: <span id="score">0</span>
13
+ </div>
14
+ <div class="game-board">
15
+ </div>
16
+ <div class="instructions">
17
+ How to play: Use arrow keys to move the tiles. Tiles with the same number merge when they touch. Add them up to reach 2048!
18
+ </div>
19
+ <a href="contact.html">Contact</a>
20
+ </div>
21
+ <script src="script.js"></script>
22
+ </body>
23
+ </html>
script.js ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const gameBoard = document.querySelector(".game-board");
2
+ const scoreDisplay = document.querySelector("#score");
3
+ let board = [];
4
+ let score = 0;
5
+
6
+ function initBoard() {
7
+ board = [
8
+ [0, 0, 0, 0],
9
+ [0, 0, 0, 0],
10
+ [0, 0, 0, 0],
11
+ [0, 0, 0, 0],
12
+ ];
13
+ addTile();
14
+ addTile();
15
+ renderBoard();
16
+ }
17
+
18
+ function addTile() {
19
+ let emptyTiles = [];
20
+ for (let i = 0; i < 4; i++) {
21
+ for (let j = 0; j < 4; j++) {
22
+ if (board[i][j] === 0) {
23
+ emptyTiles.push({ row: i, col: j });
24
+ }
25
+ }
26
+ }
27
+ if (emptyTiles.length > 0) {
28
+ let randomTile = emptyTiles[Math.floor(Math.random() * emptyTiles.length)];
29
+ board[randomTile.row][randomTile.col] = Math.random() < 0.9 ? 2 : 4;
30
+ }
31
+ }
32
+
33
+ function renderBoard() {
34
+ gameBoard.innerHTML = "";
35
+ for (let i = 0; i < 4; i++) {
36
+ for (let j = 0; j < 4; j++) {
37
+ const tileValue = board[i][j];
38
+ const tile = document.createElement("div");
39
+ tile.classList.add("tile");
40
+ if (tileValue > 0) {
41
+ tile.classList.add(`tile-${tileValue}`);
42
+ tile.textContent = tileValue;
43
+ }
44
+ gameBoard.appendChild(tile);
45
+ }
46
+ }
47
+ scoreDisplay.textContent = score;
48
+ }
49
+
50
+ function moveTiles(direction) {
51
+ let moved = false;
52
+ if (direction === "ArrowUp") {
53
+ for (let j = 0; j < 4; j++) {
54
+ for (let i = 1; i < 4; i++) {
55
+ if (board[i][j] !== 0) {
56
+ let k = i;
57
+ while (k > 0 && board[k - 1][j] === 0) {
58
+ board[k - 1][j] = board[k][j];
59
+ board[k][j] = 0;
60
+ k--;
61
+ moved = true;
62
+ }
63
+ if (k > 0 && board[k - 1][j] === board[k][j]) {
64
+ board[k - 1][j] *= 2;
65
+ score += board[k - 1][j];
66
+ board[k][j] = 0;
67
+ moved = true;
68
+ }
69
+ }
70
+ }
71
+ }
72
+ } else if (direction === "ArrowDown") {
73
+ for (let j = 0; j < 4; j++) {
74
+ for (let i = 2; i >= 0; i--) {
75
+ if (board[i][j] !== 0) {
76
+ let k = i;
77
+ while (k < 3 && board[k + 1][j] === 0) {
78
+ board[k + 1][j] = board[k][j];
79
+ board[k][j] = 0;
80
+ k++;
81
+ moved = true;
82
+ }
83
+ if (k < 3 && board[k + 1][j] === board[k][j]) {
84
+ board[k + 1][j] *= 2;
85
+ score += board[k + 1][j];
86
+ board[k][j] = 0;
87
+ moved = true;
88
+ }
89
+ }
90
+ }
91
+ }
92
+ } else if (direction === "ArrowLeft") {
93
+ for (let i = 0; i < 4; i++) {
94
+ for (let j = 1; j < 4; j++) {
95
+ if (board[i][j] !== 0) {
96
+ let k = j;
97
+ while (k > 0 && board[i][k - 1] === 0) {
98
+ board[i][k - 1] = board[i][k];
99
+ board[i][k] = 0;
100
+ k--;
101
+ moved = true;
102
+ }
103
+ if (k > 0 && board[i][k - 1] === board[i][k]) {
104
+ board[i][k - 1] *= 2;
105
+ score += board[i][k - 1];
106
+ board[i][k] = 0;
107
+ moved = true;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ } else if (direction === "ArrowRight") {
113
+ for (let i = 0; i < 4; i++) {
114
+ for (let j = 2; j >= 0; j--) {
115
+ if (board[i][j] !== 0) {
116
+ let k = j;
117
+ while (k < 3 && board[i][k + 1] === 0) {
118
+ board[i][k + 1] = board[i][k];
119
+ board[i][k] = 0;
120
+ k++;
121
+ moved = true;
122
+ }
123
+ if (k < 3 && board[i][k + 1] === board[i][k]) {
124
+ board[i][k + 1] *= 2;
125
+ score += board[i][k + 1];
126
+ board[i][k] = 0;
127
+ moved = true;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+ if (moved) {
134
+ addTile();
135
+ renderBoard();
136
+ }
137
+ }
138
+
139
+ document.addEventListener("keydown", (event) => {
140
+ moveTiles(event.key);
141
+ });
142
+
143
+ initBoard();
style.css ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ margin: 0;
4
+ display: flex;
5
+ justify-content: center;
6
+ align-items: center;
7
+ min-height: 100vh;
8
+ background-color: #faf8ef;
9
+ }
10
+
11
+ .container {
12
+ text-align: center;
13
+ }
14
+
15
+ .score-container {
16
+ font-size: 1.5em;
17
+ margin-bottom: 20px;
18
+ }
19
+
20
+ .game-board {
21
+ display: grid;
22
+ grid-template-columns: repeat(4, 100px);
23
+ grid-gap: 10px;
24
+ background-color: #bbada0;
25
+ padding: 10px;
26
+ border-radius: 5px;
27
+ }
28
+
29
+ .tile {
30
+ width: 100px;
31
+ height: 100px;
32
+ background-color: #eee4da;
33
+ color: #776e65;
34
+ font-size: 2.5em;
35
+ font-weight: bold;
36
+ display: flex;
37
+ justify-content: center;
38
+ align-items: center;
39
+ border-radius: 5px;
40
+ }
41
+
42
+ .tile.tile-2 {
43
+ background-color: #eee4da;
44
+ }
45
+
46
+ .tile.tile-4 {
47
+ background-color: #ede0c8;
48
+ }
49
+
50
+ .tile.tile-8 {
51
+ background-color: #f2b179;
52
+ color: white;
53
+ }
54
+
55
+ .tile.tile-16 {
56
+ background-color: #f59563;
57
+ color: white;
58
+ }
59
+
60
+ .tile.tile-32 {
61
+ background-color: #f67c5f;
62
+ color: white;
63
+ }
64
+
65
+ .tile.tile-64 {
66
+ background-color: #f65e3b;
67
+ color: white;
68
+ }
69
+
70
+ .tile.tile-128 {
71
+ background-color: #edcf72;
72
+ color: white;
73
+ }
74
+
75
+ .tile.tile-256 {
76
+ background-color: #edcc61;
77
+ color: white;
78
+ }
79
+
80
+ .tile.tile-512 {
81
+ background-color: #edc850;
82
+ color: white;
83
+ }
84
+
85
+ .tile.tile-1024 {
86
+ background-color: #edc53f;
87
+ color: white;
88
+ }
89
+
90
+ .tile.tile-2048 {
91
+ background-color: #edc22e;
92
+ color: white;
93
+ }
94
+
95
+ .instructions {
96
+ margin-top: 20px;
97
+ font-size: 1em;
98
+ color: #776e65;
99
+ }