Last active
August 12, 2020 07:31
-
-
Save robotman2412/121ebe82a74dbca13d3c5f3d1eddb1d7 to your computer and use it in GitHub Desktop.
Drawing a boat, on a wave: simplified
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 display, math | |
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, y0 = 0, -23 | |
x1, y1 = 3, -23 | |
x2, y2 = 3, 0 | |
x3, y3 = 0, 0 | |
x4, y4 = 12, -10 | |
x5, y5 = 3, -10 | |
x6, y6 = 3, -20 | |
display.drawQuad(x0, y0, x1, y1, x2, y2, x3, y3, mastColor) | |
display.drawTri(x6, y6, x4, y4, x5, y5, flagColor) | |
def drawBoatBottom(color): | |
# 0--------1 | |
# \ / | |
# 3----2 | |
x0, y0 = -20, 0 | |
x1, y1 = 20, 0 | |
x2, y2 = 16, 8 | |
x3, y3 = -16, 8 | |
display.drawQuad(x0, y0, x1, y1, x2, y2, x3, y3, color) | |
def drawSun(color): | |
display.pushMatrix() | |
display.translate(-3, -3 - display.height()) | |
display.drawCircle(0, 0, 30, 0, 360, True, color) | |
display.rotate(sunOffset) | |
for i in range(20): | |
display.rotate(math.pi / 10) | |
display.drawLine(0, 35, 0, 45, color) | |
display.popMatrix() | |
# For good measure. | |
display.clearMatrix() | |
display.translate(0, display.height()) | |
sunOffset = 0 | |
offset = 0 | |
boatX = display.width() / 6 | |
boatAngle = 0 | |
boatY = 0 | |
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() | |
waterLevelBeforeBoat = math.sin((boatX + 2 + offset) * math.pi / 35) * 8 - 35 | |
boatY = math.sin((boatX + offset) * math.pi / 35) * 8 - 35 | |
boatAngle = math.atan2(waterLevelBeforeBoat - boatY, 2) | |
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