本文整理汇总了Python中microbit.sleep函数的典型用法代码示例。如果您正苦于以下问题:Python sleep函数的具体用法?Python sleep怎么用?Python sleep使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sleep函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: receive
def receive():
"""Starts a receiver program"""
radio.on()
channel_idx = 0
while True:
# Handle the display switching
# A for prev channel, B for next channel
channel_selector = button_incdec()
if channel_selector != 0:
# Switch the channel
channel_idx = (channel_idx + channel_selector) % MAX_DISPLAY_IDX
radio.config(channel=BASE_CHANNEL + channel_idx, length=251)
radio.on()
# Give the user some feedback
display.show(str(channel_idx))
sleep(750)
display.clear()
msg = radio.receive()
if msg:
# TODO: validate that we have received a valid frame
try:
display.show(eval(msg))
except Exception as e:
display.show(Image.SAD)
print(repr(e))
示例2: test_sleep
def test_sleep():
# check that sleep works ok with speech because they both use low-level timers
# (this is really a test of the audio module)
print('Testing sleep with speech')
microbit.sleep(1)
speech.say('hello world')
microbit.sleep(1)
示例3: main
def main():
x = 0
y = 0
tick = 0
while True:
tick += 1
if tick == 4:
# walk around, with collision detection
tick = 0
if ac.get_x() > 200 and get_maze(x + 1, y) == 0:
x += 1
elif ac.get_x() < -200 and get_maze(x - 1, y) == 0:
x -= 1
elif ac.get_y() > 200 and get_maze(x, y + 1) == 0:
y += 1
elif ac.get_y() < -200 and get_maze(x, y - 1) == 0:
y -= 1
x = min(15, max(0, x))
y = min(15, max(0, y))
# draw the maze
draw(x, y, tick)
microbit.sleep(50)
示例4: move
def move(self):
#work out where the new segment of the snake will be
newSegment = [self.tail[0][0], self.tail[0][1]]
if self.direction == self.UP:
newSegment[1] -= 1
elif self.direction == self.DOWN:
newSegment[1] += 1
elif self.direction == self.LEFT:
newSegment[0] -= 1
elif self.direction == self.RIGHT:
newSegment[0] += 1
if self.checkCollision(newSegment[0], newSegment[1]):
#game over
snakehead = self.tail[0]
for flashHead in range(0,5):
microbit.display.set_pixel(snakehead[0], snakehead[1], self.SNAKEBRIGHTNESS)
microbit.sleep(200)
microbit.display.set_pixel(snakehead[0], snakehead[1], 0)
microbit.sleep(200)
return False
else:
self.addSegment(newSegment[0], newSegment[1])
#has the snake eaten the apple?
if newSegment[0] == self.apple[0] and newSegment[1] == self.apple[1]:
self.length += 1
self.score += 10
self.createApple()
return True
示例5: detect_motion
def detect_motion():
x = microbit.io.P0.get_digital_value()
y = x
while x == y:
x = y
y = microbit.io.P0.get_digital_value()
microbit.sleep(10)
示例6: play_game
def play_game(delay=100, accelerometer_sensitivity=1/300):
"""Enter game main event loop."""
x, y = 2, 2 # Pixel coordinates, starting in middle of display
winner = None
while winner is None:
if button_a.is_pressed():
x = x + 1
play('A:1')
if button_b.is_pressed():
x = x - 1
play('B:1')
if x > 4:
winner = 'A'
elif x < 0:
winner = 'B'
else:
# No winner - continue
set_pixel(x, y)
# Change row based on accelerometer angle
delta = accelerometer.get_y() * accelerometer_sensitivity
y = max(0, min(4, int(y + delta)))
sleep(delay)
return winner
示例7: updown
def updown():
for p in range(10, 1000, 10):
pitch(p, wait=False)
sleep(10)
for p in range(1000, 10, -10):
pitch(p, wait=False)
sleep(10)
示例8: animate_sin
def animate_sin(operation):
brightness_max = 9
half_brightness_max = brightness_max / 2
two_pi = math.pi * 2
origo_x, origo_y = 2, 2
max_distance = hypot(origo_x, origo_y)
double_max_distance = max_distance * 2
offset = 0
last_t = microbit.running_time()
while True:
for x in range(0, 5):
dist_x = x - origo_x
for y in range(0, 5):
dist_y = y - origo_y
distance = (math.fabs(hypot(dist_x, dist_y)) /
double_max_distance)
distance = (distance + (offset / operation)) % 1
sin = math.sin(distance * two_pi - math.pi)
value = round(sin * half_brightness_max + half_brightness_max)
microbit.display.set_pixel(x, y, value)
t_now = microbit.running_time()
delta_t = t_now - last_t
offset += delta_t * 0.001
last_t = t_now
microbit.sleep(10)
示例9: startGame
def startGame(self):
microbit.display.clear()
self.ox=0
self.score = 0
self.bstates0=self.getbuttons()
playing = True
samples = 0
self.ctr=0
self.move=0
while(playing):
#keep looping around, if the button is pressed, move the snake immediately,
#otherwise move it when the sample time is reached
self.ctr+=1
microbit.sleep(self.SAMPLETIME)
self.updatebuttons()
moved=0
if self.bchange[0]==1:
moved=1
self.ox+=1
elif self.bchange[1]==1:
moved=1
self.ox-=1
samples = samples + 1
if moved or samples >= self.samplespermove:
self.move+=1
if self.move%40 ==0:
self.samplespermove-=1
if self.move %50 ==0:
self.w-=1
if self.w<2:
self.w=2
self.score+=10
self.drawBoard()
#check collision
left,right=self.board[-1]
x=2-self.ox
if x<=left or x>=right:
break
self.moveBoard()
samples = 0
self.drawPlayer()
#if self.ox<0 and self.ctr>100:
# break
#microbit.display.scroll(str(self.ox))
microbit.display.scroll("Score = " + str(self.score), 100)
microbit.display.clear()
示例10: start_countdown
def start_countdown(count=3):
"""Play a countdown animation for the specified number of seconds."""
for i in range(count, 0, -1):
display.show(str(i))
play('C:1')
sleep(1000)
play('A:3')
display.clear()
示例11: forever_four_buttons
def forever_four_buttons():
while True:
four_buttons()
microbit.sleep(10)
# fade all pixels by one brightness level
fade_display()
示例12: animate
def animate(image, delay, *, stride=5, start=-5, wait=True, loop=False):
# TODO: full impl.
for frame in image:
for y in range(frame.height()):
for x in range(frame.width()):
glow = frame.get_pixel(x, y)
set_pixel(x, y, glow)
_microbit.sleep(delay)
示例13: start
def start():
for i in range(5):
sleep(500)
if not (button_a.is_pressed() and button_b.is_pressed()):
return
location = random.choice(LOCATIONS)
radio.send(location)
play(location)
示例14: led_dance
def led_dance(delay):
dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ]
while True:
dots[microbit.random(5)][microbit.random(5)] = 8
for i in range(5):
for j in range(5):
microbit.display.set_pixel(i, j, dots[i][j])
dots[i][j] = max(dots[i][j] - 1, 0)
microbit.sleep(delay)
示例15: test_timing
def test_timing():
# test that speech takes the correct amount of time over many runs
print('Testing timing of say function')
for i in range(5):
start = microbit.running_time()
speech.say('hello world')
microbit.sleep(1)
stop = microbit.running_time()
assert 800 < stop - start < 815