# +--------------------------------------------------------+
# | 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()
Comments
Please log in or sign up to comment.