summaryrefslogtreecommitdiff
path: root/tetris
diff options
context:
space:
mode:
authortomsmeding <hallo@tomsmeding.nl>2015-09-12 09:52:00 +0200
committertomsmeding <hallo@tomsmeding.nl>2015-09-12 09:52:00 +0200
commit92c104d8a2ba69db1f59890d2f62019186aece05 (patch)
tree0054658c2f26a55e1872c32150c9ad09ecf46c7f /tetris
parentcff41b52e0f801eede5428d97686661b95b9756a (diff)
almost-working engine! :tada:
Diffstat (limited to 'tetris')
-rw-r--r--tetris/blocks.prn73
-rw-r--r--tetris/blocks.txt118
-rw-r--r--tetris/engine.prn166
-rw-r--r--tetris/io.prn28
-rw-r--r--tetris/tetris.prn27
-rw-r--r--tetris/utility.prn29
6 files changed, 380 insertions, 61 deletions
diff --git a/tetris/blocks.prn b/tetris/blocks.prn
index 0eb91f5..17d783e 100644
--- a/tetris/blocks.prn
+++ b/tetris/blocks.prn
@@ -1,41 +1,56 @@
7 "b_nblocks" store
-15 23 71 51 54 99 114
+15 4369 15 4369 4 mkarrayp #I
+23 547 116 785 4 mkarrayp #L
+71 802 113 275 4 mkarrayp #J
+51 51 51 51 4 mkarrayp #O
+54 561 54 561 4 mkarrayp #S
+99 306 99 306 4 mkarrayp #Z
+114 610 624 562 4 mkarrayp #T
b_nblocks mkarrayp "b_blocks" store
- 6 3 4 7 2 1 5
+6 3 4 7 2 1 5 #the tetris standard colours -- sort of
b_nblocks mkarrayp "b_blockclrs" store
-@defun "b_getblock" { #arg: block idx [0..(b_nblocks-1)]
- "b_blocks" swapoutvar swap arridx swap "b_blocks" gstore
+
+#returns the bits for the specified block
+@defun "b_getblock" { #args: blockidx blockrot (idx in [0..(b_nblocks-1)], rot in [0..3])
+ "r" store
+ "i" store
+ #"b_blocks" swapoutvar swap arridx swap "b_blocks" gstore
+ "b_blocks" swapoutvar
+ i arridx
+ swap "b_blocks" gstore
+ r arridx
+ swap pop
}
-@defun "b_drawblock" { #args: x y blockidx
- dup "blockidx" store
- b_getblock "blockbits" store
+@defun "b_drawblock" { #args: x y blockidx blockrot
+ "blockrot" store
+ "blockidx" store
+ blockidx blockrot b_getblock "blockbits" store
"y" store
"x" store
- blockbits 16 % "row1" store
- blockbits 16 / "row2" store
-
- x "xx" store
- row1 while
- row1 2 % if
- xx y blockidx io_gsquare
- end
- xx 1 + "xx" store
- row1 2 / "row1" store
- row1 end
-
- y 1 + "y" store
-
- x "xx" store
- row2 while
- row2 2 % if
- xx y blockidx io_gsquare
- end
- xx 1 + "xx" store
- row2 2 / "row2" store
- row2 end
+ blockbits while
+
+ 0 "i" store
+ i 4 < while
+ blockbits 1 & if
+ x i + y blockidx io_gsquare
+ end
+ i 1 + "i" store
+ blockbits 2 / "blockbits" store
+ i 4 < end
+
+ y 1 + "y" store
+
+ blockbits end
+}
+
+@defun "b_undrawblock" { #args: x y blockidx blockrot
+ io_blockchar "temp" store
+ " " "io_blockchar" gstore
+ b_drawblock
+ temp "io_blockchar" gstore
}
diff --git a/tetris/blocks.txt b/tetris/blocks.txt
index f0f55f1..b08abab 100644
--- a/tetris/blocks.txt
+++ b/tetris/blocks.txt
@@ -2,42 +2,150 @@ THE TEMPLATE:
....
....
+....
+....
-byte: 76543210
+the 2 bytes: fedcba98 76543210
0123
4567
+89ab
+cdef
THE BLOCKS:
****
....
+....
+.... -> 15
+
+*...
+*...
+*...
+*... -> 4369
+
-> 15
+-> 4369
+
+
+
***.
*...
--> 23
+....
+.... -> 23
+
+**..
+.*..
+.*..
+.... -> 547
+
+..*.
+***.
+....
+.... -> 116
+
+*...
+*...
+**..
+.... -> 785
+
+
***.
..*.
--> 71
+....
+.... -> 71
+
+.*..
+.*..
+**..
+.... -> 802
+
+*...
+***.
+....
+.... -> 113
+
+**..
+*...
+*...
+.... -> 275
+
+
**..
**..
+....
+.... -> 51
+
+-> 51
+
+-> 51
+
-> 51
+
+
.**.
**..
+....
+.... -> 54
+
+*...
+**..
+.*..
+.... -> 561
+
-> 54
+-> 561
+
+
+
**..
.**.
+....
+.... -> 99
+
+.*..
+**..
+*...
+.... -> 306
+
-> 99
+-> 306
+
+
+
+.*..
+***.
+....
+.... -> 114
+
.*..
+.**.
+.*..
+.... -> 610
+
+....
***.
--> 114
+.*..
+.... -> 624
+
+.*..
+**..
+.*..
+.... -> 562
+
-15 23 71 51 54 99 114
+15 4369 15 4369
+23 547 116 785
+71 802 113 275
+51 51 51 51
+54 561 54 561
+99 306 99 306
+114 610 624 562
diff --git a/tetris/engine.prn b/tetris/engine.prn
index ebfb0c7..bd00433 100644
--- a/tetris/engine.prn
+++ b/tetris/engine.prn
@@ -1,4 +1,36 @@
-mkarray "e_blockqueue" store
+#Elements in e_blockqueue and e_boardblock are in the form (rot<<3) + idx
+
+
+mkarray "e_blockqueue" store #(rot<<3) + idx
+
+@defun "e_init" {
+ -1 ntiles_x ntiles_y * replicate
+ ntiles_x ntiles_y * mkarrayp
+ "e_boardblock" gstore #(rot<<3) + idx
+
+ 0 ntiles_x ntiles_y * replicate
+ ntiles_x ntiles_y * mkarrayp
+ "e_boardfilled" gstore #booleans
+
+ e_pushnewblock
+ e_newcblock #initialises cblock vars
+}
+
+@defun "e_test" {
+ 0 "i" store
+ i b_nblocks < while
+ 0 "j" store
+ j 4 < while
+ io_clearscreen
+ io_windowbox
+ 0 0 i j b_drawblock
+ 1000000 sleep
+ j 1 + "j" store
+ j 4 < end
+ i 1 + "i" store
+ i b_nblocks < end
+ io_pos_after io_goto
+}
@defun "e_nextblock" {
"e_blockqueue" swapoutvar
@@ -8,16 +40,138 @@ mkarray "e_blockqueue" store
end
0 arridx
swap
- arrpopf "e_blockqueue" store
+ arrpopf "e_blockqueue" gstore
+ e_pushnewblock
}
@defun "e_peekblock" {
"e_blockqueue" swapoutvar
- arrlen 2 < if
- 0
+ arrlen 1 < if
+ -1
else
- 1 arridx
+ 0 arridx
end
swap
- "e_blockqueue" store
+ "e_blockqueue" gstore
+}
+
+@defun "e_pushnewblock" {
+ "e_blockqueue" swapoutvar
+ 0 7 randr arrpush
+ "e_blockqueue" gstore
+}
+
+@defun "e_newcblock" {
+ e_nextblock "e_cblock" gstore
+ ntiles_x 2 - 2 / "e_cblockpos" gstore
+}
+
+@defun "e_fullrender" {
+ io_clearscreen
+ io_windowbox
+
+ io_printheader
+
+ "e_boardblock" swapoutvar
+ arrlen "sz" store
+ 0 "i" store
+ i sz < while
+ i arridx
+ dup -1 = ! if
+ i ntiles_x % swap
+ i ntiles_x / swap
+ dup 8 % swap 8 /
+ b_drawblock
+ else
+ pop
+ end
+ i 1 + "i" store
+ i sz < end
+ "e_boardblock" gstore
+
+ io_pos_windowend pop 4 + 2 io_pos_windowstart add2 io_goto
+ "Next block:" print
+ ntiles_x 3 + 1
+ e_peekblock dup 8 % swap 8 /
+ b_drawblock
+
+ e_cblockpos dup ntiles_x % swap ntiles_x /
+ e_cblock dup 8 % swap 8 /
+ dup4
+ b_drawblock
+ sleep_time sleep
+ b_undrawblock
+}
+
+@defun "e_validcbp" { #args: cblock cblockpos (compares purely to e_boardfilled)
+ dup ntiles_x % swap ntiles_x / "cbposy" store "cbposx" store
+ dup 8 % swap 8 / b_getblock "bits" store
+ 1 "retval" store
+ 0 "y" store
+ bits while
+ 0 "x" store
+ x 4 < bits !! & while
+ bits 1 & if
+ cbposx x + 1 <
+ cbposx x + ntiles_x 1 - > |
+ cbposy y + ntiles_y 1 - > | if
+ 0 "retval" store
+ 0 "bits" store #make the loops break
+ else
+ "e_boardfilled" swapoutvar
+ ntiles_x cbposy y + * cbposx x + +
+ arridx
+ swap "e_boardfilled" gstore
+ if
+ 0 "retval" store
+ 0 "bits" store #make the loops break
+ end
+ end
+ end
+ bits 2 / "bits" store
+ x 1 + "x" store
+ x 4 < bits !! & end
+ y 1 + "y" store
+ bits end
+ retval
+}
+
+@defun "e_applyblock" { #args: block blockpos
+ dup "blockpos" store
+ dup ntiles_x % swap ntiles_x / "blockposy" store "blockposx" store
+ dup "block" store
+ dup 8 % swap 8 / b_getblock "bits" store
+
+ "e_boardblock" swapoutvar
+ block blockpos arrset
+ "e_boardblock" gstore
+
+ "e_boardfilled" swapoutvar
+ 0 "y" store
+ y 4 < bits !! & while
+ 0 "x" store
+ x 4 < while
+ bits 1 & if
+ 1 blockposy y + ntiles_x * blockposx x + + arrset
+ end
+ bits 2 / "bits" store
+ x 1 + "x" store
+ x 4 < end
+ y 1 + "y" store
+ y 4 < bits !! & end
+ "e_boardfilled" gstore
+}
+
+@defun "e_runloop" {
+ 1 while
+ e_fullrender
+ e_cblockpos ntiles_x + #new cblockpos
+ dup e_cblock swap e_validcbp if
+ "e_cblockpos" gstore
+ else
+ pop
+ e_cblock e_cblockpos e_applyblock
+ e_newcblock
+ end
+ 1 end
}
diff --git a/tetris/io.prn b/tetris/io.prn
index 539cbee..d749150 100644
--- a/tetris/io.prn
+++ b/tetris/io.prn
@@ -1,17 +1,12 @@
-2 "padding_x" store
-2 "padding_y" store
-10 "ntiles_x" store
-12 "ntiles_y" store
-3 "tile_sizex" store
-2 "tile_sizey" store
+"#" "io_blockchar" store
@defun "io_pos_windowstart" { #puts: x y
- 0 0
+ origin_x origin_y
}
@defun "io_pos_windowend" { #puts: x y
- ntiles_x tile_sizex * padding_x 2 * + 1 -
- ntiles_y tile_sizey * padding_y 2 * +
+ ntiles_x tile_sizex * padding_x 2 * + origin_x +
+ ntiles_y tile_sizey * padding_y 2 * + origin_y +
}
@defun "io_pos_after" { #puts: x y
@@ -86,7 +81,7 @@
}
@defun "io_windowbox" {
- io_pos_windowstart io_pos_windowend io_pos_windowstart diff2 io_box
+ io_pos_windowstart io_pos_windowend io_pos_windowstart diff2 2 2 diff2 io_box
}
@defun "io_square" { #args: x y
@@ -98,14 +93,14 @@
#"| |" print
#x y 2 + io_goto
#"+--+" print
- "xxx\x1B[3D\x1B[Bxxx" print
+ io_blockchar dup dup + + dup "\x1B[3D\x1B[B" swap + + print
}
@defun "io_pos_square" { #args: x y
swap
- tile_sizex * padding_x +
+ tile_sizex * padding_x + origin_x +
swap
- tile_sizey * padding_y +
+ tile_sizey * padding_y + origin_y +
}
@defun "io_gsquare" { #args: x y blockidx
@@ -123,3 +118,10 @@
@defun "io_resetcolour" {
"\x1B[0m" print
}
+
+
+@defun "io_printheader" {
+ 0 0 io_goto
+ "##### ##### ##### #### ### ####\n # # # # # # #\n # #### # #### # ###\n # # # # # # #\n # ##### # # # ### ####"
+ print
+}
diff --git a/tetris/tetris.prn b/tetris/tetris.prn
index 3f021b9..c11ec57 100644
--- a/tetris/tetris.prn
+++ b/tetris/tetris.prn
@@ -1,13 +1,24 @@
@includeonce "io.prn"
@includeonce "utility.prn"
@includeonce "blocks.prn"
+@includeonce "engine.prn"
-0 "i" store
-i b_nblocks < while
- io_clearscreen
- io_windowbox
- 0 0 i b_drawblock
- 1000000 sleep
- i 1 + "i" store
-i b_nblocks < end
+
+#PARAMETERS
+0 "origin_x" store
+5 "origin_y" store
+3 "padding_x" store
+2 "padding_y" store
+11 "ntiles_x" store
+13 "ntiles_y" store
+3 "tile_sizex" store
+2 "tile_sizey" store
+
+500000 "sleep_time" store
+
+
+#RUNNING THE GAME
+e_init
+#e_test
+e_runloop
io_pos_after io_goto
diff --git a/tetris/utility.prn b/tetris/utility.prn
index ca68dfe..3a4b912 100644
--- a/tetris/utility.prn
+++ b/tetris/utility.prn
@@ -1,3 +1,12 @@
+@defun "add2" {
+ "y2" store
+ "x2" store
+ "y1" store
+ "x1" store
+ x1 x2 +
+ y1 y2 +
+}
+
@defun "diff2" {
"y2" store
"x2" store
@@ -13,6 +22,14 @@
a b a b
}
+@defun "dup4" {
+ "d" store
+ "c" store
+ "b" store
+ "a" store
+ a b c d a b c d
+}
+
@defun "swap2" {
"b2" store
"b1" store
@@ -20,3 +37,15 @@
"a1" store
b1 b2 a1 a2
}
+
+@defun "replicate" { #args: value times
+ dup 1 < if
+ pop pop
+ else
+ 1 - "t" store
+ t 0 > while
+ dup
+ t 1 - "t" store
+ t 0 > end
+ end
+}