Adafruit_Learning_System_Gu.../CircuitPython_GameAndWatch_Octopus/code.py
2023-11-03 17:53:01 -05:00

80 lines
2.2 KiB
Python

# SPDX-FileCopyrightText: 2022 Tim C, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
import keypad
from displayio import Group
from octopus_game_helpers import OctopusGame
# built-in display
display = board.DISPLAY
# display.brightness = 0.3
# main group that we'll show in the display
main_group = Group()
# create instance of OctopusGame, high score disabled
octopus_game = OctopusGame()
# uncomment this instead, to use NVM highscore
#octopus_game = OctopusGame(high_score_type=OctopusGame.HIGH_SCORE_NVM)
# uncomment this instead, to use SDCard highscore
#octopus_game = OctopusGame(high_score_type=OctopusGame.HIGH_SCORE_SDCARD)
# add octopus game to main group
main_group.append(octopus_game)
# initialize the shiftregister keys to read hardware buttons
buttons = keypad.ShiftRegisterKeys(
clock=board.BUTTON_CLOCK,
data=board.BUTTON_OUT,
latch=board.BUTTON_LATCH,
key_count=4,
value_when_pressed=True,
)
# show the main group on the display
display.root_group = main_group
# main loop
while True:
# get event from hardware buttons
event = buttons.events.get()
# if anything is pressed
if event:
# if the event is for the start button
if event.key_number == 2:
# if it's a pressed event
if event.pressed:
# trigger the right button press action function
octopus_game.right_button_press()
# if the event is for the select button
elif event.key_number == 3:
# if it's a pressed event
if event.pressed:
# trigger the left button press action function
octopus_game.left_button_press()
# if the event is for the b button
elif event.key_number == 0:
# if it's a pressed event
if event.pressed:
# trigger the b button press action function
octopus_game.b_button_press()
# if the event is for the a button
elif event.key_number == 1:
# if it's a pressed event
if event.pressed:
# trigger the a button press action function
octopus_game.a_button_press()
# call the game tick function
octopus_game.tick()