|
| 1 | +""" |
| 2 | +LED Hunt |
| 3 | +
|
| 4 | +Author: Piero Cornice |
| 5 | +
|
| 6 | +- Capture the faint LED with the bright one by tilting the device. |
| 7 | +- Do it 10 times to win the game. |
| 8 | +- See how quick you've been, to compete with friends. |
| 9 | +- Press either A or B to reset the game. |
| 10 | +""" |
| 11 | + |
| 12 | +from microbit import * |
| 13 | +from random import randint |
| 14 | +import music |
| 15 | +import time |
| 16 | + |
| 17 | + |
| 18 | +def normalize(value, input_range, output_range) -> float: |
| 19 | + ''' |
| 20 | + Rescale x from its min/max range to a target range [a, b] using the formula: |
| 21 | +
|
| 22 | + a + (x - min(x)) * (b - a) / (max(x) - min(x)) |
| 23 | + ''' |
| 24 | + return output_range[0] + ((value - input_range[0]) * (output_range[1] - output_range[0])) / (input_range[0] - input_range[1]) |
| 25 | + |
| 26 | +def animate(images, delay_ms=50): |
| 27 | + for img in images: |
| 28 | + display.show(Image(img)) |
| 29 | + sleep(delay_ms) |
| 30 | + display.clear() |
| 31 | + |
| 32 | + |
| 33 | +def initialize_game(): |
| 34 | + global score, x_target, y_target, start_at |
| 35 | + music.play(music.JUMP_UP, wait=False) |
| 36 | + animate(animation_start) |
| 37 | + score = 0 |
| 38 | + x_target, y_target = set_target() |
| 39 | + start_at = time.ticks_ms() |
| 40 | + |
| 41 | + |
| 42 | +def set_target(): |
| 43 | + x_target, y_target = 2, 2 |
| 44 | + # make sure the target is not in the center, as we'll probably start from there |
| 45 | + while x_target == 2 and y_target == 2: |
| 46 | + x_target = randint(0, 4) |
| 47 | + y_target = randint(0, 4) |
| 48 | + return x_target, y_target |
| 49 | + |
| 50 | + |
| 51 | +animation_win = [ |
| 52 | + '00000:00600:06960:00600:00000', |
| 53 | + '00600:06960:69696:06960:00600', |
| 54 | + '06960:69696:96069:69696:06960', |
| 55 | + '69696:96069:60006:96069:69696', |
| 56 | + '96069:60006:00000:60006:96069', |
| 57 | + '60006:00000:00000:00000:60006'] |
| 58 | + |
| 59 | +animation_start = list(reversed(animation_win)) |
| 60 | + |
| 61 | +# Initialize game variables with dummy values |
| 62 | +# and let initialize_game() do the rest. |
| 63 | +score, x_target, y_target, start_at = 0, 0, 0, 0 |
| 64 | +initialize_game() |
| 65 | + |
| 66 | +while True: |
| 67 | + if button_a.was_pressed() or button_b.was_pressed(): |
| 68 | + initialize_game() |
| 69 | + |
| 70 | + x_acc, y_acc, _ = accelerometer.get_values() |
| 71 | + |
| 72 | + x_acc = min(max(x_acc, -500), 500) |
| 73 | + y_acc = min(max(y_acc, -500), 500) |
| 74 | + |
| 75 | + x_pos = int(abs(normalize(x_acc, [-500, 500], [0, 4]))) |
| 76 | + y_pos = int(abs(normalize(y_acc, [-500, 500], [0, 4]))) |
| 77 | + |
| 78 | + #print('X: ', x_acc, ' => ', x_pos) |
| 79 | + #print('Y: ', y_acc, ' => ', y_pos) |
| 80 | + |
| 81 | + if x_pos == x_target and y_pos == y_target: |
| 82 | + # Win! Set a new target |
| 83 | + music.play(music.BA_DING, wait=False) |
| 84 | + score += 1 |
| 85 | + display.show(score) |
| 86 | + sleep(1000) |
| 87 | + |
| 88 | + if score <= 9: |
| 89 | + # End of match: set a new target |
| 90 | + animate(animation_win) |
| 91 | + x_target, y_target = set_target() |
| 92 | + else: |
| 93 | + # End of game |
| 94 | + # Show the elapsed time and restart the game |
| 95 | + end_at = time.ticks_ms() |
| 96 | + music.play(music.POWER_UP, wait=False) |
| 97 | + for _ in range(3): |
| 98 | + animate(animation_win) |
| 99 | + elapsed_time = time.ticks_diff(end_at, start_at) // 1000 |
| 100 | + display.scroll(str(elapsed_time) + ' sec', delay=100) |
| 101 | + initialize_game() |
| 102 | + |
| 103 | + # Update the screen |
| 104 | + display.clear() |
| 105 | + display.set_pixel(x_target, y_target, 5) |
| 106 | + display.set_pixel(x_pos, y_pos, 9) |
| 107 | + sleep(100) |
| 108 | + |
0 commit comments