Brad Buskey
Published © GPL3+

Magic 8 Ball

Relive the magic of asking a purple liquid-filled ball to get sarcastic answers to your burning questions.

BeginnerWork in progress801
Magic 8 Ball

Things used in this project

Hardware components

Raspberry Pi 3 Model B+
Raspberry Pi 3 Model B+
×1
Waveshare 2.7" ePaper HAT
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Code

magic8ball.py

Python
This code relys on the drivers from Waveshare. They can be downloaded from here: https://www.waveshare.com/wiki/2.7inch_e-Paper_HAT
# +--------------------------------------------------------+
# | Simple "Magic 8-Ball" Oracle from way back.            |
# | Written by: Brad Buskey                                |
# | Written in: Python 2                                   |
# | Contact: deckyon@gmail.com                             |
# +--------------------------------------------------------+

#! python

import random
import json

# Import the libraries for the Waveshare 2.7" ePaper screen
import epd2in7
import time
from PIL import Image,ImageDraw,ImageFont
import traceback

# Generate a random number from 1 to 20 and pull the correct line from the answer file.
getnum = "answer"+str(random.randint(1,20))
with file('magic8ball.json', 'r') as answerfile:
    answer = json.load(answerfile)

# Time to start creating the output and writing to the screen
try:
    # Set up the driver information.
    epd = epd2in7.EPD()
    epd.init()
    epd.Clear(0xFF)
    # Get the screen and fonts ready for drawing
    Himage = Image.new('1', (epd2in7.EPD_HEIGHT, epd2in7.EPD_WIDTH), 255)  # 255: clear the frame
    draw = ImageDraw.Draw(Himage)

    # I set up more than I needed, but wanted the ability to be able to quickly change font sizes
    font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 18)
    font16 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 16)
    font14 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 14)
    font12 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 12)
    font10 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 10)

    # Draw a simple border around the screen
    draw.rectangle((0, 0, 263, 175), outline = 0)
    draw.rectangle((1, 1, 262, 174), outline = 0)
    draw.rectangle((2, 2, 261, 173), outline = 0)
    draw.rectangle((3, 3, 260, 172), outline = 0)
    draw.rectangle((4, 4, 259, 171), outline = 0)
    draw.rectangle((5, 5, 258, 170), outline = 0)
    draw.rectangle((10, 10, 253, 165), outline = 0)

    # This is the meat of the display - where it prints out the answer
    draw.text((45, 50), "Magic 8 Ball Speaks", font = font18, fill = 0)
    draw.line((45, 73, 210, 73), fill = 0)
    draw.line((45, 73, 210, 73), fill = 0)
    draw.text((65, 76), answer[getnum], font = font14, fill = 0)

    # Display the buffer to the screen and sleep the screen, but not clear it.
    epd.display(epd.getbuffer(Himage))
    epd.sleep()

except:
    print('traceback.format_exc():\n%s' % traceback.format_exc())
    exit()

Credits

Brad Buskey

Brad Buskey

6 projects • 14 followers
Just getting started.. Currently have the Onion Omega 2+, Raspberry Pi 3 B +, Raspberry Pi Zero W. Working in Python.

Comments