Last active
June 23, 2022 19:53
-
-
Save todbot/8b524daba51bd84c92799a2401324521 to your computer and use it in GitHub Desktop.
fakey Almost Asteroids in CircuitPython on FunHouse (ESP32-S2)
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
# staroids_code.py -- fakey Almost Asteroids | |
# 4 Aug 2021 - @todbot | |
# This is just a sketch, the full version of this idea is at https://github.com/todbot/circuitpython_staroids | |
import board, time, math, random | |
import displayio, terminalio, bitmaptools | |
import adafruit_imageload | |
import bitmaptools | |
import digitalio | |
button_L = digitalio.DigitalInOut(board.BUTTON_UP) | |
button_L.switch_to_input(pull=digitalio.Pull.DOWN) | |
button_R = digitalio.DigitalInOut(board.BUTTON_DOWN) | |
button_R.switch_to_input(pull=digitalio.Pull.DOWN) | |
button_F = digitalio.DigitalInOut(board.BUTTON_SELECT) | |
button_F.switch_to_input(pull=digitalio.Pull.DOWN) | |
display = board.DISPLAY | |
display.brightness = 1.0 | |
screen = displayio.Group() # group that holds everything | |
display.show(screen) # add main group to display | |
sprites,sprites_pal = adafruit_imageload.load('staroids-sprites.bmp') | |
sprites_pal.make_transparent(0) | |
shipimg = displayio.Bitmap(30,30, len(sprites_pal)) | |
roid0img = displayio.Bitmap(30,30, len(sprites_pal)) | |
roid1img = displayio.Bitmap(30,30, len(sprites_pal)) | |
## background image, hubble star field | |
# https://www.nasa.gov/feature/goddard/2019/hubble-astronomers-assemble-wide-view-of-the-evolving-universe/ | |
#bgimg, bgpal = adafruit_imageload.load("staroids-sprites.bmp") | |
bgimg, bgpal = adafruit_imageload.load("bg_starfield.bmp") | |
screen.append(displayio.TileGrid(bgimg, pixel_shader=bgpal)) | |
ship = displayio.Group() | |
ship_grid = displayio.TileGrid(shipimg, pixel_shader=sprites_pal) | |
ship.append(ship_grid) | |
screen.append(ship) | |
roid0 = displayio.Group() | |
roid0_grid = displayio.TileGrid(roid0img, pixel_shader=sprites_pal) | |
roid0.append(roid0_grid) | |
screen.append(roid0) | |
roid1 = displayio.Group() | |
roid1_grid = displayio.TileGrid(roid1img, pixel_shader=sprites_pal) | |
roid1.append(roid1_grid) | |
screen.append(roid1) | |
ship_pos = [display.width/2, display.height/2] | |
ship_v = [0.5,0.2] | |
ship_angle = 0.0 | |
roid0_v = [-0.3,-0.2] | |
roid0_pos = [display.width/2, display.height/2] | |
roid0_angle = 0 | |
roid1_v = [0.22,-0.4] | |
roid1_pos = [display.width/2, display.height/2] | |
roid1_angle = 0 | |
print("starting rotating..") | |
last_debug_time = 0 | |
while True: | |
# display ship based on rotation and thrusting or not | |
# bitmaptools.rotozoom(shipimg, shipimg_full, angle=ship_angle, ox=15, oy=15, px=15, py=15) | |
if button_F.value: # thrusting! | |
bitmaptools.rotozoom(shipimg, sprites, angle=ship_angle, ox=15, oy=15, | |
px=45, py=15, source_clip0=(30,0), source_clip1=(59,29)) | |
else: # coasting (doing tilegrid by hand here because seems the only way) | |
bitmaptools.rotozoom(shipimg, sprites, angle=ship_angle, ox=15, oy=15, | |
px=15, py=15, source_clip0=(0,0), source_clip1=(29,29)) | |
# display asteroid | |
bitmaptools.rotozoom(roid0img, sprites, angle=roid0_angle, ox=15, oy=15, | |
px=15, py=45, source_clip0=(0,29), source_clip1=(29,59)) | |
bitmaptools.rotozoom(roid1img, sprites, angle=roid0_angle, ox=15, oy=15, | |
px=45, py=45, source_clip0=(30,29), source_clip1=(59,59)) | |
# check on the user | |
if button_L.value: # rotate left | |
ship_angle = ship_angle - 0.1 | |
if button_R.value: # rotate right | |
ship_angle = ship_angle + 0.1 | |
if button_F.value: # thrust! | |
ship_v[0] = min(2, ship_v[0] + (math.sin(ship_angle)/10) ) | |
ship_v[1] = min(2, ship_v[1] - (math.cos(ship_angle)/10) ) | |
# update ship position | |
ship_pos = (ship_pos[0] + ship_v[0], ship_pos[1] + ship_v[1]) | |
ship.x, ship.y = int(ship_pos[0]) % display.width, int(ship_pos[1]) % display.height | |
# update roids position | |
roid0_angle += 0.01 | |
roid0_pos = (roid0_pos[0] + roid0_v[0], roid0_pos[1] + roid0_v[1]) | |
roid0.x,roid0.y = (int(roid0_pos[0] % display.width), int(roid0_pos[1]) % display.height) | |
roid1_angle += 0.01 | |
roid1_pos = (roid1_pos[0] + roid1_v[0], roid1_pos[1] + roid1_v[1]) | |
roid1.x,roid1.y = (int(roid1_pos[0] % display.width), int(roid1_pos[1]) % display.height) | |
time.sleep(0.015) # sets framerate | |
# debug | |
if time.monotonic() - last_debug_time > 0.1: | |
last_debug_time = time.monotonic() | |
print("ship_pos:", ship.x,ship.y, "ship_v:",ship_v,"angle:",ship_angle) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment