This is an old revision of the document!
The game will start with a simple hello menu. Then, the grid and score will be displayed on the LCD screen.
User input:
Any functionality of the game will be implemented both ways, either by interpreting screen touch or joystick input.
Modules used:
Components will be connected as in the below block diagram:
Screen is using 3V3 for logical 1 while Arduino UNO uses 5V as pin output. Thus voltage dividers were added in order to prevent screen damage.
To obtain a good ration, two pairs of resistors were used:
Arduino SPI interface is shared between display and touchscreen.
From the module, only the left joystick is used, thus only a couple of pins are connected.
Component | Shop Link | Datasheet Link |
---|---|---|
TFT LCD 2.8” | eMAG Product Page | Datasheet & info |
Arduino Uno | eMAG Product Page | Datasheet |
Joystick Module | eMAG Product Page | Datasheet |
Resistors | eMAG Product Page | Datasheet |
Adafruit_GFX.h
Adafruit_ILI9341.h
XPT2046_Touchscreen
EEPROM.h
avr/sleep.h
Code splits itself in two main components:
→ At first, a starting menu is displayed and on button press, the game starts.
→ While playing, possible moves are LEFT
,RIGHT
, UP
and DOWN
and depending on the direction the grid changes.
→ If a tile with value 2048
is reached, the game is considered to be won and a win screen is displayed.
→ Best score is updated if necessary and start of a new game is possible on button push.
→ If no more possible moves are available, that is grid is full and no tiles can coalesce anymore, game is considered to be lost.
→ Best score is updated and lose screen is available.
→ However, user has possibility of performing an Undo
, which could lead to possibility of playing longer if a convenient tile is generated.
→ At any point in the game, 3 undos are possible. If more are requested, state is not changed.
→ On every move, if grid has changed, a new tile appears in a random position.
→ Bear in mind that if grid does not change, no tile is added in order to avoid fake moves.
→ Value of new tiles is either 2
or 4
, but they are added with different probabilities.
→ 2
appears in 8 out of 10 case, while 4
otherwise.
→ This ensures a longer game.
→ However, this might mean more trials when performing an undo if game was lost.
→ random()
function was used to generate both tile values and tile position.
→ Tile position is randomly chosen from list of free positions.
→ At game start, randomSeed is initialized with analog value of a floating pin in order to avoid predictibility.