Last active
May 29, 2019 08:05
-
-
Save orangedrink/8d774fe36536c4830d1c197d29166dab to your computer and use it in GitHub Desktop.
Demo graphics, buttons/joypad and neopixel funcionality on the Pygamer
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
""" | |
Flappy Box Demo | |
Demo of graphics, buttons/joypad and neopixel funcionality on the Pygamer | |
Joypad to move left and right | |
A for jump | |
B for telepport | |
Select/Start to cycle through box colors | |
Neopixels change color based on box position | |
Thanks to MakerMelissa for the example code for getting button presses at | |
https://github.com/makermelissa/Nametag | |
""" | |
import board | |
import gamepadshift | |
import displayio | |
import digitalio | |
import analogio | |
import time | |
from adafruit_display_shapes.rect import Rect | |
import neopixel | |
import gamepad | |
import random | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 5) | |
# Button Constants | |
BUTTON_LEFT = const(128) | |
BUTTON_UP = const(64) | |
BUTTON_DOWN = const(32) | |
BUTTON_RIGHT = const(16) | |
BUTTON_SEL = const(8) | |
BUTTON_START = const(4) | |
BUTTON_A = const(2) | |
BUTTON_B = const(1) | |
x = 75 | |
y = 75 | |
xv = 0 | |
yv = 0 | |
pad = gamepadshift.GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), | |
digitalio.DigitalInOut(board.BUTTON_OUT), | |
digitalio.DigitalInOut(board.BUTTON_LATCH)) | |
splash = displayio.Group() | |
rect = Rect(x, y, 5, 5, fill=0xFF0000) | |
splash.append(rect) | |
board.DISPLAY.show(splash) | |
xPin = analogio.AnalogIn(board.JOYSTICK_Y) | |
yPin = analogio.AnalogIn(board.JOYSTICK_X) | |
rColors = [ | |
0xFF0000, | |
0x00FF00, | |
0xFF00FF, | |
0x0000FF, | |
0xFFFFFF, | |
] | |
rIndex = 0 | |
def check_buttons(buttons): | |
global x, y, xv, yv, rIndex, rColors | |
if (buttons & BUTTON_SEL) > 0 and rIndex > 0: | |
print('Select') | |
rIndex -= 1 | |
elif (buttons & BUTTON_START) > 0 and rIndex < len(rColors) - 1: | |
print('Start') | |
rIndex += 1 | |
elif (buttons & BUTTON_A) > 0: | |
print('A') | |
yv -= 15 | |
elif (buttons & BUTTON_B) > 0: | |
print('B') | |
x = random.randint(0, 160) | |
y = random.randint(0, 80) | |
# Main Loop | |
current_buttons = pad.get_pressed() | |
last_read = 0 | |
while True: | |
#get joystick input | |
jx = round(((xPin.value / 1000) - 32) / 60) | |
jy = round(((yPin.value / 1000) - 32) / 60) | |
if(abs(xv) < 1): | |
xv += jx | |
xv = xv*.8 | |
yv = yv*.8 | |
yv = yv+.2 | |
x += xv | |
y += yv | |
size = round(y*.3) | |
if(x<0): | |
x=0 | |
xv=0 | |
elif((x+size)>160): | |
x=(155 - size) | |
xv=0 | |
if(y<0): | |
y=0 | |
yv=0 | |
elif((y+size)>125): | |
y=(125-size) | |
yv=0 | |
if(size<10): | |
size = 10 | |
#draw the box | |
rect = Rect(round(x), round(y), size, size, fill=rColors[rIndex]) | |
splash.pop() | |
splash.append(rect) | |
#light up the neopixels | |
mx = round(x) + size | |
b = round(x) + 10 | |
if(b>255): | |
b=255 | |
if(b<0): | |
b=0 | |
for i in range(0, 5): | |
if(mx > i*35): | |
pixels[i] = (b,0,0) | |
else: | |
pixels[i] = (round(b/2),0,round(b/2)) | |
#get button presses | |
if (last_read + 0.03) < time.monotonic(): | |
buttons = pad.get_pressed() | |
last_read = time.monotonic() | |
if current_buttons != buttons: | |
check_buttons(buttons) | |
current_buttons = buttons | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment