-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
279 lines (202 loc) · 8.12 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Memory Game</title>
<script src="js/phaser.min.js"></script>
<style type="text/css">
canvas{
width:100%;
height:100%;
}
body{
padding: 0px;
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
var game;
var gameWidth = 960;
var gameHeight = 600;
var background;
var elephant;
var baseCardWidth = 128;
var baseCardHeight = 128;
var minYPadding = 100;
var wCards = 5;
var basePadding = ( gameWidth - (baseCardWidth*wCards) )/6;
var basePaddingH = 18;
var cardsGrid = {};
var animalList;
var hNCards = 5;
var vNCards = 4;
var gameCards;
var gameState;
var turnState;
var score;
var openedCards;
var nPairs = 0;
var maxPairs = 10;
var game_music;
var confirm_sound;
var error_sound;
var congratulations_sound;
var you_win_sound;
var scoreText = '';
window.onload = function() {
//init game object and and disable antialiasing with last params
//new Game(width, height, renderer, parent, state, transparent, antialias, physicsConfig)
game = new Phaser.Game(gameWidth, gameHeight, Phaser.AUTO, '', { preload: preload, create: create, update: update, render:render }, true, false);
function preload () {
game.load.image('background', 'img/background0.png');
game.load.spritesheet('elephant', 'img/elephant.png', 256, 256);
game.load.spritesheet('giraffe', 'img/giraffe.png', 256, 256);
game.load.spritesheet('hippo', 'img/hippo.png', 256, 256);
game.load.spritesheet('monkey', 'img/monkey.png', 256, 256);
game.load.spritesheet('panda', 'img/panda.png', 256, 256);
game.load.spritesheet('parrot', 'img/parrot.png', 256, 256);
game.load.spritesheet('penguin', 'img/penguin.png', 256, 256);
game.load.spritesheet('pig', 'img/pig.png', 256, 256);
game.load.spritesheet('rabbit', 'img/rabbit.png', 256, 256);
game.load.spritesheet('snake', 'img/snake.png', 256, 256);
game.load.spritesheet('blank-card', 'img/blank-card.png', 256, 256);
game.load.audio('game_music', 'music/Jungle.wav');
game.load.audio('confirm_sound', 'sound/Purchase.wav');
game.load.audio('error_sound', 'sound/Error.wav');
game.load.audio('congratulations_sound', 'sound/congratulations.mp3');
game.load.audio('you_win_sound', 'sound/you_win.mp3');
}
function create () {
gameState = "MENU";
turnState = "pick";
score = 0;
openedCards = [];
background = game.add.sprite(0, 0, 'background');
background.scale.setTo( game.world.width/background.width , game.world.height/background.height);
//elephant = game.add.sprite((baseCardWidth*0)+(basePadding*1), (baseCardHeight*0)+(basePaddingH*1), 'elephant');
//elephant.scale.setTo((1/2),(1/2));
animalList = ["elephant","giraffe","hippo","monkey","panda","parrot","penguin","pig","rabbit","snake"];
gameCards = game.add.group();
//add music
game_music = game.add.audio('game_music');
game_music.volume = 0.3;
game_music.loop = true;
game_music.play();
confirm_sound = game.add.audio('confirm_sound');
confirm_sound.volume = 0.7;
confirm_sound.loop = false;
error_sound = game.add.audio('error_sound');
error_sound.volume = 0.8;
error_sound.loop = false;
you_win_sound = game.add.audio('you_win_sound');
you_win_sound.volume = 0.8;
you_win_sound.loop = false;
congratulations_sound = game.add.audio('congratulations_sound');
congratulations_sound.volume = 0.8;
congratulations_sound.loop = false;
congratulations_sound.onStop.add(function(){you_win_sound.play();}, this);
createCards();
scoreText = game.add.text(16, 16, 'Score: '+score, { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
scoreText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
}
function update () {
scoreText.setText('Score: ' + score);
}
function render() {
}
function createCards(){
var animalListTemp = animalList.slice(0);
var animalListObject = {};
for(x=0; x< animalList.length; x++){
animalListObject[animalList[x]+""] = {
name: animalList[x],
positions: 0
};
}
var r;
for(x=0; x<hNCards; x++){
for(y=0; y<vNCards; y++){
r = Math.floor((Math.random() * animalListTemp.length));
cardsGrid["_"+x+""+y] = {
name:animalListTemp[r]
}
animalListObject[animalListTemp[r]].positions += 1;
if(animalListObject[animalListTemp[r]].positions >= 2){
animalListTemp.splice(r, 1);
}
}
}
console.log("Grid: ", cardsGrid);
for(x=0; x<hNCards; x++){
for(y=0; y<vNCards; y++){
var gameCard = gameCards.create( (baseCardWidth*x)+(basePadding*(x+1)), (baseCardHeight*y)+(basePaddingH*(y+1)), 'blank-card');
gameCard.scale.setTo((1/2),(1/2));
gameCard.inputEnabled = true;
gameCard.events.onInputDown.add(cardClicked, this);
gameCard.hiden = true;
gameCard.symbol = cardsGrid["_"+x+""+y].name;
}
}
}
function cardClicked(sprite, pointer){
if(turnState !== "pick" || sprite.hiden === false || openedCards.length >= 2){
error_sound.play();
return;
}
turnState = "fliping";
tween = game.add.tween(sprite);
tween.to({ y: [sprite.y -20, sprite.y +20, sprite.y] }, 400, "Linear");
tween.onComplete.add(function(){
sprite.loadTexture(sprite.symbol);
sprite.hiden = false;
openedCards.push(sprite);
checkPairs();
}, this);
tween.start();
//sprite.loadTexture(sprite.symbol);
}
function checkPairs(){
if(openedCards.length <=1){
turnState = "pick";
return;
}
if(openedCards[0].symbol === openedCards[1].symbol){
confirm_sound.play();
nPairs += 1;
score += 100;
openedCards = [];
if(nPairs >= maxPairs){
turnState = "end";
gameState = "finished";
congratulations_sound.play();
}else{
turnState = "pick";
}
return;
}else{
error_sound.play();
score -= 25;
tween1 = game.add.tween(openedCards[0]);
tween2 = game.add.tween(openedCards[1]);
tween1.to({ y: [openedCards[0].y -20, openedCards[0].y +20, openedCards[0].y] }, 400, "Linear");
tween1.onComplete.add(function(){
openedCards[0].loadTexture('blank-card');
openedCards[0].hiden = true;
tween2.start();
}, this);
tween2.to({ y: [openedCards[1].y -20, openedCards[1].y +20, openedCards[1].y] }, 400, "Linear");
tween2.onComplete.add(function(){
openedCards[1].loadTexture('blank-card');
openedCards[1].hiden = true;
turnState = "pick";
openedCards = [];
}, this);
tween1.start();
}
}
};
</script>
</body>
</html>