Introduction: DIY a Raspberry Game - 2048

About: Howdy, we are application engineers in Seeed. Sharing projects with the maker community is awesome. Hope you like it XD Seeed is the IoT hardware enabler providing services that empower IoT developers to swift…

Game 【2048】is a single-player sliding block puzzle game by Italian web developer Gabriele Cirulli. 2048 was originally written in JavaScript and CSS during a weekend, and released on March 9, 2014, as free and open-source software subject to the MIT license.

The game's objective is to slide numbered tiles on a grid to combine them to create a tile with the number 2048.

In this project, I rewritten the game 【2048】in Python on Raspberry Pi, and made a little change with it. The numbers in the grid are swapped with a series of words that explain the the process of 【how does an idea become a real product in the market】:

  • 2 => Idea
  • 4 => Design
  • 8 => Make
  • 16 => Prototype
  • 32 => Seeed
  • 64 => Improve
  • 128 => Sample
  • 256 => Product
  • 512 => Promote
  • 1024 => Market
  • 2048 => Success

There is a camera mounted in front of the game machine, when you win the game, it will take a photo of you and list it on the ranklist.

Besides, to add more fun, I connected the game machine to a Canon SELPHY CP1200 photo printer so it can print out the photo of the winner as a reward.

When you finished this tutorial, you can acquire the following knowledge:

  • Be familiar with Raspberry Pi
  • Understand python programming using pyside
  • The skills of dealing with pictures using Pi Camera

Step 1: What Do We Need

Hardware:

Structure:

  • 3mm wood
  • 5mm wood
  • 3mm Bamboo plate
  • 3mm Studs
  • 3mm Nuts
  • 3mm Screws
  • 2mm Studs
  • 2mm Screw
  • Screwdriver

Step 2: Wiring

The Grove – Thumb Joystick has X and Y axes, each of them are ~10K potentiometers and can control 2D movement by generating analog signals. So we connect this module to an analog port – A0.

Then connect the Pi Camera to the Raspberry Pi just as below picture shows.

Step 3: Setup the GrovePi+

The software work occupies the most part of this project. At first we should build the software development environment.

GrovePi is an open source platform for connecting Grove Sensors to the Raspberry Pi. In this project we use GrovePi+ to deal with some real time work, such as the joystick.

You can clickhere to learn how to get started the GrovePi+.

Step 4: Setup the Pi Camera

If you are using the Raspbian distro, it is best for you to install picamera using the system’s package manager: apt. This will ensure that the picamera will always be up-to-date and also easy to remove if you want in the future. It will also make picamera available for all users on the system. To install picamera using apt simply:

$ sudo apt-get update

$ sudo apt-get install python-picamera python3-picamera

Make sure the camera module is not in touch with on any conductive object like the Pi’s USB ports or its GPIO pins. Now, apply power to your Pi. Once booted, start the Raspberry Pi Configuration utility and enable the camera module:

More information
about Pi Camera you can click here.

Step 5: Setup the Photo Printer

In this project, we select a Canon-SELPHY-CP1200-Wireless-Compact-Photo printer, even though the printer supports wireless connect, to ensure the stability of system we use mini USB cable to connect Photo Printer with Raspberry Pi.

Now we should install the printer driver CUPS :

sudo apt-get install cups

More info. about how to install and use cups please clickhere.

Then configure the printer as below picture shows.

  1. Add printer and select canon SELPHY CP1200.
  2. Click “Continue”.
  3. Then, we should set the photo size, in this project we set the media size “Card 54×86mm”.

At last we should install some libraries for pycups.

sudo apt-get install python-dev

sudo apt-get install libcups2-dev

Download the pycups code and install

git clone git://git.fedorahosted.org/git/pycups.git

sudo python setup.py install

Now, we can print a photo to have a test.

Step 6: Install Pyside and Download the Project Code

We use pyside to develop the game UI.

sudo apt-get install python-pyside

And then download the code from github.

cd ~

git clone <a href="https://github.com/Lee-Kevin/RPI_Photo_booth"> https://github.com/Lee-Kevin/RPI_Photo_booth</a>

Now you can run the seeed.py to see whether the game start normally. And if you’re luckily enough you can see the picture as below shows.

Step 7: Setup the Startup Script

At last, we want the game to be lanuched automatically when we boot our Raspberry pi.

The alternative method is to create a configuration file that is unique to the currently logged in user. First you need to edit this text file :

sudo nano ~/.config/lxsession/LXDE/autostart

This file represents a list of commands to be executed when the GUI loads. It is usually blank when you first edit it so just add the applications you need to auto-load:

For this project You can auto-launch th Python scripts by adding the line :

@/usr/bin/python /home/pi/RPI_Photo_booth/seeed.py

This works best in this example the Python script is stored in the home directory of the default Pi user. If another user is logged in they wouldn’t have access to this directory so LXDE may not be able to autoload it.

To save and exit the nano editor press CTRL-X, Y and then ENTER.

Step 8: Laser Cut the Plate

Download the file and laser cut it.

Here we use 3mm wood, 5mm wood and 3mm Bamboo plate.

Step 9: Install the Screen Section

Installation screen part, simple structure, fixed with screws

Step 10: Install the Hardware

Here, use screws and nuts fix the hardware into wood.

In order to balance the forces behind, you can install several support columns.

Step 11: Install the Housing

Installation of the shell is a bit more difficult, please be patient. You can firstly make sure every screws are fixed without tighten them, so you can adjust the board position and make sure the mount holes are aligned, and then tighten the screws when you find out everthing is OK.

BTW, do not forget to install the back cover and buttons, also, do not forget to lead the camera‘s cable!

Step 12: Install the Camera and Top Panel

The assembly of the camera part is relatively simple, After assembly, insert it into the corresponding hole, fix it. Then secure the top cover with double-sided tape

Step 13: The Result

Now you can use the Grove – Thumb Joystick to play this game. When you get Market(1024), the camera will start and take a photo of yours then print it out, at the same time your photo will be shown on the Ranklist. Enjoy!