DIY a Raspberry Game - 2048

by Seeed Studio in Circuits > Raspberry Pi

8109 Views, 47 Favorites, 0 Comments

DIY a Raspberry Game - 2048

632598596831531242.jpg
2048
IMG_0766.JPG

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

What Do We Need

1.png
2.png
3.png
4.png
5.png
6.png

Hardware:

Structure:

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

Wiring

7.jpg
8.png

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.

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 click here to learn how to get started the GrovePi+.

Setup the Pi Camera

9.png

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.

Setup the Photo Printer

10.png
11.png
12.png
13.png
14.png
15.png

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 click here.

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.

Install Pyside and Download the Project Code

16.png

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.

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.

Laser Cut the Plate

156647026676560439.jpg
1.png
2.png
3.png

Download the file and laser cut it.

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

Downloads

Install the Screen Section

490948301651894743.jpg
187967066978132938.jpg
558545294321177593.jpg

Installation screen part, simple structure, fixed with screws

Install the Hardware

88199948160600447.jpg
610158172556440592.jpg

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

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

Install the Housing

318911085090811253.jpg
670797303439292281.jpg
759698695842401204.jpg
397320874991740410.jpg

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!

Install the Camera and Top Panel

IMG_0784.JPG
IMG_0774.JPG
805406815702877949.jpg

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

The Result

IMG_0786.JPG
596095351235508109.jpg

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!