### Description: Leiden Tech
### Category: Games
### License: MIT
### Appname: Dice
### Built-in: no

import display
import buttons
import machine
import math
import random
import mch22

bgcolor = display.WHITE
fgcolor = display.BLACK
font = "PermanentMarker22"
numOfDie = 1
cheatOn = False

def on_action_btn_a(pressed):
  if pressed:
    drawDice()

def on_action_btn_b(pressed):
  if pressed:
    drawDice()

def on_action_btn_up(pressed):
  global cheatOn
  if pressed:
    cheatOn = True

def on_action_btn_down(pressed):
  global cheatOn
  if pressed:
    cheatOn = False

def on_action_btn_left(pressed):
  global numOfDie, pipRad, pipDim, gyoffset, gxoffset
  if pressed:
    if (numOfDie > 1):
      numOfDie = numOfDie - 1
      resize(numOfDie)
    drawDice()

def on_action_btn_right(pressed):
  global numOfDie, pipRad, pipDim, gyoffset, gxoffset
  if pressed:
    if (numOfDie < 5):
      numOfDie = numOfDie + 1
      resize(numOfDie)
    drawDice()

def on_action_btn_start(pressed):
  if pressed:
    mch22.exit_python()

def on_action_btn_select(pressed):
  if pressed:
    global font, bgcolor, fgcolor
    clearbg()
    display.string(50, 50, "Restarting", fgcolor, font)
    display.flush()

def drawPip(pipNum, dieNum):
# 1 2 3
# 4 5 6
# 7 8 9
  global fgcolor, gxoffset, gyoffset,  pipRad, pipDim, xoffset
  row = math.floor( (pipNum - 1) / 3 ) + 1
  col = ((pipNum - 1) % 3) + 1
  x = col * 2
  y = row * 2
  dieWidth = pipDim * 7
  display.drawCircle(x * pipDim + xoffset - pipRad, y * pipDim + gyoffset - pipRad, pipRad, 0, 360, True, fgcolor)
def drawDice():
  global fgcolor, gxoffset, gyoffset, pipRad, pipDim, numOfDie, cheatOn, xoffset
  clearbg()
  for i in range(numOfDie):
    dieNum = i + 1
    dieWidth = pipDim * 7
    xoffset = gxoffset + (dieWidth * (dieNum - 1)) + (i * 2)
    if i is 0 or cheatOn is False:
      value = random.randint(1,6)
    if (value == 6):#6
      drawPip(2, dieNum)
      drawPip(8, dieNum)
    if (value > 1): #2 3 4 5 6
      drawPip(3, dieNum)
      drawPip(7, dieNum)
    if (value > 3): #4 5 6
      drawPip(1, dieNum)
      drawPip(9, dieNum)
    if (value % 2 != 0 ): #1 3 5
      drawPip(5, dieNum)
    display.drawLine(xoffset,gyoffset,xoffset,dieWidth+gyoffset,fgcolor)
    display.drawLine(xoffset,dieWidth+gyoffset,dieWidth+xoffset,dieWidth+gyoffset,fgcolor)
    display.drawLine(dieWidth+xoffset,dieWidth+gyoffset,dieWidth+xoffset,gyoffset,fgcolor)
    display.drawLine(dieWidth+xoffset,gyoffset,xoffset,gyoffset,fgcolor)
  display.flush()

def resize(numOfDie):
  global font, pipRad, pipDim, gyoffset, gxoffset
  pipRad = 9 - numOfDie
  pipDim = pipRad * 2
  gyoffset = 5 * numOfDie
  gxoffset = math.floor(296 / 2 - ( ( pipDim * 7 * numOfDie ) / 2 )) - pipRad

def quit():
  global font, bgcolor, fgcolor
  clearbg()
  display.string(50, 50, "Quitting", fgcolor, font)
  display.flush()
  mch22.exit_python()


def clearbg():
  global font, bgcolor, fgcolor
  display.drawFill(bgcolor)
  display.flush()

########
# MAIN #
########
[year, month, mday, wday, hour, minute, second, microseconds] = machine.RTC().datetime()
random.seed(int(microseconds))

buttons.attach(buttons.BTN_A, on_action_btn_a)
buttons.attach(buttons.BTN_B, on_action_btn_b)
buttons.attach(buttons.BTN_LEFT, on_action_btn_left)
buttons.attach(buttons.BTN_RIGHT, on_action_btn_right)
buttons.attach(buttons.BTN_UP, on_action_btn_up)
buttons.attach(buttons.BTN_DOWN, on_action_btn_down)
buttons.attach(buttons.BTN_SELECT, on_action_btn_select)
buttons.attach(buttons.BTN_START, on_action_btn_start)

resize(numOfDie)
drawDice()