Creating TetrisClock using OpenCV #2
Implementing text tetris
In the previous article, you learned how to implement numbers. This time, I will port the character tetris implemented by Brian Lough to Python in https://github.com/toblum/TetrisAnimation/blob/master/src/TetrisLetters.h.
It is not very different from the previous number implementation. You only need to port the values of the fall_instr_let structure that makes up the character.
Original code analysis
Character Analysis
You
can find out by looking at https://github.com/toblum/TetrisAnimation/blob/master/src/TetrisLetters.h
for Tetris character. This header file contains information that can represent the characters from ascii code 33 (!) To ascii code 90 (Z) in tetris.
// // ASCII characters // // ********************************************************************* // ascii 33 - ! // ********************************************************************* fall_instr_let a33[5] = { {0,0,2,16,0}, {2,5,3,13,2}, {1,3,1,13,2}, {7,1,1,10,3}, {7,6,3,10,0} };
<Ascii code 33>
// ********************************************************************* // ascii 90 - Z // ********************************************************************* fall_instr_let a90[9] = { {2,5,0,16,1}, {1,3,3,16,3}, {3,1,1,15,0}, {0,0,2,14,0}, {7,6,3,13,2}, {1,1,4,11,2}, {4,7,3,11,0}, {7,6,1,10,3}, {7,2,0,10,1} };
<Ascii code 90>
It is a little different from the previous post. The shape is not 0 ~ 6 but 7 is newly seen. A number of new shapes were added to the numbers to represent the letters.
<New shape for character Tetris>
Complementing this point, there is no problem in expressing the text in Tetris.
Python Porting
Python Code
''' ascii 33 - ! ''' a33 = ( (0,0,2,16,0), (2,5,3,13,2), (1,3,1,13,2), (7,1,1,10,3), (7,6,3,10,0) ) ''' skip ''' ''' ascii 90 - Z ''' a90 = ( (2,5,0,16,1), (1,3,3,16,3), (3,1,1,15,0), (0,0,2,14,0), (7,6,3,13,2), (1,1,4,11,2), (4,7,3,11,0), (7,6,1,10,3), (7,2,0,10,1) ) ''' skip ''' mychars = (a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \ a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, a61, a62, a63, a64, a65, a66, a67, a68, a69, a70, \ a71, a72, a73, a74, a75, a76, a77, a78, a79, a80, a81, a82, a83, a84, a85, a86, a87, a88, a89, a90 ) ''' skip ''' def get_tetris_char(ascii): n = ord(ascii) if(n < 33 or n > 90): return None return mychars[n - 33] def draw_shape(canvas, x, y, color, shape, rotate, y_pos): ''' skip ''' elif shape == 7: if rot == 0: for i in range(x, x + 1*scale): for j in range(y, y + 1*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True for i in range(x, x + 2*scale): for j in range(y + 1*scale, y + 2*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True elif rot == 1: for i in range(x, x + 2*scale): for j in range(y, y + 1*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True for i in range(x, x + 1*scale): for j in range(y + 1*scale, y + 2*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True elif rot == 2: for i in range(x, x + 2*scale): for j in range(y, y + 1*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True for i in range(x + 1*scale, x + 2*scale): for j in range(y + 1*scale, y + 2*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True elif rot == 3: for i in range(x + 1*scale, x + 2*scale): for j in range(y, y + 1*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True for i in range(x, x + 2*scale): for j in range(y + 1*scale, y + 2*scale): tcanvas[j, i] = color if j == (y_pos - 1): ret = True
Like numbers, convert Tetris information for ascii characters into Python tuples. Then, add a function get_tetris_char that can retrieve a Tetris tuple corresponding to the ASCII character.
Then, add the new drawable shape to draw_shape function.
Then, add the new drawable shape to draw_shape function.
If you run the code, you can see this window.
python3 tetris_chars.py
Wrapping up
In addition to the numbers in the previous post, this post showed how to represent letters in tetris. Tobias Blum's github, Brian Lough's github made a very interesting Tetris Clock that works on the ESP32. I also made their Tetris watch that works on ESP32 and introduced it on my post https://iot-for-maker.blogspot.com/2020/04/led-9-rgb-led-matrix-drive-with-esp-32.html . However, I prefer a platform like Raspberry Pi with a Linux operating system over MCUs like Arduino, ESP32, ESP8266. So I wanted to make their wonderful clock work on Raspberry Pi. The two articles introduced here are the basics for implementing a Tetris clock using Python OpenCV. The actual implementation of the Tetris Clock using the RGB LED Matrix and the Raspberry Pi will continue to be introduced in my next blog, LED display For Makers. If you are interested in the clock that actually works on the LED Matrix, stay tuned to the LED display For Makers articles.
댓글
댓글 쓰기