Skip to content

Instantly share code, notes, and snippets.

@robotman2412
Last active July 22, 2020 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotman2412/337720ad9866055448bbb7761f507024 to your computer and use it in GitHub Desktop.
Save robotman2412/337720ad9866055448bbb7761f507024 to your computer and use it in GitHub Desktop.
My matrix multiplication, used to row a boat.
import display, math, time, buttons
def drawBoat():
display.pushMatrix()
drawBoatBottom(0x000000)
display.translate(-4, 0)
drawBoatMast(0x000000, 0x000000)
display.popMatrix()
def drawBoatMast(mastColor, flagColor):
# 0--1
# | |
# | 6
# | |\
# | 5-4
# | |
# 3--2
x0 = 0
y0 = -23
x1 = 3
y1 = -23
x2 = 3
y2 = 0
x3 = 0
y3 = 0
x4 = 12
y4 = -10
x5 = 3
y5 = -10
x6 = 3
y6 = -20
display.drawQuad(x0, y0, x1, y1, x2, y2, x3, y3, mastColor)
# display.drawLine(x0, y0, x1, y1, 0x000000)
# display.drawLine(x1, y1, x2, y2, 0x000000)
# display.drawLine(x2, y2, x3, y3, 0x000000)
# display.drawLine(x3, y3, x0, y0, 0x000000)
display.drawTri(x6, y6, x4, y4, x5, y5, flagColor)
# display.drawLine(x6, y6, x4, y4, 0x000000)
# display.drawLine(x4, y4, x5, y5, 0x000000)
# display.drawLine(x4, y4, x6, y6, 0x000000)
def drawBoatBottom(color):
# 0--------1
# \ /
# 3----2
x0 = -20
y0 = 0
x1 = 20
y1 = 0
x2 = 16
y2 = 8
x3 = -16
y3 = 8
display.drawQuad(x0, y0, x1, y1, x2, y2, x3, y3, color)
# display.drawLine(x0, y0, x1, y1, 0x000000)
# display.drawLine(x1, y1, x2, y2, 0x000000)
# display.drawLine(x2, y2, x3, y3, 0x000000)
# display.drawLine(x3, y3, x0, y0, 0x000000)
def drawSun(color):
display.pushMatrix()
display.translate(-3, -3 - display.height())
display.drawCircle(0, 0, 30, 0, math.pi * 2, True, color)
display.rotate(sunOffset)
for i in range(20):
display.rotate(math.pi / 10)
display.drawLine(0, 35, 0, 45, color)
display.popMatrix()
def btnAEvent(value):
global boatInWater, boatVelocity, boatY
if value and boatInWater and boatVelocity == 0:
boatInWater = False
boatVelocity = -10
boatY -= 8
# For good measure.
display.clearMatrix()
display.translate(0, display.height())
sunOffset = 0
offset = 0
boatX = display.width() / 6
boatAngle = 0
boatY = 0
boatInWater = True
boatVelocity = 0
buttons.attach(buttons.BTN_A, btnAEvent)
while True:
display.drawFill(0xffffff)
drawSun(0x000000)
for i in range(display.width()):
wave = math.sin((i + offset) * math.pi / 35) * 8 - 35
display.drawPixel(i, wave, 0x000000)
if i & 1:
for j in range(round(wave - 1) | 1, 0, 2):
display.drawPixel(i, j + ((i >> 1) & 1) + 1, 0x000000)
display.pushMatrix()
waterLevelAtBoat = math.sin((boatX + offset) * math.pi / 35) * 8 - 35 + boatVelocity
if boatInWater:
if boatY > waterLevelAtBoat and boatVelocity < 0:
boatY += boatVelocity
boatVelocity -= 2
if boatY <= waterLevelAtBoat:
boatY = waterLevelAtBoat
boatVelocity = 0
else:
boatY = waterLevelAtBoat
boatVelocity = 0
waterLevelBeforeBoat = math.sin((boatX + 2 + offset) * math.pi / 35) * 8 - 35 + boatVelocity
boatAngle = math.atan2(waterLevelBeforeBoat - waterLevelAtBoat, 2)
else:
boatY += boatVelocity
boatVelocity = boatVelocity + 3 if boatVelocity < 20 else 20
if boatY >= waterLevelAtBoat and boatVelocity > 0:
boatInWater = True
boatVelocity = -1
boatY = waterLevelAtBoat
display.translate(boatX, boatY - 6)
display.rotate(boatAngle)
drawBoat()
display.popMatrix()
offset += 8
sunOffset += math.pi * 0.025
display.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment