Skip to content

Commit 653bd0c

Browse files
author
Lucas CHARVOLIN
committed
patch tetris
1 parent 151d452 commit 653bd0c

File tree

3 files changed

+68
-63
lines changed

3 files changed

+68
-63
lines changed

tetris/css/main.css

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ body
2626
.piece
2727
{
2828
position:absolute;
29-
background-image:radial-gradient(circle at top right,rgb(230, 126, 34),rgb(231, 76, 60));
3029
width:25px;
3130
height:25px;
3231
}
@@ -125,3 +124,16 @@ body
125124
{
126125
background-image:radial-gradient(circle at top right,red,red);
127126
}
127+
#game_over
128+
{
129+
position: absolute;
130+
width:100%;
131+
height: 100%;
132+
color:white;
133+
background-color:rgba(25,25,25,0.5);
134+
}
135+
#game_over p
136+
{
137+
margin-top:250px;
138+
font-size: 2em;
139+
}

tetris/js/main.js

+55-47
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function draw_piece(y, x)
4040
function draw_alea_piece()
4141
{
4242
var alea = get_alea(0,7);
43+
if (check_end_of_game() == 1)
44+
{
45+
game = 0;
46+
return;
47+
}
4348
if (alea == 1)
4449
{
4550
draw_piece(0,0 + 4);
@@ -147,30 +152,6 @@ function get_the_color(nb)
147152
return("rgb(149, 165, 166),rgb(127, 140, 141)");
148153
}
149154

150-
// function actu_map()
151-
// {
152-
// var i = 21;
153-
// var i2 = 0;
154-
// var color;
155-
// var map_elem = document.getElementById('inner_map');
156-
// map_elem.innerHTML = "";
157-
// while (i >= 0)
158-
// {
159-
// i2 = 0;
160-
// while (i2 <= 9)
161-
// {
162-
// color = get_the_color(map[i][i2]);
163-
// if (map[i][i2] == (active_piece - 1))
164-
// map_elem.innerHTML += "<div class=\"piece " + (active_piece - 1) + "\" style=\"background-image:radial-gradient(circle at top right," + color + ");top:" + (i * 50) + "px;left:" + (i2 * 50) + "px;\"></div>";
165-
// else if (map[i][i2] != 0)
166-
// map_elem.innerHTML += "<div class=\"piece " + map[i][i2] + "\" style=\"background-image:radial-gradient(circle at top right," + color + ");top:" + (i * 50) + "px;left:" + (i2 * 50) + "px;\"></div>";
167-
// i2++;
168-
// }
169-
// i--;
170-
// }
171-
172-
// }
173-
174155
function actu_map()
175156
{
176157
var i = 21;
@@ -319,7 +300,6 @@ function check_end_of_turn()
319300
function move_fixed_blocks(nb_deleted_lines, frst_line_deleted)
320301
{
321302
var fixed_blocks = document.getElementById('fixed_block');
322-
var active_blocks = document.getElementById('inner_map');
323303
var i = 0;
324304
var tmp = 0;
325305
var i2 = 0;
@@ -336,12 +316,17 @@ function move_fixed_blocks(nb_deleted_lines, frst_line_deleted)
336316
i++;
337317
}
338318
i = 0;
319+
var active_blocks = document.getElementById('inner_map');
320+
console.log(active_blocks);
339321
while (i < active_blocks.childNodes.length)
340322
{
341-
tmp = map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)];
342-
map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)] = 0;
343-
active_blocks.childNodes[i].style.top = parseInt(active_blocks.childNodes[i].style.top) + (nb_deleted_lines * 25) + "px";
344-
map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)] = tmp;
323+
if (active_blocks.childNodes[i] != undefined)
324+
{
325+
tmp = map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)];
326+
map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)] = 0;
327+
active_blocks.childNodes[i].style.top = parseInt(active_blocks.childNodes[i].style.top) + (nb_deleted_lines * 25) + "px";
328+
map[(parseInt(active_blocks.childNodes[i].style.top) / 25)][(parseInt(active_blocks.childNodes[i].style.left) / 25)] = tmp;
329+
}
345330
i++;
346331
}
347332
}
@@ -429,6 +414,7 @@ function restart()
429414
}
430415
score = 0;
431416
active_piece = 1;
417+
game = 1;
432418
draw_alea_piece();
433419
actu_map();
434420
}
@@ -439,38 +425,59 @@ function actu_score()
439425
score_elem.innerHTML = score;
440426
}
441427

428+
function check_end_of_game()
429+
{
430+
var i = 0;
431+
var fixed_block = document.getElementById('fixed_block');
432+
while (i < 10)
433+
{
434+
if (map[0][i] != 0)
435+
{
436+
fixed_block.innerHTML += "<div id=\"game_over\"><p>Game over</p></div>";
437+
return(1);
438+
}
439+
i++;
440+
}
441+
return(0);
442+
}
443+
442444
function run()
443445
{
444-
if (check_end_of_turn() == 0)
445-
go_down();
446-
if (check_end_of_turn() == 1)
446+
if (game == 1)
447447
{
448-
check_full_line();
449-
draw_alea_piece();
450-
turn++;
448+
if (check_end_of_turn() == 0)
449+
go_down();
450+
if (check_end_of_turn() == 1)
451+
{
452+
check_full_line();
453+
draw_alea_piece();
454+
}
455+
actu_map();
456+
actu_score();
451457
}
452-
actu_map();
453-
actu_score();
454458
}
455459

456460
window.addEventListener("keydown", function(e)
457461
{
458-
if(e.keyCode ==37)
459-
move_left();
460-
else if(e.keyCode == 38)
461-
rotate();
462-
else if(e.keyCode == 39)
463-
move_right();
464-
else if(e.keyCode == 40 && check_end_of_turn() == 0)
465-
move_bot();
466-
actu_map();
462+
if (game == 1)
463+
{
464+
if(e.keyCode ==37)
465+
move_left();
466+
else if(e.keyCode == 38)
467+
rotate();
468+
else if(e.keyCode == 39)
469+
move_right();
470+
else if(e.keyCode == 40 && check_end_of_turn() == 0)
471+
move_bot();
472+
actu_map();
473+
}
467474
}, true);
468475

469476
var active_piece = 1;
470477
var piece_type;
471478
var score = 0;
472479
var map = new Array(22);
473-
var turn = 1;
480+
var game = 1;
474481
for (var i = 0; i < 22; i++)
475482
{
476483
map[i] = new Array(10);
@@ -480,4 +487,5 @@ for (var i = 0; i < 22; i++)
480487
}
481488
}
482489
draw_alea_piece();
490+
actu_map();
483491
setInterval(function(){ run(); },500);

tetris/js/tools.js

-15
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ function random_color()
2020
return("color8");
2121
}
2222

23-
function draw_title(nb_piece)
24-
{
25-
var nb_piece_on_line = nb_piece / 5;
26-
var pieces = document.getElementsByClassName('relative_piece');
27-
var i = 0;
28-
console.log(nb_piece);
29-
while (i < pieces.length)
30-
{
31-
if ((i >= 5 && i <= 7) || i == (6 + nb_piece_on_line))
32-
pieces[i].className = "relative_piece low_opacity color10";
33-
i++;
34-
}
35-
}
36-
3723
function draw_background_title()
3824
{
3925
var title_contner = document.getElementById('title_contner');
@@ -45,7 +31,6 @@ function draw_background_title()
4531
title_contner.innerHTML += "<div class=\"relative_piece low_opacity " + rand_color + "\">";
4632
i++;
4733
}
48-
draw_title(i);
4934
}
5035
draw_background_title()
5136
window.onresize = function(){

0 commit comments

Comments
 (0)