Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cminter committed Aug 31, 2020
1 parent 53452a8 commit 8ef0270
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
30 changes: 18 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
mode = 0

def fill1(mode, brightness=255):
def xPlot(x:number, y:number, b:number=255, d:number=0):
col, row = [[x, y], [4-y, x], [4-x, 4-y], [y, 4-x]][d]
led.plot_brightness(col, row, b)

def wipe1(direction, brightness=255):
for i in range(5):
for j in range(5):
(row, col) = (i, j) if mode == 0 else (j, i)
led.plot_brightness(col, row, brightness)
xPlot(j, i, b=brightness, d=direction)
basic.pause(50)

def spiral1(brightness=255):
def spiral1(brightness:number=255):
for i in range(3):
led.plot_brightness(i, 0, brightness)
led.plot_brightness(4, i, brightness)
Expand All @@ -29,25 +32,28 @@ def spiral1(brightness=255):
led.plot_brightness(2, 2, brightness)
basic.pause(100)

def oppose1(mode, brightness=255):
def oppose1(direction, brightness=255):
for i in range(5):
for jEven in [0, 2, 4]:
(row, col) = (i, jEven) if mode == 0 else (jEven, i)
led.plot_brightness(col, row, brightness)
xPlot(jEven, i, b=brightness, d=direction)
for jOdd in [1, 3]:
(row, col) = (4-i, jOdd) if mode == 0 else (jOdd, 4-i)
led.plot_brightness(col, row, brightness)
xPlot(jOdd, 4 - i, b=brightness, d=direction)
basic.pause(100)

def on_forever():
# fill then clear
for brightness in [255, 0]:
if mode == 0: fill1(randint(0, 1), brightness)
elif mode == 1: spiral1(brightness)
elif mode == 2: oppose1(randint(0, 1), brightness)
direction = randint(0, 3)
# based on mode
if mode == 0: wipe1(direction, brightness)
elif mode == 1: oppose1(direction, brightness)
elif mode == 2: spiral1(brightness)

def on_button_pressed_a():
global mode
# cycle through modes
mode += 1
if mode > 2: mode = 0

input.on_button_pressed(Button.A, on_button_pressed_a)
basic.forever(on_forever)
29 changes: 18 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
let mode = 0
function fill1(mode: number, brightness: number = 255) {
function xPlot(x: number, y: number, b: number = 255, d: number = 0) {
let [col, row] = [[x, y], [4 - y, x], [4 - x, 4 - y], [y, 4 - x]][d]
led.plotBrightness(col, row, b)
}

function wipe1(direction: number, brightness: number = 255) {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
let [row, col] = mode == 0 ? [i, j] : [j, i]
led.plotBrightness(col, row, brightness)
xPlot(j, i, brightness, direction)
basic.pause(50)
}
}
Expand Down Expand Up @@ -36,36 +40,39 @@ function spiral1(brightness: number = 255) {
basic.pause(100)
}

function oppose1(mode: number, brightness: number = 255) {
function oppose1(direction: number, brightness: number = 255) {
for (let i = 0; i < 5; i++) {
for (let jEven of [0, 2, 4]) {
let [row, col] = mode == 0 ? [i, jEven] : [jEven, i]
led.plotBrightness(col, row, brightness)
xPlot(jEven, i, brightness, direction)
}
for (let jOdd of [1, 3]) {
let [row, col] = mode == 0 ? [4 - i, jOdd] : [jOdd, 4 - i]
led.plotBrightness(col, row, brightness)
xPlot(jOdd, 4 - i, brightness, direction)
}
basic.pause(100)
}
}

input.onButtonPressed(Button.A, function on_button_pressed_a() {

// cycle through modes
mode += 1
if (mode > 2) {
mode = 0
}

})
basic.forever(function on_forever() {
let direction: number;
// fill then clear
for (let brightness of [255, 0]) {
direction = randint(0, 3)
// based on mode
if (mode == 0) {
fill1(randint(0, 1), brightness)
wipe1(direction, brightness)
} else if (mode == 1) {
spiral1(brightness)
oppose1(direction, brightness)
} else if (mode == 2) {
oppose1(randint(0, 1), brightness)
spiral1(brightness)
}

}
Expand Down

0 comments on commit 8ef0270

Please sign in to comment.