# Circuit Playground TFT Gizmo Dreidel

## Overview

https://youtu.be/wWG1xdM45QI

Dreidel, dreidel, dreidel, I made you out of...circuits and code? Well, in this guide we will. This guide will show you how to use a [Circuit Playground Bluefruit](https://www.adafruit.com/product/4333) along with a [TFT Gizmo](https://www.adafruit.com/product/4367) to create a digital cyber dreidel.

Instead of spinning, you'll give your cyber dreidel a shake to set it into action. It will replicate the actual spinning top on the TFT Gizmo display. It even plays the catchy little [Dreidel Song](https://www.wemakethemusic.org/blog/2015/12/12/dreidel-song) while the display is "spinning". And then you get a random symbol to represent the dreidel landing on a side.

And...it's all programmed in [CircuitPython](https://circuitpython.org/). Let's get started.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/795/medium800/graphic_tfts_banner.jpg?1576190902 Adiel lo [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)])

### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
![shot of a Black woman's neon-green manicured hand holding up a Circuit Playground Bluefruit glowing rainbow LEDs.](https://cdn-shop.adafruit.com/640x480/4333-11.jpg)

### Circuit Playground TFT Gizmo - Bolt-on Display + Audio Amplifier

[Circuit Playground TFT Gizmo - Bolt-on Display + Audio Amplifier](https://www.adafruit.com/product/4367)
Extend and expand your Circuit Playground projects with a bolt on TFT Gizmo that lets you add a lovely color display in a sturdy and reliable fashion. This PCB looks just like a round TFT breakout but has permanently affixed M3 standoffs that act as mechanical and electrical...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4367)
[Related Guides to the Product](https://learn.adafruit.com/products/4367/guides)
![Hand pressing buttons on circuit playground, then turning over to show TFT gizmo display an image of a friendly robot or snake](https://cdn-shop.adafruit.com/product-videos/640x480/4367-05.jpg)

# Circuit Playground TFT Gizmo Dreidel

## The Dreidel Game

First, a bit of background on the game itself. The [rules of the dreidel game](https://en.wikipedia.org/wiki/Dreidel) are simple. You can play with any set of items for the pieces. It doesn't have to be money. It could be candy!

Everybody starts with the same number of pieces. Then, everybody puts one piece into the pot. Also, anytime there is one or zero pieces in the pot, everybody adds one piece.

Then, everybody takes turns spinning the dreidel. After the dreidel stops spinning and falls, one side will be facing up. This will decide what action you take as follows:

- Nun ("nothing") - Do nothing

![graphic_tfts_nun.png](https://cdn-learn.adafruit.com/assets/assets/000/085/754/medium640/graphic_tfts_nun.png?1576173975)

- Gimel ("Gimme") - Take everything in the pot.

![graphic_tfts_gimel.png](https://cdn-learn.adafruit.com/assets/assets/000/085/752/medium640/graphic_tfts_gimel.png?1576173941)

- Hay ("half") - Take half of the pot.

![graphic_tfts_hay.png](https://cdn-learn.adafruit.com/assets/assets/000/085/753/medium640/graphic_tfts_hay.png?1576173961)

- Shin ("put in") - Put one piece in the pot.

![graphic_tfts_shin.png](https://cdn-learn.adafruit.com/assets/assets/000/085/755/medium640/graphic_tfts_shin.png?1576173994)

When a player runs out of pieces, they are out of the game.

And that's it. Keep playing until someone walks away with a lot of candy. :)

# Circuit Playground TFT Gizmo Dreidel

## Dreidel Code

## CircuitPython and Library Setup

The dreidel code is written in [CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython). So, first make sure you have the latest firmware installed, which can be downloaded from the CircuitPython website here:

[Circuit Playground Bluefruit Firmware](https://circuitpython.org/board/circuitplayground_bluefruit/)
Then make sure you have the following libraries installed. These should go in your **CIRCUITPY/lib** folder:

![](https://cdn-learn.adafruit.com/assets/assets/000/085/771/medium800/graphic_tfts_lib_folder.png?1576177507)

You can download the library bundle from here:

[CircuitPython Libraries](https://circuitpython.org/libraries)
## Bitmap Image Files

Next, you'll need these two BMP files, which contain the dreidel background image and symbols. Put these in your root **CIRCUITPY** folder (same place the .py code will go):

[dreidel_background.bmp](https://cdn-learn.adafruit.com/assets/assets/000/085/773/original/dreidel_background.bmp?1576177548)
[dreidel_sheet.bmp](https://cdn-learn.adafruit.com/assets/assets/000/085/772/original/dreidel_sheet.bmp?1576177535)
## Dreidel Code

And here's the code for the dreidel game. To have this run automatically, save it as **code.py** in your **CIRCUITPY** folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/TFT_Gizmo_Dreidel/code.py

# Circuit Playground TFT Gizmo Dreidel

## How It Works

Here's a breakdown of how the dreidel code works. The use of a sprite sheet along with group scaling for the symbols is probably the most interesting from a displayio aspect. But we'll go over all the code.

## Dreidel Background

First, a static background image is used to represent the dreidel itself. It looks like this full size:

![](https://cdn-learn.adafruit.com/assets/assets/000/085/768/medium800/graphic_tfts_dreidel.jpg?1576175202)

Note it is 240x240 pixels, since that's the size of the TFT Gizmo display.

Then, we define a face area where the symbols will go: The area is 120x120 pixels and is located at x=60, y=70.

![graphic_tfts_dreidel2.png](https://cdn-learn.adafruit.com/assets/assets/000/085/767/medium640/graphic_tfts_dreidel2.png?1576193172)

There's nothing needed in code for this definition. This is just a convention that will be used for setting up the symbols.

## Dreidel Symbols

Now for the fun part. There are four symbols to deal with. We could have four separate 120x120 pixel images for each. But then we have all those separate files to keep track of and deal with. A better approach is to use the idea of a [sprite sheet](https://learn.adafruit.com/circuitpython-display-support-using-displayio/sprite-sheet). This will let us have only a single file to deal with. The sprite sheet will contain the four symbols and each symbol will be a separate sprite.

That's the approach taken here. The dreidel symbol sprite sheet looks like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/085/762/medium800/graphic_tfts_dreidel_sheet1.png?1576174945)

Further, the dreidel symbols are fairly simple graphics-wise. Our target size is 120x120 pixels, but we really don't need to define the symbols at that resolution. We can get away with defining them at half that, so 60x60 pixels, and then use the [scale feature of the displayio Group](https://circuitpython.readthedocs.io/en/latest/shared-bindings/displayio/Group.html) to have them render at the desired 120x120 pixels, by setting `scale=2`.

So our actual sprite sheet will look like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/085/763/medium800/graphic_tfts_dreidel_sheet2.png?1576174955)

Each of the four symbols is only 60x60 pixels. The overall size of the entire sprite sheet ends up being 120x120 pixels. Here's what it looks like full size:

![](https://cdn-learn.adafruit.com/assets/assets/000/085/765/medium800/graphic_tfts_dreidel_sheet.jpg?1576175076)

The yellow background is used so we can easily find that when the image is loaded and set that palette index to be transparent.

## Game Logic and Melody

The melody definition is pretty straight forward. Just define the frequency and duration for each note. Then loop through each of those and use `play_tone()` to play them.

But how do we play the melody while also updating the symbols on the display? Well, it turns out we can get away with something very simple. Since we went to the trouble to define the symbols very small and then scale them at render time, they load pretty darn fast. So fast in fact that we can just add a call to reload a different symbol within the loop that plays the melody. The display updates without noticeably slowing down the melody playback. Neat!

The whole thing is kicked off by waiting for the board to be shaken. **So, shake to "spin" the dreidel.** Then, after the melody, a random symbol is shown. And then it just loops back to the start and waits for a shake again.


## Featured Products

### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
### Circuit Playground TFT Gizmo - Bolt-on Display + Audio Amplifier

[Circuit Playground TFT Gizmo - Bolt-on Display + Audio Amplifier](https://www.adafruit.com/product/4367)
Extend and expand your Circuit Playground projects with a bolt on TFT Gizmo that lets you add a lovely color display in a sturdy and reliable fashion. This PCB looks just like a round TFT breakout but has permanently affixed M3 standoffs that act as mechanical and electrical...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4367)
[Related Guides to the Product](https://learn.adafruit.com/products/4367/guides)
### Adafruit Circuit Playground Bluefruit Express Starter Kit

[Adafruit Circuit Playground Bluefruit Express Starter Kit](https://www.adafruit.com/product/4504)
If you missed out on ADABOX 014, its not too late for you to pick up the parts necessary to build many of the projects! This kit pack doesn't come with tissue paper or the nifty extras, but it does have all the electronic goodies you need  
  
This project pack features...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4504)
[Related Guides to the Product](https://learn.adafruit.com/products/4504/guides)

## Related Guides

- [Adafruit Circuit Playground Bluefruit](https://learn.adafruit.com/adafruit-circuit-playground-bluefruit.md)
- [Adafruit Circuit Playground TFT Gizmo](https://learn.adafruit.com/adafruit-tft-gizmo.md)
- [Wirelessly Code your Bluetooth Device with CircuitPython](https://learn.adafruit.com/wirelessly-code-your-bluetooth-device-with-circuitpython.md)
- [How to use the Puppet Module in the Bluefruit Playground App](https://learn.adafruit.com/how-to-use-the-puppet-module-in-the-bluefruit-playground-app.md)
- [Cyberpunk Santa Eye](https://learn.adafruit.com/cyberpunk-santa-eye.md)
- [PyLeap WAV Playback for Circuit Playground Bluefruit](https://learn.adafruit.com/pyleap-wav-playback-for-cpb.md)
- [AdaBox 014](https://learn.adafruit.com/adabox014.md)
- [Neopixel Crystal Chandelier with CircuitPython Animations and Speed Control](https://learn.adafruit.com/neopixel-crystal-chandelier-with-circuitpython-animations-and-speed-control.md)
- [TFT Gizmo Animated Eye](https://learn.adafruit.com/tft-gizmo-animated-eye.md)
- [LED Emerald with Circuit Playground Bluefruit](https://learn.adafruit.com/led-emerald-with-circuit-playground.md)
- [Sailor Moon Star Locket](https://learn.adafruit.com/sailor-moon-star-locket.md)
- [PyLeap device enabled - In Rainbows](https://learn.adafruit.com/pyleap-device-enabled-in-rainbows.md)
- [BLE Volume Knob with CircuitPython](https://learn.adafruit.com/bluetooth-le-hid-volume-knob-with-circuitpython.md)
- [PyLeap Tone Piano for Circuit Playground Bluefruit](https://learn.adafruit.com/pyleap-tone-piano-for-cpb.md)
- [Which CircuitPython Board is Right for You?](https://learn.adafruit.com/choose-your-circuitpython-board.md)
- [CircuitPython Essentials](https://learn.adafruit.com/circuitpython-essentials.md)
