File: tetris.html

package info (click to toggle)
jsxgraph 1.3.5+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 26,044 kB
  • sloc: xml: 5,869; java: 1,072; python: 747; php: 192; makefile: 146; sh: 47
file content (405 lines) | stat: -rw-r--r-- 14,833 bytes parent folder | download | duplicates (2)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<html>
<head>
   <title>JSXGraph example</title>
   <style type="text/css">
       .jxgbox {
        overflow:hidden;
        background-color:#ffffff;
        border-style:solid;
        border-width:2px;
        border-color:#000000;
       }
       body {
        font-family:Arial;
       }
    </style> 
   
   <script type="text/javascript" src="/javascript/prototype/prototype.js"></script>
   <script type="text/javascript" src="../src/loadjsxgraph.js"></script>
</head>
<body>
  <h1><font color='#DC143C'>T</font><font color='#FF8C00'>e</font><font color='#0000CD'>t</font><font color='#FFD700'>r</font><font color='#4B0082'>i</font><font color='#32CD32'>s</font></h1>
  <div id="jxgbox" class="jxgbox" style="width:250px; height:500px;position:absolute;left:200px;top:75px"></div>
  <div style="width:125px; height:25px;position:absolute;left:500px;top:75px"><b>Next</b></div>  
  <div id="jxgbox2" class="jxgbox" style="width:125px; height:125px;position:absolute;left:500px;top:100px"></div>
  <div style="width:125px; height:25px;position:absolute;left:500px;top:300px"><b>Level</b></div>
  <div style="width:125px; height:25px;position:absolute;left:500px;top:325px"><b id='level'>1</b></div>   
  <div style="width:125px; height:25px;position:absolute;left:500px;top:400px"><b>Score</b></div>
  <div style="width:125px; height:25px;position:absolute;left:500px;top:425px"><b id='score'>0</b></div>  
  <br clear=all>
  <div id="debug" style="display:block;position:absolute;left:500px;width:200px"></div>
  <script type="text/javascript">
    /* <![CDATA[ */
    var board = JXG.JSXGraph.initBoard('jxgbox', {originX: 0, originY: 500, unitX: 25, unitY: 25, showCopyright:false, showNavigation:false}); // Spielfeld 10x20
    var board2 = JXG.JSXGraph.initBoard('jxgbox2', {originX: 0, originY: 125, unitX: 25, unitY: 25, showCopyright:false, showNavigation:false});
    var usedPolys = new Array(10);
    var i,j;
    for(i=0;i<10;i++) {
        usedPolys[i] = new Array(20);
        for(j=0;j<20;j++) {
            usedPolys[i][j] = 0;
        }
    }
    var colors = ['none','#DC143C','#FF8C00','#87CEEB','#FFD700','#4B0082','#0000CD','#32CD32'];
    
    function drawPointsAndPolys(board,width,height,vorschau) {
        var i,j,pArr = {};
        board.suspendUpdate();
        for(i=0;i<=width;i++) {
            for(j=0; j<=height;j++) {
                pArr[i+''+j] = board.createElement('point',[i,j],{withLabel:false,face:'plus',draft:true});
            }
        }
        for(i=0;i<width;i++) {
            for(j=0; j<height;j++) {
                var idpoly = 'poly_'+i+'_'+j;
                if(vorschau) {
                    idpoly = 'poly_'+i+'_'+j+'_v';
                }
                var poly = board.createElement('polygon',
                                       [pArr[i+''+j], pArr[(i+1)+''+j], pArr[(i+1)+''+(j+1)], pArr[i+''+(j+1)]],
                                       {fillColor:'white',visible:false,id:idpoly});
                for(k=0;k<4;k++) {
                    poly.borders[k].setProperty('strokeColor:#666666');
                } 
            }
        }
        board.unsuspendUpdate();
        return pArr;
    }    
    
    function shape(style) {
        if(style == 1) { // I
            return {face:[[1,1,1,1],[0,0,0,0],[0,0,0,0],[0,0,0,0]],size:4};
        }
        else if(style == 2) { // T
            return {face:[[2,0,0],[2,2,0],[2,0,0]],size:3};
        }
        else if(style == 3) { // O
            return {face:[[3,3],[3,3]],size:2};
        }
        else if(style == 4) { // L
            return {face:[[4,4,4],[4,0,0],[0,0,0]],size:3};
        }      
        else if(style == 5) { // J
            return {face:[[5,0,0],[5,5,5],[0,0,0]],size:3};
        }
        else if(style == 6) { // S
            return {face:[[6,0,0],[6,6,0],[0,6,0]],size:3};
        }  
        else if(style == 7) { // Z
            return {face:[[0,7,0],[7,7,0],[7,0,0]],size:3};
        }          
    }    
    
    function compareWithAbsoluteShape(s,posX,posY) {
        for(i=0;i<s.size;i++) {
            for(j=0;j<s.size;j++) {
                if(s.face[i][j] != 0) {
                    if(i+posX < 0 || i+posX > 9) {
                        return false;
                    }
                    if(j+posY < 0 || j+posY > 19) {
                        return false;
                    }                    
                    if(usedPolys[i+posX][j+posY] == 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    function drawShape(s,posX,posY) {
        board.suspendUpdate();
        var i,j;
        for(i=0;i<s.size;i++) {
            for(j=0;j<s.size;j++) {
                if(s.face[i][j] != 0) {
                    showCube(i+posX,j+posY,colors[s.face[i][j]]);
                    usedPolys[i+posX][j+posY] = 1;
                }
            }
        }
        board.unsuspendUpdate();             
        return {shape:s,x:posX,y:posY};
    }
    
    function removeShape(s,posX,posY) {
        board.suspendUpdate();
        var i,j, shape = new Array();
        for(i=0;i<s.size;i++) {
            for(j=0;j<s.size;j++) {
                if(s.face[i][j] != 0) {
                    hideCube(i+posX,j+posY);
                    usedPolys[i+posX][j+posY] = 0;
                }
            }
        }
        board.unsuspendUpdate();        
        return {shape:s,x:posX,y:posY};
    } 
    
    function drawVorschauShape(s,posX,posY) {
        board2.suspendUpdate();
        var i,j;
        for(i=0;i<s.size;i++) {
            for(j=0;j<s.size;j++) {
                if(s.face[i][j] != 0) {
                    showCubeV(i+posX,j+posY,colors[s.face[i][j]]);
                }
            }
        }
        board2.unsuspendUpdate();
    } 

    function clearVorschau() {
        for(i=0;i<5;i++) {
            for(j=0;j<5;j++) {
                hideCubeV(i,j);
            }
        }
    }

    function showCube(posX,posY,color) {
        board.objects['poly_'+posX+'_'+posY].setProperty('visible:true');
        board.objects['poly_'+posX+'_'+posY].setProperty('fillColor:'+color);
    }
    
    function hideCube(posX,posY) {
        board.objects['poly_'+posX+'_'+posY].setProperty('visible:false');
    }   
    
    function showCubeV(posX,posY,color) {
        board2.objects['poly_'+posX+'_'+posY+'_v'].setProperty('visible:true');
        board2.objects['poly_'+posX+'_'+posY+'_v'].setProperty('fillColor:'+color);
    }
    
    function hideCubeV(posX,posY) {
        board2.objects['poly_'+posX+'_'+posY+'_v'].setProperty('visible:false');
    } 
    
    function rotate(shape) { // gegen den Uhrzeigersinn drehen
        if(shape.size == 2) {
            shape.face = [[shape.face[0][1],shape.face[1][1]],
                          [shape.face[0][0],shape.face[1][1]]];
        }
        else if(shape.size == 3) {
            shape.face = [[shape.face[0][2],shape.face[1][2],shape.face[2][2]],
                          [shape.face[0][1],shape.face[1][1],shape.face[2][1]],
                          [shape.face[0][0],shape.face[1][0],shape.face[2][0]]];
        }
        else if(shape.size == 4) {
            shape.face = [[shape.face[0][3],shape.face[1][3],shape.face[2][3],shape.face[3][3]],
                          [shape.face[0][2],shape.face[1][2],shape.face[2][2],shape.face[3][2]],
                          [shape.face[0][1],shape.face[1][1],shape.face[2][1],shape.face[3][1]],
                          [shape.face[0][0],shape.face[1][0],shape.face[2][0],shape.face[3][0]]];
        }          
        return shape;
    }
    
    function getRandom() {
        return parseInt(1+Math.random()*7);
    }
    
    function testLine(i) {
        var j;
        for(j=0; j<10; j++) {
            if(usedPolys[j][i] == 0) {
                return false;
            }
        }
        return true;
    }
    
    function testLines() {
        var i, arr = new Array();
        for(i=0; i<20; i++) {
            if(testLine(i)) {
                arr.push(i);
            }
        }
        return arr;
    }
    
    /* doesn't work properly */
    function removeLines() {
        var arr = testLines(),i,j,k, line;
        if(arr.length > 0) {
            if(arr.length == 1) {
                score += (level+1)*40;
            }
            else if(arr.length == 2) {
                score += (level+1)*100;
            }
            else if(arr.length == 3) {
                score += (level+1)*300;
            }
            else if(arr.length == 4) {
                score += (level+1)*1200;
            }     
            scoreNode.innerHTML = score;             
            for(i=0;i<arr.length;i++) {
                line = arr[i];
                for(j=line+1-i;j<20;j++) {
                    for(k=0;k<10;k++) {
                        if(usedPolys[k][j] == 1) {
                            showCube(k,j-1,board.objects['poly_'+k+'_'+j].visProp['fillColor']);
                            usedPolys[k][j-1] = 1;
                            hideCube(k,j);
                            usedPolys[k][j] = 0;
                        }
                        else {
                            hideCube(k,j-1);
                            usedPolys[k][j-1] = 0;
                        }
                    }
                }
            }
        }
    }
    
    function keyDown(Evt) {
        var code;
        if (!Evt) {
            Evt = window.event;
        }
        if (Evt.which) {
            code = Evt.which;
        } else if (Evt.keyCode) {
            code = Evt.keyCode;
        }
        // 37: left,  38: up,  39: right,  40: down
        if (code==38) { 
            removeShape(curShape.shape,curShape.x,curShape.y);
            // echte Kopie anlegen...
            var s = {};
            s.size = curShape.shape.size;
            s.face = (new Array()).concat(curShape.shape.face);
            if(compareWithAbsoluteShape(rotate(s),curShape.x,curShape.y)) {
                curShape = drawShape(rotate(curShape.shape),curShape.x,curShape.y);
            }
            else {
                curShape = drawShape(curShape.shape,curShape.x,curShape.y);
            }
            return false;
        }
        else if(code==39) {
            removeShape(curShape.shape,curShape.x,curShape.y);        
            if(compareWithAbsoluteShape(curShape.shape,curShape.x+1,curShape.y)) {        
                curShape = drawShape(curShape.shape,curShape.x+1,curShape.y);
            }
            else {
                curShape = drawShape(curShape.shape,curShape.x,curShape.y);
            }
            return false;
        }
        else if(code==37) {
            removeShape(curShape.shape,curShape.x,curShape.y);
            if(compareWithAbsoluteShape(curShape.shape,curShape.x-1,curShape.y)) {         
                curShape = drawShape(curShape.shape,curShape.x-1,curShape.y);
            }
            else {
                curShape = drawShape(curShape.shape,curShape.x,curShape.y);
            }
            return false;
        }    
        else if(code==40) {
            removeShape(curShape.shape,curShape.x,curShape.y);
            if(compareWithAbsoluteShape(curShape.shape,curShape.x,curShape.y-1)) {           
                curShape = drawShape(curShape.shape,curShape.x,curShape.y-1);
                score += (level + 1);
                scoreNode.innerHTML = score;
            }
            else {
                curShape = drawShape(curShape.shape,curShape.x,curShape.y);
            }
            return false;
        } 
        else if(code==32) { // Space
            if(curShape.y > 0) {
                removeShape(curShape.shape,curShape.x,curShape.y);
                var i=curShape.y-1; 
                var min = curShape.y;
                while(i>=0) {
                    if(compareWithAbsoluteShape(curShape.shape,curShape.x,i)) {           
                        min = i
                    }
                    else {
                        i=0;
                    }
                    i--;
                }
                score += (level + 1)*(curShape.y-min);
                scoreNode.innerHTML = score;                   
                curShape = drawShape(curShape.shape,curShape.x,min);
            }
        }
        return true;
    }
    
    function gameOver() {
        for(i=0;i<10; i++) {
            if(usedPolys[i][18] == 1) {
                return true;
            }
        }
        return false;
    }

    function goDownByTime() {
        removeShape(curShape.shape,curShape.x,curShape.y);
        if(compareWithAbsoluteShape(curShape.shape,curShape.x,curShape.y-1)) {           
            curShape = drawShape(curShape.shape,curShape.x,curShape.y-1);
            setTimeout(goDownByTime,timeOutTimes[level]);
        }
        else {
            curShape = drawShape(curShape.shape,curShape.x,curShape.y);
            document.getElementById('score').innerHTML
            if(!gameOver()) {
                removeLines();
                if(compareWithAbsoluteShape(shape(next),4,16)) {
                    curShape = drawShape(shape(next),4,16);
                    next = getRandom();
                    clearVorschau();
                    drawVorschauShape(shape(next),1,1);
                    setTimeout(goDownByTime,timeOutTimes[level]);
                }
                else {
                    curShape = drawShape(shape(next),4,16);
                    alert("Game Over!");
                }
            }
            else {
                alert("Game Over!");
            }
        }
         
    }
    
    function levelUp() {
        if(level < 15) {
            level += 1;
            document.getElementById('level').innerHTML = level;
            setTimeout(levelUp,1000*30);
        }
    }
    var pArray = drawPointsAndPolys(board,10,20,false);
    drawPointsAndPolys(board2,5,5,true);
    
    var score = 0;
    var scoreNode = document.getElementById('score');
    var level = 1;
    var timeOutTimes = [2000,1000,900,800,700,600,500,400,350,300,250,200,150,100,50,25]
    var curShape = drawShape(shape(getRandom()),4,16); 
    var next = getRandom();
    drawVorschauShape(shape(next),1,1);
    
    document.onkeydown = keyDown;    
    setTimeout(goDownByTime,timeOutTimes[level]);
    setTimeout(levelUp,1000*30);
  /* ]]> */
  </script>
</body>
</html>