tetris
-rw-r--r--Logic.gd18
-rw-r--r--Main.tscn2
-rw-r--r--Tetris.gd27
3 files changed, 31 insertions, 16 deletions
diff --git a/Logic.gd b/Logic.gd
index 682169d..c919ee5 100644
--- a/Logic.gd
+++ b/Logic.gd
@@ -47,20 +47,18 @@ static func valid_move(matrix: Array, piece, offset := Vector2()) -> bool:
return true
-static func clear_lines(matrix: Array) -> void:
+static func clear_lines(matrix: Array) -> PoolIntArray:
+ var clearable_lines := PoolIntArray()
var y = len(matrix)
- while y > 0:
+ while y > 0: # go from the bottom up
y -= 1
- if matrix[y].min() > 0:
- for yy in range(y, 1, -1):
+ if matrix[y].min() > 0: # if a line has no empty
+ for yy in range(y, 1, -1): # move all lines dodwn
matrix[yy] = matrix[yy - 1]
- y += 1
+ clearable_lines.append(y)
+ y += 1 # and go down one, to check the new thing
+ return clearable_lines
static func get_index_or_null(arr: Array, y: int, x: int) -> int:
return arr[y][x] if y < len(arr) and x < len(arr[y]) else null
-
-
-static func print_matrix(mat: Array):
- for i in mat:
- print(i)
diff --git a/Main.tscn b/Main.tscn
index 4565239..5c802aa 100644
--- a/Main.tscn
+++ b/Main.tscn
@@ -20,7 +20,7 @@ custom_constants/margin_left = 5
custom_constants/margin_bottom = 5
script = ExtResource( 1 )
colors = PoolColorArray( 0.862745, 0.196078, 0.184314, 1, 0.521569, 0.6, 0, 1, 0.14902, 0.545098, 0.823529, 1, 0.423529, 0.443137, 0.768627, 1, 0.709804, 0.537255, 0, 1, 0.576471, 0.631373, 0.631373, 1, 0.164706, 0.631373, 0.596078, 1 )
-transcolors = PoolColorArray( 0.780392, 0.627451, 0.623529, 0.670588, 0.584, 0.6, 0.48, 0.619608, 0.712, 0.815833, 0.89, 0.564706, 0.76, 0.7695, 0.95, 0.784314, 0.79, 0.7505, 0.632, 0.67451, 0.690196, 0.760784, 0.760784, 0.627451, 0.464, 0.58, 0.570333, 0.701961 )
+transcolors = PoolColorArray( 0.780392, 0.627451, 0.623529, 0.670588, 0.584, 0.6, 0.48, 0.619608, 0.712, 0.815833, 0.89, 0.564706, 0.76, 0.7695, 0.95, 0.784314, 0.79, 0.7505, 0.632, 0.67451, 0.690196, 0.760784, 0.760784, 0.627451, 0.282353, 0.580392, 0.54902, 0.337255 )
grid_color = Color( 0.933333, 0.909804, 0.835294, 1 )
bg_color = Color( 0.992157, 0.964706, 0.890196, 1 )
diff --git a/Tetris.gd b/Tetris.gd
index a2b8bd5..d4df1e0 100644
--- a/Tetris.gd
+++ b/Tetris.gd
@@ -166,6 +166,17 @@ func draw_preview(shape: TetrisPiece = current_shape) -> PoolVector2Array:
return set_squares
+func highlight_clearable_lines() -> void:
+ var mat := matrix.duplicate(true)
+ current_shape.embed(mat)
+ var lines = Logic.clear_lines(mat.duplicate(true))
+ for l in lines:
+ for x in range(COLUMNS):
+ var c = colors[mat[l][x] - 1]
+ c.v += .2
+ get_square(Vector2(x, l)).color = c
+
+
func _input(event):
if event is InputEventSwipe:
match event.direction.round().normalized():
@@ -196,10 +207,8 @@ func _input(event):
func tick(create_timer := true):
- if create_timer:
- var t := get_tree().create_timer(.25, false)
- t.connect("timeout", self, "tick")
-
+ var no_draw := false
+ var t_length := .25
if not current_shape.move(Vector2(0, 1), matrix): # invalid move: cant go down
if current_shape.position.y == 0:
get_tree().reload_current_scene()
@@ -210,7 +219,15 @@ func tick(create_timer := true):
Logic.clear_lines(matrix)
else:
current_shape.stopped = true
- draw_board()
+ draw_board()
+ highlight_clearable_lines()
+ no_draw = true
+ t_length = .5
+ if create_timer:
+ var t := get_tree().create_timer(t_length, false)
+ t.connect("timeout", self, "tick")
+ if not no_draw:
+ draw_board()
#warning-ignore:shadowed_variable