Skip to content

Commit 2af4c4e

Browse files
committed
add game restart confirmation
1 parent c870b2a commit 2af4c4e

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ <h1 class="title">2048</h1>
3737
<div class="lower">
3838
<a class="keep-playing-button">Keep going</a>
3939
<a class="retry-button">Try again</a>
40+
<a class="confirm-button">OK</a>
41+
<a class="cancel-button">Cancel</a>
4042
</div>
4143
</div>
4244

js/game_manager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function GameManager(size, InputManager, Actuator, StorageManager) {
88

99
this.inputManager.on("move", this.move.bind(this));
1010
this.inputManager.on("restart", this.restart.bind(this));
11+
this.inputManager.on("restartWithConfirmation", this.restartWithConfirmation.bind(this));
1112
this.inputManager.on("keepPlaying", this.keepPlaying.bind(this));
1213

1314
this.setup();
@@ -20,6 +21,13 @@ GameManager.prototype.restart = function () {
2021
this.setup();
2122
};
2223

24+
// Restart the game after user confirmation
25+
26+
GameManager.prototype.restartWithConfirmation = function () {
27+
// Open confirm message
28+
this.actuator.promptRestart();
29+
};
30+
2331
// Keep playing after winning (allows going over 2048)
2432
GameManager.prototype.keepPlaying = function () {
2533
this.keepPlaying = true;

js/html_actuator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ HTMLActuator.prototype.message = function (won) {
132132
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
133133
};
134134

135+
HTMLActuator.prototype.promptRestart = function () {
136+
var message = "Start a new game?";
137+
this.messageContainer.classList.add("restart-game");
138+
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
139+
};
140+
135141
HTMLActuator.prototype.clearMessage = function () {
136142
// IE only takes one value to remove at a time.
137143
this.messageContainer.classList.remove("game-won");
138144
this.messageContainer.classList.remove("game-over");
145+
this.messageContainer.classList.remove("restart-game");
139146
};

js/keyboard_input_manager.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ KeyboardInputManager.prototype.listen = function () {
7070

7171
// Respond to button presses
7272
this.bindButtonPress(".retry-button", this.restart);
73-
this.bindButtonPress(".restart-button", this.restart);
73+
this.bindButtonPress(".restart-button", this.restartWithConfirmation);
7474
this.bindButtonPress(".keep-playing-button", this.keepPlaying);
75+
this.bindButtonPress(".confirm-button", this.restart);
76+
this.bindButtonPress(".cancel-button", this.keepPlaying);
7577

7678
// Respond to swipe events
7779
var touchStartClientX, touchStartClientY;
@@ -132,6 +134,11 @@ KeyboardInputManager.prototype.restart = function (event) {
132134
this.emit("restart");
133135
};
134136

137+
KeyboardInputManager.prototype.restartWithConfirmation = function (event) {
138+
event.preventDefault();
139+
this.emit("restartWithConfirmation");
140+
};
141+
135142
KeyboardInputManager.prototype.keepPlaying = function (event) {
136143
event.preventDefault();
137144
this.emit("keepPlaying");

style/main.css

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,30 @@ hr {
188188
height: 40px;
189189
line-height: 42px;
190190
margin-left: 9px; }
191-
.game-container .game-message a.keep-playing-button {
191+
.game-container .game-message a.keep-playing-button, .game-container .game-message a.confirm-button, .game-container .game-message a.cancel-button {
192192
display: none; }
193193
.game-container .game-message.game-won {
194194
background: rgba(237, 194, 46, 0.5);
195195
color: #f9f6f2; }
196196
.game-container .game-message.game-won a.keep-playing-button {
197197
display: inline-block; }
198-
.game-container .game-message.game-won, .game-container .game-message.game-over {
198+
.game-container .game-message.game-won, .game-container .game-message.game-over, .game-container .game-message.restart-game {
199199
display: block; }
200200

201+
.game-container .game-message.restart-game {
202+
-webkit-animation: fade-in 400ms ease 100ms;
203+
-moz-animation: fade-in 400ms ease 100ms;
204+
animation: fade-in 400ms ease 100ms;
205+
-webkit-animation-fill-mode: both;
206+
-moz-animation-fill-mode: both;
207+
animation-fill-mode: both; }
208+
.game-container .game-message.restart-game .lower {
209+
margin-top: 85px; }
210+
.game-container .game-message.restart-game .lower a.confirm-button, .game-container .game-message.restart-game .lower a.cancel-button {
211+
display: inline-block; }
212+
.game-container .game-message.restart-game .lower a.retry-button {
213+
display: none; }
214+
201215
.grid-container {
202216
position: absolute;
203217
z-index: 1; }
@@ -590,15 +604,28 @@ hr {
590604
height: 40px;
591605
line-height: 42px;
592606
margin-left: 9px; }
593-
.game-container .game-message a.keep-playing-button {
607+
.game-container .game-message a.keep-playing-button, .game-container .game-message a.confirm-button, .game-container .game-message a.cancel-button {
594608
display: none; }
595609
.game-container .game-message.game-won {
596610
background: rgba(237, 194, 46, 0.5);
597611
color: #f9f6f2; }
598612
.game-container .game-message.game-won a.keep-playing-button {
599613
display: inline-block; }
600-
.game-container .game-message.game-won, .game-container .game-message.game-over {
614+
.game-container .game-message.game-won, .game-container .game-message.game-over, .game-container .game-message.restart-game {
601615
display: block; }
616+
.game-container .game-message.restart-game {
617+
-webkit-animation: fade-in 400ms ease 100ms;
618+
-moz-animation: fade-in 400ms ease 100ms;
619+
animation: fade-in 400ms ease 100ms;
620+
-webkit-animation-fill-mode: both;
621+
-moz-animation-fill-mode: both;
622+
animation-fill-mode: both; }
623+
.game-container .game-message.restart-game .lower {
624+
margin-top: 85px; }
625+
.game-container .game-message.restart-game .lower a.confirm-button, .game-container .game-message.restart-game .lower a.cancel-button {
626+
display: inline-block; }
627+
.game-container .game-message.restart-game .lower a.retry-button {
628+
display: none; }
602629

603630
.grid-container {
604631
position: absolute;

0 commit comments

Comments
 (0)