Created
November 27, 2023 18:33
-
-
Save BenjaminSantiago/60ca7c6e5c1acd0c6e8026cb4222fd9c to your computer and use it in GitHub Desktop.
(This is code for a simple "timing" game" with the CPX, that is heavily annotated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
"TIMING GAME" annotated code | |
(by Benjamin Santiago for Bryan Rendon) | |
DES3090 --> FALL 2023 | |
Hey y y y! So this is code from Fall 2022 (last year), and it was | |
for someone doing a similar idea to show them how the code | |
would be structured since it is kinda, radically different | |
than what we've done in class. | |
Commented extensively below! | |
''' | |
# imports | |
import time | |
from adafruit_circuitplayground import cp | |
import random | |
# variables | |
''' | |
I'd encourage you to play with the numbers here and see | |
how it effects the behavior of the program. | |
''' | |
# ---------------------------------------------- | |
winning_light = random.randint(0,9) | |
#hold frames is a counter, that will be incremented (+1) | |
#and the max value is 5. This is for determining how long | |
#we've held down button A | |
hold_frames = 0 | |
hold_frames_THRESHOLD = 5 | |
#this is for how long the light will wait before going | |
#to the next light. it starts at zero, and is incremented | |
#until it gets to 10. | |
lightWAIT_frames = 0 | |
lightWAIT_max = 20 | |
#this is the current pixel we are displaying (0-10) | |
current_light = 0 | |
#I didn't have the code do anything with this | |
# but a next step would be to make a "game over" animation | |
# and play that when lives are 0. | |
lives = 5 | |
#these are the game modes, | |
#they are just numbers with words attached to them, | |
# they could be anything as long as two modes | |
# don't have the same number | |
INTRO = 0 | |
GAME = 1 | |
mode = INTRO | |
# ---------------------------------------------- | |
# settings | |
cp.pixels.brightness = 0.1 | |
# functions | |
# ---------------------------------------------- | |
# these are just little light animations | |
# we have functions for | |
def anim__HIT(): | |
for bright in range(0, 255, 4): | |
cp.pixels.fill((bright, 0, bright)) | |
time.sleep(0.002) | |
for bright in range(255, 0, -2): | |
cp.pixels.fill((bright, 0, bright)) | |
time.sleep(0.002) | |
def anim__MISS(): | |
cp.pixels.fill((255,0,0)) | |
time.sleep(0.08) | |
cp.pixels.fill((0,0,0)) | |
time.sleep(0.08) | |
cp.pixels.fill((255,0,0)) | |
time.sleep(0.08) | |
cp.pixels.fill((0,0,0)) | |
time.sleep(0.08) | |
# ---------------------------------------------- | |
# MAIN LOOP | |
# ---------------------------------------------- | |
''' | |
The structure of this code is a bit different, the reason | |
being that it needs to not use code like "time.sleep()" | |
for circumstances where timing is important. To do this, | |
it uses A LOT of if statements and variables. | |
The numbers are fairly self explanatory, but basically, | |
the if statements test if a number has reached a certain point | |
rather than "sleep-ing" a certain amount of time. | |
It also uses "modes" so that there is some structure to the | |
game as a whole. | |
''' | |
while True: | |
#INTRO MODE | |
# ---------------------------------------------- | |
# this just loops an animation until someone presses A | |
if mode is INTRO: | |
anim__HIT() | |
if cp.button_a: | |
mode = GAME | |
#GAME MODE | |
# ---------------------------------------------- | |
if mode is GAME: | |
# display lights | |
# light up the "target light" green | |
# (calculated randomly above) | |
cp.pixels[winning_light] = (0, 255, 0) | |
#light up the "current" light white | |
cp.pixels[current_light] = (255, 255, 255) | |
#cycle through the counters | |
#what we want to do here, is wait for 15 "frames" | |
#or times through the loop. | |
#this checks if the amount of frames we've waited, | |
#is less than the maximum (default is 20) | |
# ---------------------------------------------- | |
if lightWAIT_frames < lightWAIT_max: | |
#if it is less than the max, add one | |
lightWAIT_frames = lightWAIT_frames + 1 | |
else: | |
#this code is if we waited 20 frames | |
#turn off the current pixel | |
cp.pixels[current_light] = (0,0,0) | |
#reset counter | |
lightWAIT_frames = 0 | |
#move to next light | |
# we have to make sure we don't go above 10 | |
if current_light + 1 < 10: | |
current_light = current_light + 1 | |
else: | |
current_light = 0 | |
# ---------------------------------------------- | |
# if a button gets pushed! | |
# the below code just increases a counter to determine | |
# how long we've been holding down the button | |
# if we let go, it resets to 0 | |
# ---------------------------------------------- | |
if cp.button_a is True: | |
#start counting hold frames | |
hold_frames = hold_frames + 1 | |
else: | |
#if we press then released, reset the counter | |
if hold_frames > 1: | |
hold_frames = 0 | |
# ---------------------------------------------- | |
#if we successfully pressed the button at the right time | |
# ---------------------------------------------- | |
''' | |
we want to check if where the light is currently is the "winning-light" AND | |
that we've pushed the button, and not simply kept holding down the button | |
(the default wait threshold is 5 frames, see above in variables) | |
''' | |
if current_light == winning_light and cp.button_a is True and hold_frames < hold_frames_THRESHOLD: | |
#display a lil animation | |
anim__HIT() | |
#choose a new random light | |
winning_light = random.randint(0,9) | |
#reset light cycle and counters | |
current_light = 0 | |
lightWAIT_frames = 0 | |
#speed things up for a greater challenge | |
# by decreasing the light wait time | |
if lightWAIT_max > 0: | |
if lightWAIT_max <= 20: | |
lightWAIT_max = lightWAIT_max - 2 | |
else: | |
lightWAIT_max = lightWAIT_max - 20 | |
# if we missed | |
if cp.button_a is True and current_light != winning_light and hold_frames < hold_frames_THRESHOLD: | |
anim__MISS() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment