Skip to content

Commit

Permalink
Scheduler and rain demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Golovkin committed Jul 23, 2016
1 parent 8fd49ac commit dce23ba
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions rain_demo/rain_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# scheduler and accelerometer demoimport randomfrom microbit import *BOARD = {(x, y): 0 for y in range(5) for x in range(5)}POINTS = []class Scheduler: def __init__(self): self.tasks = [] def set_task(self, task, delay=0, repeat=False): cur_time = running_time() self.tasks.append((cur_time + delay, task, delay, repeat)) self.tasks.sort(key=lambda x: x[0], reverse=True) def run(self): while self.tasks: cur_time = running_time() time_to_run, task, delay, repeat = self.tasks[-1] #print (time_to_run, cur_time) if time_to_run <= cur_time: #print (self.tasks) time_to_run, task, delay, repeat = self.tasks.pop() task() if repeat: self.set_task(task, delay, repeat)def show_board(): make_board() for (x, y), value in BOARD.items(): display.set_pixel(x, y, value) def make_board(): global POINTS new_points = [] for i, (x, y, value) in enumerate(POINTS): xi = int(round(x)) yi = int(round(y)) value = int(round(value)) if 0 <= xi <= 4 and 0 <= yi <= 4 and 0 <= value <= 9: BOARD[(xi, yi)] = max(value, BOARD[(xi, yi)]) new_points.append((x, y, value)) POINTS[:] = new_points def set_random_point(): if len(POINTS) < 3: position = random.randint(0, 5 * 5 - 1) y, x = divmod(position, 5) POINTS.append((x, y, 9))#random.randint(5, 9))) def fall(): x_delta = accelerometer.get_x() / 2000 y_delta = accelerometer.get_y() / 2000 for i, (x, y, value) in enumerate(POINTS): #value -= 0.5 x += x_delta y += y_delta POINTS[i] = x, y, value def fade_out(): for (x, y), value in BOARD.items(): if value > 0: BOARD[(x, y)] = value - 1 scheduler = Scheduler()scheduler.set_task(set_random_point, 100, True)scheduler.set_task(fall, 50, True)scheduler.set_task(fade_out, 80, True)scheduler.set_task(show_board, 20, True)scheduler.run()
Expand Down

0 comments on commit dce23ba

Please sign in to comment.