TigerJython4Kids
micro:bit Robotik
tetris
Deutsch   English   

WORKSHEET 4: MICRO TETRIS

 

 

NEOPIXEL

 

To play this game you need the micro:pix 4by8 board for BBC micro:bit (Source: of supplywww.proto-pic.co.uk). It is a matrix of 32 color LEDs called neopixels. The micro:bit is simply inserted in  the board's header.

 


 

GAME DESCRIPTION

 

The game is a simplified version of the famous Tetris game. You hold the board in your hand so that you have 4 columns and 8 rows of LEDs.

At the beginning of the game, 4 colors red, green, blue and yellow appear in the bottom row, specifying a color identifier of each column.

In the top row a pixel appears in a random column. It is colored in one of  the 4 colors and jumps down from row to row. You can change its horizontal position using the left and right buttons provided that there are no  bright pixels where you want to go. The aim is to stack up the falling pixels at the row with the same color.
 

If a pixel falls on on a pixel with different color, the falling pixel is deleted, as well as the underlying pixel (except for bottom pixels). In total, 24 color pixels are dropped and the challenge is to place as many pixels as possible in the correct columns.

 

 

STEP 1: ACTIVATING NEOPIXEL

 

Before you start constructing the game, you need to know how neopixels are handled with a Python program. First you import the module neopixel and create a variable np:

np = NeoPixel(pin0, 32)

The parameters determine how many LEDs you have and which port is used.

np is a list and the list index 0..31 correspond to the position of the LED (see figure). To set the color of a neopixel, you assign a tuple with RGB values to its corresponding list element.

np[pix] = (r, g, b)

The parameters r, g, b define the red, green and blue color component (integers 0..255, to avoid consuming too much current, use values between 0 and 50).

In order that any modification of np becomes visible, you must call

np.show()

 

For example, in the following program, you cycle through all neopixels and turn them on in green color for 200 ms.

Program:

from microbit import *
from neopixel import *

np = NeoPixel(pin0, 32)

for pix in range(32):
        np[pix] = (0, 50, 0)
        np.show()
        sleep(300)
        np[pix] = (0, 0, 0)
► Copy to clipboard

It is preferable that you define a x-y coordinate system, so you can refer to a neopixel with its row and column coordinates x and y. To do so, define a function toPix(x, y) that returns the corresponding pixel number (0 - 31) from the row and column coordinate defined as seen in the figure.

If your code in toPix() is correct, the following program displays the pixels in each column from left to right.

 

Program:

from microbit import *
from neopixel import *
import neopixel

def toPix(x, y):
    return ..... 

np = NeoPixel(pin0, 32)

for x in range(4):
    for y in range(8):
        pix = toPix(x, y)
        np[pix] = (50, 0, 0)
        np.show()
        sleep(300)
        np[pix] = (0, 0, 0)
        np.show()
► Copy to clipboard

 

 

STEP 2: FALLING PIXELS IN RANDOM COLORS

 

You define 4 colors red, green, blue and yellow as RGB tuples and save them in a colors list:

colors = [(50, 0, 0),(0, 50, 0),(0, 0, 50), (20, 20, 0)]

Your program should select a color and a column randomly and move a pixel down from row to row. When it arrives at the bottom, the next pixel should fall down.

 

 

STEP 3: CHANGE COLUMN

 

By clicking on button A, the falling pixel should move one column to the left, by clicking on button B one column to the right. Note that x may only take the values 0, 1, 2 or 3.

To process the button click, use the following code:

if button_a.was_pressed()and x < 3:
    x += 1
if button_b.was_pressed()and x > 0: 
    x -= 1

 

 

STEP 4: SET COLUMN COLORS

 

Write a function init(), which defines the column colors shown in row 7. Choose the colors from the colors list, where each color is allowed only once.

Next you have to make ensure that the pixels in row 7 are not "overwritten" by the falling pixels.

 

 

STEP 5: RECOGNIZE OCCUPIED POSITIONS

 

A pixel must stop falling when the position below is occupied. You should distinguish the following cases: If the position below is free, there is no problem and you can move the pixel down. If there is already a pixel and it has the same color, you can leave the pixel where is is; otherwise you delete the pixel below and the pixel itself.

Hint: You can retrieve the color c of a pixel by calling c = np[pix].

 

 

STEP 6: RESTRICT LEFT / RIGHT MOVEMENT

 

Prevent the movement by button actions if the position to the left or right is occupied.

 

 

STEP 7: END OF GAME/ SHOW RESULTS

 

After all 24 pixels are played, you want to write out how many of them actually remained on the game field. Write out this game score as scrolling text.

 

 

STEP 8: REALIZE YOUR OWN IDEAS

 

Modify the game to your own ideas to give it a personal touch.