2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
extends Control
|
|
|
|
|
|
|
|
# Simple Tetris-like demo, (c) 2012 Juan Linietsky
|
|
|
|
# Implemented by using a regular Control and drawing on it during the _draw() callback.
|
|
|
|
# The drawing surface is updated only when changes happen (by calling update())
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
# Member variables
|
2014-02-10 02:10:30 +01:00
|
|
|
var score = 0
|
2015-11-21 16:13:43 +01:00
|
|
|
var score_label = null
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
const MAX_SHAPES = 7
|
|
|
|
|
|
|
|
var block = preload("block.png")
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var block_colors = [
|
|
|
|
Color(1, 0.5, 0.5),
|
|
|
|
Color(0.5, 1, 0.5),
|
|
|
|
Color(0.5, 0.5, 1),
|
|
|
|
Color(0.8, 0.4, 0.8),
|
|
|
|
Color(0.8, 0.8, 0.4),
|
|
|
|
Color(0.4, 0.8, 0.8),
|
|
|
|
Color(0.7, 0.7, 0.7)]
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var block_shapes = [
|
|
|
|
[ Vector2(0, -1), Vector2(0, 0), Vector2(0, 1), Vector2(0, 2) ], # I
|
|
|
|
[ Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) ], # O
|
|
|
|
[ Vector2(-1, 1), Vector2(0, 1), Vector2(0, 0), Vector2(1, 0) ], # S
|
|
|
|
[ Vector2(1, 1), Vector2(0, 1), Vector2(0, 0), Vector2(-1, 0) ], # Z
|
|
|
|
[ Vector2(-1, 1), Vector2(-1, 0), Vector2(0, 0), Vector2(1, 0) ], # L
|
|
|
|
[ Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(-1, 0) ], # J
|
|
|
|
[ Vector2(0, 1), Vector2(1, 0), Vector2(0, 0), Vector2(-1, 0) ]] # T
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var block_rotations = [
|
|
|
|
Matrix32(Vector2(1, 0), Vector2(0, 1), Vector2()),
|
|
|
|
Matrix32(Vector2(0, 1), Vector2(-1, 0), Vector2()),
|
|
|
|
Matrix32(Vector2(-1, 0), Vector2(0, -1), Vector2()),
|
|
|
|
Matrix32(Vector2(0, -1), Vector2(1, 0), Vector2())]
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var width = 0
|
|
|
|
var height = 0
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var cells = {}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var piece_active = false
|
|
|
|
var piece_shape = 0
|
|
|
|
var piece_pos = Vector2()
|
|
|
|
var piece_rot = 0
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func piece_cell_xform(p, er = 0):
|
|
|
|
var r = (4 + er + piece_rot) % 4
|
|
|
|
return piece_pos + block_rotations[r].xform(p)
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func _draw():
|
|
|
|
var sb = get_stylebox("bg", "Tree") # Use line edit bg
|
|
|
|
draw_style_box(sb, Rect2(Vector2(), get_size()).grow(3))
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
var bs = block.get_size()
|
|
|
|
for y in range(height):
|
|
|
|
for x in range(width):
|
2015-11-21 16:13:43 +01:00
|
|
|
if (Vector2(x, y) in cells):
|
|
|
|
draw_texture_rect(block, Rect2(Vector2(x, y)*bs, bs), false, block_colors[cells[Vector2(x, y)]])
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (piece_active):
|
|
|
|
for c in block_shapes[piece_shape]:
|
2015-11-21 16:13:43 +01:00
|
|
|
draw_texture_rect(block, Rect2(piece_cell_xform(c)*bs, bs), false, block_colors[piece_shape])
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func piece_check_fit(ofs, er = 0):
|
2014-02-10 02:10:30 +01:00
|
|
|
for c in block_shapes[piece_shape]:
|
2015-11-21 16:13:43 +01:00
|
|
|
var pos = piece_cell_xform(c, er) + ofs
|
2014-02-10 02:10:30 +01:00
|
|
|
if (pos.x < 0):
|
|
|
|
return false
|
|
|
|
if (pos.y < 0):
|
|
|
|
return false
|
|
|
|
if (pos.x >= width):
|
|
|
|
return false
|
|
|
|
if (pos.y >= height):
|
|
|
|
return false
|
|
|
|
if (pos in cells):
|
|
|
|
return false
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
return true
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func new_piece():
|
|
|
|
piece_shape = randi() % MAX_SHAPES
|
|
|
|
piece_pos = Vector2(width/2, 0)
|
|
|
|
piece_active = true
|
|
|
|
piece_rot = 0
|
|
|
|
if (piece_shape == 0):
|
|
|
|
piece_pos.y += 1
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (not piece_check_fit(Vector2())):
|
2015-11-21 16:13:43 +01:00
|
|
|
# Game over
|
2014-02-10 02:10:30 +01:00
|
|
|
game_over()
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
update()
|
|
|
|
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
func test_collapse_rows():
|
2015-11-21 16:13:43 +01:00
|
|
|
var accum_down = 0
|
2014-02-10 02:10:30 +01:00
|
|
|
for i in range(height):
|
|
|
|
var y = height - i - 1
|
|
|
|
var collapse = true
|
|
|
|
for x in range(width):
|
2015-11-21 16:13:43 +01:00
|
|
|
if (Vector2(x, y) in cells):
|
2014-02-10 02:10:30 +01:00
|
|
|
if (accum_down):
|
2015-11-21 16:13:43 +01:00
|
|
|
cells[Vector2(x, y + accum_down)] = cells[Vector2(x, y)]
|
2014-02-10 02:10:30 +01:00
|
|
|
else:
|
2015-11-21 16:13:43 +01:00
|
|
|
collapse = false
|
2014-02-10 02:10:30 +01:00
|
|
|
if (accum_down):
|
2015-11-21 16:13:43 +01:00
|
|
|
cells.erase(Vector2(x, y + accum_down))
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
if (collapse):
|
|
|
|
accum_down += 1
|
|
|
|
|
|
|
|
score += accum_down*100
|
2014-02-10 02:10:30 +01:00
|
|
|
score_label.set_text(str(score))
|
2015-11-21 16:13:43 +01:00
|
|
|
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
func game_over():
|
2015-11-21 16:13:43 +01:00
|
|
|
piece_active = false
|
|
|
|
get_node("gameover").set_text("Game over!")
|
|
|
|
update()
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
func restart_pressed():
|
2015-11-21 16:13:43 +01:00
|
|
|
score = 0
|
|
|
|
score_label.set_text("0")
|
|
|
|
cells.clear()
|
|
|
|
get_node("gameover").set_text("")
|
|
|
|
piece_active = true
|
|
|
|
get_node("../restart").release_focus()
|
|
|
|
update()
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
func piece_move_down():
|
|
|
|
if (!piece_active):
|
|
|
|
return
|
2015-11-21 16:13:43 +01:00
|
|
|
if (piece_check_fit(Vector2(0, 1))):
|
|
|
|
piece_pos.y += 1
|
|
|
|
update()
|
2014-02-10 02:10:30 +01:00
|
|
|
else:
|
|
|
|
for c in block_shapes[piece_shape]:
|
|
|
|
var pos = piece_cell_xform(c)
|
2015-11-21 16:13:43 +01:00
|
|
|
cells[pos] = piece_shape
|
2014-02-10 02:10:30 +01:00
|
|
|
test_collapse_rows()
|
|
|
|
new_piece()
|
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func piece_rotate():
|
2014-02-10 02:10:30 +01:00
|
|
|
var adv = 1
|
2015-11-21 16:13:43 +01:00
|
|
|
if (not piece_check_fit(Vector2(), 1)):
|
2014-02-10 02:10:30 +01:00
|
|
|
return
|
|
|
|
piece_rot = (piece_rot + adv) % 4
|
|
|
|
update()
|
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func _input(ie):
|
2014-02-10 02:10:30 +01:00
|
|
|
if (not piece_active):
|
|
|
|
return
|
|
|
|
if (!ie.is_pressed()):
|
|
|
|
return
|
|
|
|
|
|
|
|
if (ie.is_action("move_left")):
|
2015-11-21 16:13:43 +01:00
|
|
|
if (piece_check_fit(Vector2(-1, 0))):
|
|
|
|
piece_pos.x -= 1
|
2014-02-10 02:10:30 +01:00
|
|
|
update()
|
|
|
|
elif (ie.is_action("move_right")):
|
2015-11-21 16:13:43 +01:00
|
|
|
if (piece_check_fit(Vector2(1, 0))):
|
|
|
|
piece_pos.x += 1
|
2014-02-10 02:10:30 +01:00
|
|
|
update()
|
|
|
|
elif (ie.is_action("move_down")):
|
|
|
|
piece_move_down()
|
|
|
|
elif (ie.is_action("rotate")):
|
|
|
|
piece_rotate()
|
2015-11-21 16:13:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
func setup(w, h):
|
|
|
|
width = w
|
|
|
|
height = h
|
|
|
|
set_size(Vector2(w, h)*block.get_size())
|
2014-02-10 02:10:30 +01:00
|
|
|
new_piece()
|
|
|
|
get_node("timer").start()
|
2015-11-21 16:13:43 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
func _ready():
|
2015-11-21 16:13:43 +01:00
|
|
|
setup(10, 20)
|
2014-02-10 02:10:30 +01:00
|
|
|
score_label = get_node("../score")
|
2015-11-21 16:13:43 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
set_process_input(true)
|