tetris
update tetronimo picking
bendn 2022-08-19
parent a97b11e · commit 6b32149
-rw-r--r--Logic.gd34
-rw-r--r--Tetris.gd17
-rw-r--r--TetrisPiece.gd11
3 files changed, 35 insertions, 27 deletions
diff --git a/Logic.gd b/Logic.gd
index 946604d..682169d 100644
--- a/Logic.gd
+++ b/Logic.gd
@@ -1,23 +1,23 @@
extends Reference
class_name Logic
-const shapes = [
- [ 0, 0, 0, 0,
- 1, 1, 1, 1],
- [ 0, 0, 0, 0,
- 1, 1, 1, 0,
- 1 ],
- [ 1, 1, 1, 0,
- 0, 0, 1 ],
- [ 0, 1, 1, 0,
- 0, 1, 1 ],
- [ 1, 1, 0, 0,
- 0, 1, 1 ],
- [ 0, 1, 1, 0,
- 1, 1 ],
- [ 0, 1, 0, 0,
- 1, 1, 1 ]
-];
+const i := [ 0, 0, 0, 0,
+ 1, 1, 1, 1 ]
+const j := [ 0, 1, 1, 1,
+ 0, 0, 0, 1 ]
+const l := [ 1, 1, 1, 0,
+ 1 ]
+const o := [ 0, 1, 1, 0,
+ 0, 1, 1 ]
+const s := [ 0, 1, 1, 0,
+ 1, 1 ]
+const t := [ 0, 1, 0, 0,
+ 1, 1, 1 ]
+const z := [ 1, 1, 0, 0,
+ 0, 1, 1 ]
+
+const piece_bag := [i, i, i, i, j, j, j, j, l, l, l, l, o, o, o, o, s, s, s, s, t, t, t, t, z, z, z, z]
+const piece_indexs := {i: 0, j: 1, l: 2, o: 3, s: 4, t: 5, z: 6}
static func create_matrix(rows: int, columns: int) -> Array:
diff --git a/Tetris.gd b/Tetris.gd
index a249126..c54ad8a 100644
--- a/Tetris.gd
+++ b/Tetris.gd
@@ -14,6 +14,15 @@ var matrix := Logic.create_matrix(ROWS, COLUMNS)
var bg_squares := []
var squares := []
+var bag = []
+
+
+func random_shape() -> Array:
+ if bag.empty():
+ bag = Logic.piece_bag.duplicate(true)
+ randomize()
+ return bag.pop_at(randi() % len(bag))
+
func get_square(at: Vector2) -> Control:
return squares[at.y * COLUMNS + at.x]
@@ -62,8 +71,8 @@ func _ready() -> void:
c.color = bg_color
add_child(c)
set_process(false)
- assert(len(colors) == Logic.shapes.size()) # 7
- assert(len(transcolors) == Logic.shapes.size())
+ assert(len(colors) == 7)
+ assert(len(transcolors) == 7)
ratio = float(COLUMNS) / float(ROWS)
init_squares()
start()
@@ -72,7 +81,7 @@ func _ready() -> void:
func start() -> void:
set_process(true)
# warning-ignore-all:integer_division
- current_shape = TetrisPiece.new(Vector2((COLUMNS / 2) - 2, 0))
+ current_shape = TetrisPiece.new(Vector2((COLUMNS / 2) - 2, 0), random_shape())
tick()
@@ -139,7 +148,7 @@ func tick(create_timer := true):
if current_shape.stopped:
print(ascii(matrix))
current_shape.embed(matrix)
- current_shape = TetrisPiece.new(Vector2(randi() % (COLUMNS - 4), 0))
+ current_shape = TetrisPiece.new(Vector2(randi() % (COLUMNS - 4), 0), random_shape())
Logic.clear_lines(matrix)
else:
current_shape.stopped = true
diff --git a/TetrisPiece.gd b/TetrisPiece.gd
index 584ac8f..27f9280 100644
--- a/TetrisPiece.gd
+++ b/TetrisPiece.gd
@@ -2,17 +2,16 @@ extends Resource
class_name TetrisPiece
var shape_type: int
-var shape := []
+var shape: Array
var position: Vector2
var stopped := false
-func _init(pos: Vector2) -> void:
+func _init(pos: Vector2, _s: Array) -> void:
position = pos
- randomize()
- shape_type = randi() % len(Logic.shapes)
- var s: PoolIntArray = [0, 0, 0, 0]
- s.append_array(Logic.shapes[shape_type])
+ shape_type = Logic.piece_indexs[_s]
+ var s := [0, 0, 0, 0]
+ s.append_array(_s)
for y in range(4):
shape.append([])