Advertisement
KRITSADA

Python Example for make Class in microbit

Oct 28th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from microbit import *
  2.  
  3. sleep_time = 200
  4. warning_time = 5000
  5.  
  6. class Pinger:
  7.    
  8.   def __init__(self):
  9.     self.direction = 1
  10.     self.x = 2
  11.     self.y = 2
  12.      
  13.   def update(self):
  14.     display.set_pixel(self.x, self.y, 0)
  15.     if self.x >= 4:
  16.         self.x = 4
  17.         self.direction = -self.direction
  18.     elif self.x <= 0:
  19.         self.x = 0
  20.         self.direction = -self.direction
  21.        
  22.     self.x += self.direction
  23.     display.set_pixel(self.x, self.y, 9)
  24.    
  25.    
  26. class DinosaurDetector:
  27.    
  28.   def __init__(self):
  29.     self.horizontal_start = 0
  30.     self.vertical_start = 0
  31.     self.reset()
  32.      
  33.   def dinosaur_detected(self):
  34.     horizontal_now = accelerometer.get_x()
  35.     vertical_now = accelerometer.get_y()
  36.  
  37.     return self.horizontal_start != horizontal_now or self.vertical_start != vertical_now
  38.  
  39.   def reset(self):
  40.     self.horizontal_start = accelerometer.get_x()
  41.     self.vertical_start = accelerometer.get_y()
  42.  
  43.  
  44. display.scroll('Dino Detector', delay=100)
  45.  
  46. detector = DinosaurDetector()
  47. pinger = Pinger()
  48.  
  49. glass1 = Image("00000:00000:00900:00000:00000")
  50. glass2 = Image("00000:00900:09090:00900:00000")
  51. glass3 = Image("09990:90009:90009:90009:09990")
  52. glass4 = Image("09090:90009:00000:90009:09090")
  53. ripple_animation = [glass1, glass2, glass3, glass4, glass3, glass2, glass1]
  54.  
  55. while True:
  56.  
  57.   pinger.update()
  58.   sleep(sleep_time)
  59.    
  60.   if detector.dinosaur_detected():
  61.       detector.reset()
  62.       display.show(ripple_animation, delay=100)
  63.       display.show(Image.GIRAFFE)
  64.       sleep(warning_time)
  65.       display.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement