code.py
  1  # SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  import displayio
  7  from adafruit_circuitplayground import cp
  8  import adafruit_imageload
  9  from adafruit_gizmo import tft_gizmo
 10  from adafruit_display_shapes.circle import Circle
 11  
 12  #  setup for the Gizmo TFT
 13  display = tft_gizmo.TFT_Gizmo()
 14  
 15  #  loading the background image
 16  bg_bitmap, bg_palette = adafruit_imageload.load(
 17      "/clouds_bg.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
 18  )
 19  bg_grid = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette)
 20  
 21  #  loading the crescent moon bitmap sequence
 22  bitmap, palette = adafruit_imageload.load(
 23      "/moon_anime.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
 24  )
 25  #  makes the black background transparent so we only see the cresent moon
 26  palette.make_transparent(0)
 27  
 28  tile_grid = displayio.TileGrid(
 29      bitmap,
 30      pixel_shader=palette,
 31      width=1,
 32      height=1,
 33      tile_height=120,
 34      tile_width=120,
 35      default_tile=0,
 36  )
 37  
 38  #  two circles for the center "jewel"
 39  jewel_outline = Circle(x0=120, y0=120, r=40, fill=0xFBF236)
 40  jewel = Circle(x0=120, y0=120, r=35, fill=0xF70570)
 41  
 42  #  adding the two jewel circle elements to a group
 43  jewel_splash = displayio.Group()
 44  jewel_splash.append(jewel_outline)
 45  jewel_splash.append(jewel)
 46  
 47  #  making a group for the crescent moon sequence
 48  #  scale is 2 because at full 240x240 resolution image is too big
 49  moon_group = displayio.Group(scale=2)
 50  #  group to hold all of the display elements
 51  main_group = displayio.Group()
 52  
 53  #  adding the crescent moon tile grid to the moon group
 54  moon_group.append(tile_grid)
 55  #  adding the background to the main group
 56  main_group.append(bg_grid)
 57  #  adding the moon group to the main group
 58  main_group.append(moon_group)
 59  #  adding the jewel circles to the main group
 60  main_group.append(jewel_splash)
 61  
 62  #  showing the main group on the display
 63  display.show(main_group)
 64  
 65  #  tracks the tilegrid index location for the crescent moon
 66  moon = 0
 67  #  holds time.monotonic()
 68  crescent = 0
 69  #  a button debouncing
 70  a_pressed = False
 71  #  b button debouncing
 72  b_pressed = False
 73  #  tracks if music is playing
 74  music_playing = False
 75  #  tracks if animation is paused
 76  animation_pause = False
 77  
 78  while True:
 79      #  button debouncing
 80      if not cp.button_a and a_pressed:
 81          a_pressed = False
 82      if not cp.button_b and b_pressed:
 83          b_pressed = False
 84      #  runs crescent moon animation
 85      if not music_playing and not animation_pause:
 86          #  every .8 seconds...
 87          if (crescent + 0.8) < time.monotonic():
 88              #  the moon animation cycles
 89              tile_grid[0] = moon
 90              #  moon is the tilegrid index location
 91              moon += 1
 92              #  resets timer
 93              crescent = time.monotonic()
 94              #  resets tilegrid index
 95              if moon > 35:
 96                  moon = 0
 97      #  if music is NOT playing and you press the a button...
 98      if not music_playing and (cp.button_a and not a_pressed):
 99          #  music begins playing and will loop
100          music_playing = True
101          a_pressed = True
102          print("music playing")
103          #  song plays once
104          cp.play_file("moonlight_densetsu.wav")
105          #  music_playing state is updated
106          music_playing = False
107      #  if the animation IS playing and you press the b button...
108      if not animation_pause and (cp.button_b and not b_pressed):
109          #  the animation pauses by updating the animation_pause state
110          animation_pause = True
111          b_pressed = True
112          #  debugging REPL message
113          print("animation paused")
114      #  if the animation is PAUSED and you press the b button...
115      if animation_pause and (cp.button_b and not b_pressed):
116          #  the animation begins again by updating the animation_pause state
117          animation_pause = False
118          b_pressed = True
119          #  debugging REPL message
120          print("animation running again")