Tetris game on mbed
Overview
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.
Component
4D-Systems-uLCD-144-G2 Text LCD ShiftBright Speaker
Connection
Component | mbed Pin |
---|---|
Left button | p16 |
Right button | p19 |
Rotate button | p17 |
Down button | p30 |
Speaker input | p21 |
ShiftBright DI | p11 |
ShiftBright LI | p10 |
ShiftBright EI | p9 |
ShiftBright CI | p13 |
ShiftBright gnd | gnd |
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); try { port.Open(); port.WriteLine(""); Console.WriteLine("Port is open."); } catch { } Console.ReadLine(); Console.ReadLine(); } private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { string input = port.ReadExisting(); Console.WriteLine(input); int i = 0; if (input.Contains('a')) { i = random.Next(7); } else if(input.Contains('b')) { i = random.Next(4); } Console.WriteLine(i + ""); port.WriteLine(i.ToString()); } } }
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:
Please log in to post comments.
"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.