@@ -16,20 +16,22 @@ var Game = function(engine){
16
16
17
17
this . rulesGame = function ( ) {
18
18
19
- if ( this . snakeBody . pieces [ 0 ] . x < 0 ||
20
- this . snakeBody . pieces [ 0 ] . y < 0 ||
21
- this . snakeBody . pieces [ 0 ] . x > this . engine . ctx . canvas . clientWidth - this . snakeBody . config . size ||
22
- this . snakeBody . pieces [ 0 ] . y > this . engine . ctx . canvas . clientHeight - this . snakeBody . config . size )
23
- {
24
- clearInterval ( this . time ) ;
25
- var alert = this . engine . libraries . canvas . elements . TextObj ;
26
- alert . x = this . engine . ctx . canvas . clientWidth / 2 - 100 ;
27
- alert . y = this . engine . ctx . canvas . clientHeight / 2 ;
28
- alert . bgColor = "#fff" ;
29
- alert . font = "20px Arial" ;
30
- alert . text = "Fail - Enter to continue" ;
31
- this . canvas . render ( this . engine . ctx , alert ) ;
32
- this . snakeBody . reset ( ) ;
19
+ if ( this . snakeBody . pieces . length > 0 ) {
20
+ if ( this . snakeBody . pieces [ 0 ] . x < 0 ||
21
+ this . snakeBody . pieces [ 0 ] . y < 0 ||
22
+ this . snakeBody . pieces [ 0 ] . x > this . engine . ctx . canvas . clientWidth - this . snakeBody . config . size ||
23
+ this . snakeBody . pieces [ 0 ] . y > this . engine . ctx . canvas . clientHeight - this . snakeBody . config . size )
24
+ {
25
+ var alert = this . engine . libraries . canvas . elements . TextObj ;
26
+ alert . x = this . engine . ctx . canvas . clientWidth / 2 - 100 ;
27
+ alert . y = this . engine . ctx . canvas . clientHeight / 2 ;
28
+ alert . bgColor = "#fff" ;
29
+ alert . font = "20px Arial" ;
30
+ alert . text = "Fail - Enter to continue" ;
31
+ this . canvas . render ( this . engine . ctx , alert ) ;
32
+ this . snakeBody . reset ( ) ;
33
+ clearInterval ( this . time ) ;
34
+ }
33
35
}
34
36
35
37
this . shortPiece . renderPiece ( this ) ;
@@ -39,8 +41,7 @@ var Game = function(engine){
39
41
&& this . snakeBody . pieces [ 0 ] . y == this . shortPiece . piece . y ) {
40
42
this . shortPiece . piece = { } ;
41
43
this . shortPiece . renderPiece ( this ) ;
42
- // var hc = Object.create(this.engine.libraries.canvas);
43
- //this.snakeBody.addPiece(hc);
44
+ this . snakeBody . addPiece ( this . engine ) ;
44
45
}
45
46
}
46
47
@@ -53,8 +54,10 @@ var Game = function(engine){
53
54
if ( this . piece . x == undefined ) {
54
55
piecesX = controller . engine . ctx . canvas . clientWidth / controller . snakeBody . config . size ;
55
56
piecesY = controller . engine . ctx . canvas . clientHeight / controller . snakeBody . config . size ;
56
- randPieceX = Math . floor ( ( Math . random ( ) * piecesX ) + 1 ) * controller . snakeBody . config . size ;
57
- randPieceY = Math . floor ( ( Math . random ( ) * piecesY ) + 1 ) * controller . snakeBody . config . size ;
57
+ //randPieceX = Math.floor((Math.random() * piecesX) + 1) * controller.snakeBody.config.size;
58
+ //randPieceY = Math.floor((Math.random() * piecesY) + 1) * controller.snakeBody.config.size;
59
+ randPieceX = Math . floor ( ( Math . random ( ) * 20 ) + 10 ) * controller . snakeBody . config . size ;
60
+ randPieceY = Math . floor ( ( Math . random ( ) * 20 ) + 10 ) * controller . snakeBody . config . size ;
58
61
59
62
var newPiece = Object . create ( controller . engine . libraries . canvas . elements . RectObj ) ;
60
63
newPiece . width = controller . snakeBody . config . size ;
@@ -87,11 +90,17 @@ var Game = function(engine){
87
90
controller . engine . libraries . canvas . clearAll ( controller . engine . ctx ) ;
88
91
controller . snakeBody . init ( controller . engine , controller . snakeBody . moveLeft ( ) ) ;
89
92
90
- controller . time = setInterval ( function ( ) {
91
- controller . engine . libraries . canvas . fadeAll ( controller . engine , controller . snakeBody . config . speed ) ;
92
- controller . snakeBody . renderSnake ( controller . engine ) ;
93
- controller . rulesGame ( ) ;
94
- } , controller . snakeBody . config . speed ) ;
93
+ function start ( ) {
94
+ controller . time = setTimeout ( function ( ) {
95
+ controller . engine . libraries . canvas . fadeAll ( controller . engine , controller . snakeBody . config . speed ) ;
96
+ controller . snakeBody . renderSnake ( controller . engine , function ( ) {
97
+ start ( ) ;
98
+ controller . rulesGame ( ) ;
99
+ } ) ;
100
+ } , controller . snakeBody . config . speed ) ;
101
+ }
102
+
103
+ start ( ) ;
95
104
96
105
}
97
106
break ;
@@ -107,6 +116,9 @@ var Game = function(engine){
107
116
case 'ArrowRight' :
108
117
controller . snakeBody . moveRight ( ) ;
109
118
break ;
119
+ case 'Control' :
120
+ clearInterval ( controller . time ) ;
121
+ break ;
110
122
}
111
123
112
124
}
@@ -143,16 +155,11 @@ var Game = function(engine){
143
155
}
144
156
145
157
this . snakeBody = {
146
- config : { size : 10 , speed : 100 } ,
158
+ config : { size : 10 , speed : 200 } ,
147
159
pieces : [ ] ,
148
- newTurn : {
149
- x :0 ,
150
- y :0 ,
151
- moveX :0 ,
152
- moveY :0
153
- } ,
154
160
centerX : 0 ,
155
161
centerY : 0 ,
162
+ Rendering : false ,
156
163
reset : function ( ) {
157
164
this . pieces = [ ] ;
158
165
this . coordinatesChange = [ { x :0 , y :0 } ] ;
@@ -168,8 +175,9 @@ var Game = function(engine){
168
175
else
169
176
{
170
177
lastPiece = this . pieces . length - 1 ;
171
- snakePiece . x = this . pieces [ lastPiece ] . x -= this . pieces [ lastPiece ] . moveX ;
172
- snakePiece . y = this . pieces [ lastPiece ] . y -= this . pieces [ lastPiece ] . moveY ;
178
+
179
+ snakePiece . x = this . pieces [ lastPiece ] . x + this . pieces [ lastPiece ] . moveX ;
180
+ snakePiece . y = this . pieces [ lastPiece ] . y + this . pieces [ lastPiece ] . moveY ;
173
181
snakePiece . moveX = this . pieces [ lastPiece ] . moveX ;
174
182
snakePiece . moveY = this . pieces [ lastPiece ] . moveY ;
175
183
}
@@ -186,88 +194,60 @@ var Game = function(engine){
186
194
snakePiece . y = 0 ;
187
195
snakePiece . moveX = 0 ;
188
196
snakePiece . moveY = 0 ;
189
- snakePiece . turn = [ ] ;
190
197
191
198
snakePiece = this . pieceMove ( snakePiece ) ;
192
199
193
200
this . pieces . push ( snakePiece ) ;
194
201
} ,
195
- renderSnake : function ( engine ) {
196
-
197
- for ( var i = 0 ; i < this . pieces . length ; i ++ ) {
198
-
199
- this . pieces [ i ] . x -= this . pieces [ i ] . moveX ;
200
- this . pieces [ i ] . y -= this . pieces [ i ] . moveY ;
201
-
202
- engine . libraries . canvas . render ( engine . ctx , this . pieces [ i ] ) ;
203
-
204
- if ( this . pieces [ i ] . turn . length > 0 ) {
205
- if ( this . pieces [ i ] . x == this . pieces [ i ] . turn [ 0 ] . x &&
206
- this . pieces [ i ] . y == this . pieces [ i ] . turn [ 0 ] . y ) {
207
-
208
- filtercoord = this . pieces [ i ] . turn . slice ( 1 , this . pieces [ i ] . turn . length - 1 ) ;
209
- this . pieces [ i ] . turn = filtercoord ;
210
- this . pieces [ i ] . moveX = this . pieces [ i ] . turn [ 0 ] . moveX ;
211
- this . pieces [ i ] . moveY = this . pieces [ i ] . turn [ 0 ] . moveY ;
212
- }
202
+ renderSnake : function ( engine , callback ) {
203
+ this . Rendering = false ;
204
+ for ( var invert = this . pieces . length - 1 ; invert >= 0 ; invert -- ) {
205
+ if ( invert == 0 ) {
206
+ this . pieces [ invert ] . x -= this . currentMoveX ;
207
+ this . pieces [ invert ] . y -= this . currentMoveY ;
208
+ this . pieces [ invert ] . moveX = this . currentMoveX ;
209
+ this . pieces [ invert ] . moveY = this . currentMoveY ;
213
210
}
211
+ else {
212
+ this . pieces [ invert ] . x = this . pieces [ invert - 1 ] . x ;
213
+ this . pieces [ invert ] . y = this . pieces [ invert - 1 ] . y ;
214
+ this . pieces [ invert ] . moveX = this . pieces [ invert - 1 ] . moveX ;
215
+ this . pieces [ invert ] . moveY = this . pieces [ invert - 1 ] . moveY ;
216
+ }
217
+ engine . libraries . canvas . render ( engine . ctx , this . pieces [ invert ] ) ;
214
218
}
219
+
220
+ this . Rendering = false ;
221
+ callback ( ) ;
215
222
} ,
216
- filterTurn : function ( arrayTurns ) {
217
- return function ( obj ) {
218
- return obj . x != arrayTurns . x &&
219
- obj . y != arrayTurns . y &&
220
- obj . moveX != arrayTurns . moveX &&
221
- obj . moveY != arrayTurns . moveY ;
222
- }
223
- } ,
224
223
moveLeft : function ( ) {
225
- this . currentMoveX = this . config . size ;
226
- this . currentMoveY = 0 ;
227
- this . filterCoordenates ( ) ;
224
+ if ( ! this . Rendering ) {
225
+ this . currentMoveX = this . config . size ;
226
+ this . currentMoveY = 0 ;
227
+ }
228
228
} ,
229
229
moveRight : function ( ) {
230
- this . currentMoveX = - this . config . size ;
231
- this . currentMoveY = 0 ;
232
- this . filterCoordenates ( ) ;
230
+ if ( ! this . Rendering ) {
231
+ this . currentMoveX = - this . config . size ;
232
+ this . currentMoveY = 0 ;
233
+ }
233
234
} ,
234
235
moveUp : function ( ) {
235
- this . currentMoveX = 0 ;
236
- this . currentMoveY = this . config . size ;
237
- this . filterCoordenates ( ) ;
236
+ if ( ! this . Rendering ) {
237
+ this . currentMoveX = 0 ;
238
+ this . currentMoveY = this . config . size ;
239
+ }
238
240
} ,
239
241
moveDown : function ( ) {
240
- this . currentMoveX = 0 ;
241
- this . currentMoveY = - this . config . size ;
242
- this . filterCoordenates ( ) ;
242
+ if ( ! this . Rendering ) {
243
+ this . currentMoveX = 0 ;
244
+ this . currentMoveY = - this . config . size ;
245
+ }
243
246
} ,
244
- filterCoordenates : function ( ) {
245
- lastIdx = this . pieces . length - 1 ;
246
- if ( lastIdx > 1 ) {
247
- this . newTurn = {
248
- x :this . pieces [ lastIdx ] . x ,
249
- y :this . pieces [ lastIdx ] . y ,
250
- moveX :this . currentMoveX ,
251
- moveY :this . currentMoveY
252
- } ;
253
-
254
- this . pieces [ 0 ] . turn . push ( this . newTurn ) ;
255
- for ( var k = 1 ; k < this . pieces . length ; k ++ ) {
256
- this . pieces [ k ] . turn . push ( this . newTurn ) ;
257
- }
258
- }
259
-
260
- if ( lastIdx > - 1 ) {
261
- this . pieces [ lastIdx ] . moveX = this . currentMoveX ;
262
- this . pieces [ lastIdx ] . moveY = this . currentMoveY ;
263
- }
264
-
265
- } ,
266
247
init : function ( engine , move ) {
267
248
this . pieces = [ ] ;
268
249
this . centerX = engine . ctx . canvas . clientWidth / 2 ;
269
250
this . centerY = engine . ctx . canvas . clientHeight / 2 ;
270
- move ;
271
251
this . addPiece ( engine ) ;
272
252
}
273
253
0 commit comments