-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
291 lines (268 loc) · 14.1 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
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE html>
<html>
<head>
<title>Пятнашки</title>
<script type="text/javascript" src="cocos2d-js-v3.8.js" charset="UTF-8"></script>
<meta charset="UTF-8">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-112766081-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-112766081-1');
</script>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script type="text/javascript">
//Functions
function drawGameField(gField, numbers){
for(var j=0;j<4;j++){
for (var i=0;i<4;i++){
var k = gField[i][j];
if(k != 0){
var actionTo = cc.moveTo(0.1, cc.p(getX(j),getY(i)));
numbers[k].stopAllActions();
numbers[k].runAction(actionTo);
}
}
}
}
function isWin(gField){
var winField = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,0]];
for(var j=0;j<4;j++){
for (var i=0;i<4;i++){
if (gField[i][j] != winField[i][j]){
return false;
}
}
}
return true;
}
function getMoveDirection(gField, num){
for(var j=0;j<4;j++){
for (var i=0;i<4;i++){
if (gField[i][j] == num){
if (i>0 && gField[i-1][j] == 0){
return 3;
}
if (i<3 && gField[i+1][j] == 0){
return 4;
}
if (j>0 && gField[i][j-1] == 0){
return 1;
}
if (j<3 && gField[i][j+1] == 0){
return 2;
}
}
}
}
return -1;
}
function shiftElement(gField, currentFreePos, direction){
switch(direction){
case 1: //left
if (currentFreePos.y == 3){
return;
}else{
gField[currentFreePos.x][currentFreePos.y] = gField[currentFreePos.x][currentFreePos.y + 1];
currentFreePos.y++;
}
break;
case 2: //right
if (currentFreePos.y == 0){
return;
}else{
gField[currentFreePos.x][currentFreePos.y] = gField[currentFreePos.x][currentFreePos.y - 1];
currentFreePos.y--;
}
break;
case 3: //up
if (currentFreePos.x == 3){
return;
}else{
gField[currentFreePos.x][currentFreePos.y] = gField[currentFreePos.x + 1][currentFreePos.y];
currentFreePos.x++;
}
break;
case 4: //down
if (currentFreePos.x == 0){
return;
}else{
gField[currentFreePos.x][currentFreePos.y] = gField[currentFreePos.x - 1][currentFreePos.y];
currentFreePos.x--;
}
break;
default:
return;
}
gField[currentFreePos.x][currentFreePos.y] = 0;
}
function shuffleNumbers(gField, currentFreePos, iterations){
for (var i=0;i<iterations;i++){ //May be not cool, but this is guarantee shiftElement puzzle will be assembled.
var direction = Math.floor(Math.random() *4) + 1; //random direction
shiftElement(gField, currentFreePos, direction);
}
}
function getX(x){
return (212.5+125*x);
}
function getY(y){
return (487.5-125*y);
}
//Main
window.onload = function(){
cc.game.onStart = function(){
//load resources
cc.LoaderScene.preload(["game_field.png","num1.png","num2.png","num3.png","num4.png",
"num5.png","num6.png","num7.png","num8.png",
"num9.png","num10.png","num11.png","num12.png",
"num13.png","num14.png","num15.png",
"sound_on.png", "sound_off.png", "restart.png"], function () {
var MyScene = cc.Scene.extend({
onEnter:function () {
this._super();
var origin = cc.director.getVisibleOrigin();
var size = cc.director.getWinSize();
cc.audioEngine.setEffectsVolume(0.5);
var seStatus = true;
//Add game field sprite
var sprite = cc.Sprite.create("game_field.png");
sprite.setPosition(size.width/2, size.height/2);
this.addChild(sprite, 0);
//Add number sprites
var numbers = new Array(15);
for (var i=1; i<=15; i++){
numbers[i]=cc.Sprite.create("num"+i+".png");
numbers[i].setName(i);
this.addChild(numbers[i], 1);
}
//Add header label
var label = cc.LabelTTF.create("Пятнашки", "Arial", 40);
label.setPosition(size.width/2, size.height - 25);
label.setColor("000000ff");
this.addChild(label, 2);
//Add status labels
var statusLabel = new cc.LabelTTF("Попробуй собрать эту головоломку!", "", 20);
statusLabel.setPosition(origin.x + size.width/2, 20);
statusLabel.setColor("000000ff");
this.addChild(statusLabel, 2);
var countMovesLabel = new cc.LabelTTF("Ходов: 0", "", 20);
countMovesLabel.setPosition(size.width - 75, 75);
countMovesLabel.setColor("000000ff");
this.addChild(countMovesLabel, 2);
//Add sound effects on/off control sprites
var seOn = cc.Sprite.create("sound_on.png");
seOn.setPosition(size.width - 40, 35);
seOn.setScale(0.75);
var seOff = cc.Sprite.create("sound_off.png");
seOff.setPosition(size.width + 40, 35);
seOff.setScale(0.75);
this.addChild(seOn, 2);
this.addChild(seOff, 2);
//Add restart control sprite
var restartBtn = cc.Sprite.create("restart.png");
restartBtn.setPosition(size.width - 100, 35);
restartBtn.setScale(0.75);
this.addChild(restartBtn, 2);
//Init game logic
var gField = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,0]];
var currentFreePos = {x:3,y:3};
shuffleNumbers(gField, currentFreePos, 1000);
drawGameField(gField, numbers);
var countMoves = 0;
// Make num1 touchable
var listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event){
var pos = touch.getLocation(), target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(pos);
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
target.setScale(1.1);
var num = parseInt(target.getName());
var direction = getMoveDirection(gField, num);
if (direction != -1){
shiftElement(gField, currentFreePos, direction);
drawGameField(gField, numbers);
countMoves++;
countMovesLabel.setString("Ходов: "+countMoves);
cc.audioEngine.playEffect("tm2_switch000.wav");
}
return true;
}
return false;
},
onTouchEnded: function(touch, event){
var target = event.getCurrentTarget();
target.setScale(1);
if (isWin(gField) == true){
statusLabel.setString("Вы выиграли!");
}
}
});
cc.eventManager.addListener(listener1, numbers[1]);
for (var i=2; i<16;i++){
cc.eventManager.addListener(listener1.clone(), numbers[i]);
}
var listener2 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event){
var pos = touch.getLocation(), target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(pos);
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
seStatus = !seStatus;
target.setScale(1);
return true;
}
return false;
},
onTouchEnded: function(touch, event){
var target = event.getCurrentTarget();
cc.audioEngine.setEffectsVolume(seStatus == true ? 0.5 : 0);
seOn.setPosition(seStatus == true ? size.width - 40 : size.width + 35, 35);
seOff.setPosition(seStatus == false ? size.width - 40 : size.width + 35, 35);
target.setScale(0.75);
}
});
cc.eventManager.addListener(listener2, seOn);
cc.eventManager.addListener(listener2.clone(), seOff);
var listener3 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event){
var pos = touch.getLocation(), target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(pos);
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
target.setScale(1);
return true;
}
return false;
},
onTouchEnded: function(touch, event){
var target = event.getCurrentTarget();
shuffleNumbers(gField, currentFreePos, 1000);
drawGameField(gField, numbers);
countMoves = 0;
countMovesLabel.setString("Ходов: "+countMoves);
target.setScale(0.75);
statusLabel.setString("Попробуй собрать эту головоломку!");
}
});
cc.eventManager.addListener(listener3, restartBtn);
}
});
cc.director.runScene(new MyScene());
}, this);
};
cc.game.run("gameCanvas");
};
</script>
</body>
</html>