Skip to content

Commit

Permalink
Made code more readable.
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonschlosshauer committed Aug 24, 2018
1 parent 1e6bbdf commit 7267c82
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 205 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>Tetris</title>
<link rel="stylesheet" href="master.css">
<script type="text/javascript" src="js/block.js"></script>
<script type="text/javascript" src="js/game.js"></script>
Expand Down
151 changes: 56 additions & 95 deletions js/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ function Block(size,y,x,type,save,field)
this.blocked = false;
}

function move(temp_block,movementy, movementx)
Block.prototype.move = function (y,x)
{
//We can only move if there is no block in our way
var coll = collided(temp_block,movementy,movementx);
var coll = this.collided(y,x);
if(coll == 0)
{
//The actual moving part
temp_block.positiony += movementy;
temp_block.positionx += movementx;
this.positiony += y;
this.positionx += x;
}

return coll;
}

function rotate_block()
};
Block.prototype.rotate_block = function ()
{
//Make next blocks
//Check if anything is out of bounds
Expand All @@ -34,7 +33,7 @@ function rotate_block()
//else break

//Choose next block
var next_type = current_block.type;
var next_type = this.type;
next_type *= 10;

if(Math.floor(next_type / 10000) > 0)
Expand All @@ -43,87 +42,92 @@ function rotate_block()
}

//Create next block
temp_block = generate_block(next_type,current_block.positiony,current_block.positionx);
temp_block = generate_block(next_type,this.positiony,this.positionx);

var direction = 1;
var max_mov = 2;
if(temp_block.positionx > playfield.width / 2) direction = -1;

//If collided, don't change block
if(collided(temp_block,0,0))
if(temp_block.collided(0,0))
{
return;
}

current_block = temp_block;
}
this.size = temp_block.size;
this.type = temp_block.type;
this.positiony = temp_block.positiony;
this.positionx = temp_block.positionx;
this.saveable = temp_block.saveable;
this.field = temp_block.field;
this.blocked = temp_block.blocked;
};

function drop(temp_block)
Block.prototype.drop = function ()
{
while(!at_bottom(temp_block) && !move(temp_block,1,0)){}
}
while(!this.at_bottom() && !this.move(1,0)){}
};

function at_bottom(temp_block)
Block.prototype.at_bottom = function ()
{
//Is at line 19 ?
if(temp_block.positiony + temp_block.size >= playfield.height - 1)
//Is at line the end of the field (19) ?
if(this.positiony + this.size >= playfield.height - 1)
{

//Find which line in field of temp_block is the last
var targety = 0;
while(targety + temp_block.positiony < playfield.height - 1)
while(targety + this.positiony < playfield.height - 1)
{
targety++;
}

//Savety check : No out of bounds on temp_block.field
if(targety >= temp_block.size)
if(targety >= this.size)
{
return false;
}
//Check if it's not an empty line
for(var x = 0; x < temp_block.size; x++)
for(var x = 0; x < this.size; x++)
{
if(temp_block.field[targety][x])
if(this.field[targety][x])
{
return true;
}
}
}

return false;
}
};

function collided(temp_block,movementy, movementx)
{
for(var tempy = 0 ; tempy < temp_block.size; tempy++)
Block.prototype.collided = function (y,x) {
for(var tempy = 0 ; tempy < this.size; tempy++)
{
for(var tempx = 0; tempx < temp_block.size; tempx++)
for(var tempx = 0; tempx < this.size; tempx++)
{
if(temp_block.field[tempy][tempx] == 1)
if(this.field[tempy][tempx] == 1)
{
//Make sure we don't move out of bounds
while(temp_block.positionx+tempx+movementx >= playfield.width)
while(this.positionx+tempx+x >= playfield.width)
{
temp_block.positionx--;
this.positionx--;
}

while(temp_block.positionx+tempx+movementx < 0)
while(this.positionx+tempx+x < 0)
{
temp_block.positionx++;
this.positionx++;
}

while(temp_block.positiony+tempy+movementy < 0)
while(this.positiony+tempy+y < 0)
{
temp_block.positiony++;
this.positiony++;
}

//Blocked
if(temp_block.positiony + tempy + movementy > 39)
if(this.positiony + tempy + y > playfield.height - 1)
{
return 1;
}
if(playfield.field[temp_block.positiony+tempy+movementy][temp_block.positionx+tempx+movementx])
if(playfield.field[this.positiony+tempy+y][this.positionx+tempx+x])
{
return 2;
}
Expand All @@ -132,65 +136,24 @@ function collided(temp_block,movementy, movementx)
}

return 0;
}

function is_rotateable_xd(temp_block)
{
//Make sure we don't hit any walls
//Check if we're blocked
//If we are, look if move by 2 on x fixes
//else return false

correct_possible_wall_collision(temp_block,0,0);

var available_x_movement = 2;
//Move right by default
var movement_direction = 1;

if(temp_block.postionx > playfield.width / 2)
{
movement_direction = -1;
}

//Try resolving block
while(available_x_movement > 0 && collided_block(temp_block,0,0))
{
if(available_x_movement > 0)
{
temp_block.positionx += movement_direction;
available_x_movement--;
}
}
};

if(available_x_movement > 0)
{
return true;
}
return false;
}

function toggle_backup()
Block.prototype.toggle_backup = function ()
{
//Doesn't save after loading.

//If nothing is saved, put curr into saved
//generate new block and put into temp

//Load curr into temp
//Load saved into curr
//Load temp into saved

//Save current block id
//Generate new block with id that is != current
//Set backup flag to false

if(!current_block.saveable)
if(!this.saveable)
{
return;
}

//Save current_block into temp
var temp = current_block;
var temp = this;

//Load curr into temp
//Load saved into curr
//Load temp into saved

if(saved_block == 0)
{
Expand All @@ -202,14 +165,12 @@ function toggle_backup()
saved_block = new Block(field_size,initialy,initialx,block_type,false,get_field(block_type, field_size));
}

current_block = saved_block;
current_block.positionx = initialx;
current_block.positiony = initialy;
current_block.saveable = false;
this.size = saved_block.size;
this.type = saved_block.type;
this.field = saved_block.field;
this.blocked = saved_block.blocked;
this.positionx = initialx;
this.positiony = initialy;
this.saveable = false;
saved_block = temp;

update_hold_block_window_frame();
update_hold_block_window();
update_next_block_window_frame();
update_next_block_window();
}
};
44 changes: 34 additions & 10 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ function initialise_game_logic()
return setInterval(lower_piece,1200);
}

function initialise_visual_tools(temp_height, temp_width)
{
var temp_canvas = document.getElementById('playfield_section');
return new Toolkit(temp_canvas,temp_canvas.getContext('2d'),temp_height,temp_width);
}

function save_current_blocks_to_playfield()
{
//TODO: current_block and playfield
for(var cany = 0 ; cany < current_block.size; cany++)
{
for(var canx = 0 ; canx < current_block.size ; canx++)
Expand Down Expand Up @@ -89,12 +96,12 @@ function generate_block(block_type, temp_position_y,temp_position_x)

function locked()
{

//TODO: kit, playfield current_block
save_current_blocks_to_playfield();
check_playfield_for_completed_line();
current_block = generate_block(choose_next_block(),initialy,initialx);
update_next_block_window_frame();
update_next_block_window();
kit.update_next_block_window_frame(playfield);
kit.update_next_block_window(playfield);
//Check if game over
if(check_game_over()) reset_game();
update_visuals(playfield,current_block,kit);
Expand All @@ -114,14 +121,15 @@ function check_game_over()

function lower_piece()
{
//TODO: current_block
//move
//check if locked
if(at_bottom(current_block) || current_block.blocked)
if(current_block.at_bottom() || current_block.blocked)
{
locked();
}

if(move(current_block,1,0) == 2)
if(current_block.move(1,0) == 2)
{
current_block.blocked = true;
}
Expand Down Expand Up @@ -197,14 +205,13 @@ function reset_game()
generate_block_ids();
current_block = generate_block(choose_next_block(),initialy,initialx);
saved_block = 0;
update_next_block_window_frame();
update_next_block_window();
update_hold_block_window_frame();
update_hold_block_window();
kit.update_next_block_window_frame(playfield);
kit.update_next_block_window(playfield);
kit.update_hold_block_window_frame(playfield);
kit.update_hold_block_window(playfield, saved_block);
update_visuals(playfield, current_block, kit);
}


function overwrite_array_dimensions(temp_target_array, old_x, old_y)
{
temp_target_array = [old_y];
Expand All @@ -216,3 +223,20 @@ function overwrite_array_dimensions(temp_target_array, old_x, old_y)

reset_array(temp_target_array,old_y,old_x);
}

function update_visuals(temp_playfield, temp_block, temp_toolkit)
{
temp_toolkit.print_playfield_to_canvas(temp_playfield);
temp_toolkit.print_ghost_to_canvas(temp_playfield,temp_block);
temp_toolkit.print_currentblock_to_canvas(temp_block);
temp_toolkit.update_line_count();
}

function change_canvas_size(temp_height,temp_width)
{
//TODO kit
kit.canvas.style.height = visual_height;
kit.canvas.style.width = visual_width;
kit.canvas.height = visual_height;
kit.canvas.width = visual_width;
}
Loading

0 comments on commit 7267c82

Please sign in to comment.