Tetris game on mbed


This project is to make a mbed version of the Tetris game.The project uses a 128x128 smart color LCD display to display main game play, a 16x2 text LCD display to display scores, 4 push bottons for game control, a shiftbright led module to add visual effect, a speaker for sound.


4D-Systems-uLCD-144-G2 Text LCD ShiftBright Speaker


Componentmbed Pin
Left buttonp16
Right buttonp19
Rotate buttonp17
Down buttonp30
Speaker inputp21
ShiftBright DIp11
ShiftBright LIp10
ShiftBright EIp9
ShiftBright CIp13
ShiftBright gndgnd
ShiftBright v+VU


Serial Communication

Because the mbed does not have clock, so it is not possible to generate random numbers using time as seed. I write a console application using c# running on PC to communicate with mbed through virtual com port. The mbed will ask PC for a random number every time it needs one. The source code in c# is posted below.

c# code for PC to communicate with mbed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace MBEDTetris
    class Program
        static Random random;
        static SerialPort port;

        static void Main(string[] args)
            port = new SerialPort("COM4");
            random = new Random();
            port.BaudRate = 9600;
            port.Parity = Parity.Odd;
            port.DataBits = 8;
            port.StopBits = StopBits.One;
            port.Handshake = Handshake.None;
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                Console.WriteLine("Port is open.");


        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            string input = port.ReadExisting();
            int i = 0;
            if (input.Contains('a'))
                i = random.Next(7);
            else if(input.Contains('b'))
                i = random.Next(4);
            Console.WriteLine(i + "");

The Program

Import programTetris_GL

This is a remake of tetris game for mbed. Please see detail here http://developer.mbed.org/users/sucrelv/notebook/tetris-game-on-mbed

Game Play Screenshots

Before power up


Waiting for PC side to get ready


Waiting for user input to start game


Game play


PC side console app showing random numbers sent


1 comment on Tetris game on mbed:

10 Sep 2016

"it is not possible to generate random numbers using time as seed."

When I started to learn coding mcu's, I faced this same problem as soon as I made my first game. I was scratching my head hard for a while, even went as far as thinking of measuring thermal noise of a resistor to get true random seed :D

But after all it was very simple.. When player does press button to start a game, start interrupt timer when button goes down, stop the timer when button is released.. And there you have quite true random number generated for seed. Quite impossible to get same number ever again when the timer runs so fast.

Please log in to post comments.