Last active
October 22, 2019 17:26
-
-
Save eighthree/e63c2ae5de172806abcb9b4b9c9acb03 to your computer and use it in GitHub Desktop.
cp square 3D
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
import time | |
import analogio | |
import digitalio | |
import board | |
import gamepadshift | |
import displayio | |
import math | |
import random | |
from adafruit_display_shapes.circle import Circle | |
# init | |
last_tick = time.monotonic() | |
scale = 9 | |
speed = 0 | |
# Create the display | |
display = board.DISPLAY | |
w = display.width | |
h = display.height | |
# Create the display context | |
cube3d = displayio.Group(max_size=100) | |
cx = w//2 | |
cy = h//2 | |
verts = (-1,-1,-1),(1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,1),(1,-1,1),(1,1,1),(-1,1,1) | |
edges = (0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7) | |
# paletted | |
palette = displayio.Palette(4) | |
palette[0] = 0x000000 | |
palette[1] = 0x555555 | |
palette[2] = 0x00ff00 | |
palette[3] = 0xffffff | |
class Cam: | |
def __init__(self,pos=(0,0,0),rot=(0,0)): | |
self.pos = list(pos) | |
self.rot = list(rot) | |
def update(self,dt,key): | |
s=dt*20 | |
self.pos[1]-=s | |
cam = Cam((0,0,20)) | |
cube = displayio.Bitmap(w, h, 3) | |
root = displayio.TileGrid(cube, pixel_shader=palette) | |
def sprite(): | |
face = 0 | |
for edge in edges: | |
points = [] | |
for x,y,z in (verts[edge[0]],verts[edge[1]]): | |
x -= cam.pos[0] | |
y -= cam.pos[1] | |
z -= cam.pos[2] | |
x,z = rotate2d((x,z),cam.rot[1]) | |
y,z = rotate2d((y,z),cam.rot[0]) | |
f = 200/z | |
x *= f | |
y *= f | |
points+=[(cx+int(x),cy+int(y))] | |
if face > 3 and face < 8: | |
line(cube, points[1][0],points[1][1],points[0][0],points[0][1],3) | |
else: | |
line(cube, points[1][0],points[1][1],points[0][0],points[0][1],1) | |
if face >= 10: | |
face = 0 | |
else: | |
face += 1 | |
def bsprite(): | |
for edge in edges: | |
points = [] | |
for x,y,z in (verts[edge[0]],verts[edge[1]]): | |
x -= cam.pos[0] | |
y -= cam.pos[1] | |
z -= cam.pos[2] | |
x,z = rotate2d((x,z),cam.rot[1]) | |
y,z = rotate2d((y,z),cam.rot[0]) | |
f = 200/z | |
x *= f | |
y *= f | |
points+=[(cx+int(x),cy+int(y))] | |
line(cube, points[1][0],points[1][1],points[0][0],points[0][1],0) | |
def rotate2d(pos,rad): x,y=pos; s,c = math.sin(rad),math.cos(rad); return x*c-y*s,y*c+x*s | |
def tick(fps): | |
global last_tick | |
last_tick += 1 / fps | |
wait = max(0, last_tick - time.monotonic()) | |
if wait: | |
time.sleep(wait) | |
else: | |
last_tick = time.monotonic() | |
class PyGamerButtons: | |
K_X = 0x01 | |
K_O = 0x02 | |
K_START = 0x04 | |
K_SELECT = 0x08 | |
K_DOWN = 0x10 | |
K_LEFT = 0x20 | |
K_RIGHT = 0x40 | |
K_UP = 0x80 | |
def __init__(self): | |
self.buttons = gamepadshift.GamePadShift( | |
digitalio.DigitalInOut(board.BUTTON_CLOCK), | |
digitalio.DigitalInOut(board.BUTTON_OUT), | |
digitalio.DigitalInOut(board.BUTTON_LATCH), | |
) | |
self.joy_x = analogio.AnalogIn(board.JOYSTICK_Y) | |
self.joy_y = analogio.AnalogIn(board.JOYSTICK_X) | |
def get_pressed(self): | |
pressed = self.buttons.get_pressed() | |
dead = 15000 | |
x = self.joy_x.value - 32767 | |
if x < -dead: | |
pressed |= self.K_LEFT | |
elif x > dead: | |
pressed |= self.K_RIGHT | |
y = self.joy_y.value - 32767 | |
if y < -dead: | |
pressed |= self.K_UP | |
elif y > dead: | |
pressed |= self.K_DOWN | |
return pressed | |
def line(bm, x0, y0, x1, y1, color): | |
global cube | |
if x0 == x1: | |
if y0 > y1: | |
y0, y1 = y1, y0 | |
for _h in range(y0, y1): | |
bm[x0, _h] = color | |
elif y0 == y1: | |
if x0 > x1: | |
x0, x1 = x1, x0 | |
for _w in range(x0, x1): | |
bm[_w, y0] = color | |
else: | |
steep = abs(y1 - y0) > abs(x1 - x0) | |
if steep: | |
x0, y0 = y0, x0 | |
x1, y1 = y1, x1 | |
if x0 > x1: | |
x0, x1 = x1, x0 | |
y0, y1 = y1, y0 | |
dx = x1 - x0 | |
dy = abs(y1 - y0) | |
err = dx / 2 | |
if y0 < y1: | |
ystep = 1 | |
else: | |
ystep = -1 | |
for x in range(x0, x1): | |
if steep: | |
bm[y0, x] = color | |
else: | |
bm[x, y0] = color | |
err -= dy | |
if err < 0: | |
y0 += ystep | |
err += dx | |
display.show(cube3d) | |
cube3d.append(root) | |
frame = 0 | |
randSize = 8 | |
buttons = PyGamerButtons() | |
moveX = 0 | |
moveY = 0 | |
def updateLocation(): | |
global randSize, moveX, moveY | |
keys = buttons.get_pressed() | |
if keys & buttons.K_START: | |
randSize-= 0.1 | |
return True | |
elif keys & buttons.K_SELECT: | |
randSize+= 0.1 | |
return True | |
elif keys & buttons.K_UP: #left/right | |
moveX-= 0.1 | |
return True | |
elif keys & buttons.K_DOWN: | |
moveX+= 0.1 | |
return True | |
elif keys & buttons.K_LEFT: | |
moveY-= 0.1 | |
return True | |
elif keys & buttons.K_RIGHT: | |
moveY+= 0.1 | |
return True | |
while True: | |
updateLocation() | |
if updateLocation(): | |
bsprite() | |
cam = Cam((moveX,moveY,randSize)) | |
frame = (frame + 1) % 8 | |
sprite() | |
tick(24) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment