SHARE
TWEET

radio and class games example

KRITSADA Nov 14th, 2019 156 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. import radio
  4. from microbit import display, Image, button_a, button_b, pin0, running_time
  5.  
  6.  
  7. class Utils:
  8.     @staticmethod
  9.     def deepcopy(arr):
  10.         return [row[:] for row in arr]
  11.  
  12.     @staticmethod
  13.     def build_img(arr):
  14.         result = ""
  15.         for subarr in arr:
  16.             for item in subarr:
  17.                 result += str(item)
  18.             result += ":"
  19.         return Image(result)
  20.  
  21.  
  22. class Player:
  23.     x = 2
  24.     y = 4
  25.     health = 9
  26.     last_changed = running_time()
  27.  
  28.     def __init__(self):
  29.         radio.send('EP {}'.format(self.x))
  30.  
  31.     def allowed_to_move(self):
  32.         return running_time() - self.last_changed > 200
  33.  
  34.     def act(self, frame: Image):
  35.         if self.allowed_to_move():
  36.             if button_a.is_pressed():
  37.                 self.x = max(self.x - 1, 0)
  38.                 radio.send("EP {}".format(self.x))
  39.                 self.last_changed = running_time()
  40.             elif button_b.is_pressed():
  41.                 self.x = min(self.x + 1, 4)
  42.                 radio.send("EP {}".format(self.x))
  43.                 self.last_changed = running_time()
  44.  
  45.         frame.set_pixel(self.x, self.y, self.health)
  46.         return frame
  47.  
  48.  
  49. class RemotePlayer:
  50.     last_position = None
  51.     health = 9
  52.  
  53.     def act(self, frame: Image, msg):
  54.         if msg is not None:
  55.             if 'EP' in msg:
  56.                 try:
  57.                     self.last_position = int(msg.lstrip('EP').strip())
  58.                 except:
  59.                     pass
  60.  
  61.         if self.last_position is not None:
  62.             frame.set_pixel(self.last_position, 0, self.health)
  63.  
  64.         return frame
  65.  
  66.  
  67. class BulletStorm:
  68.     last_changed = running_time()
  69.     last_shot = running_time()
  70.     player_bullet_positions = []
  71.     enemy_bullet_positions = []
  72.  
  73.     def __init__(self, player: Player, remote: RemotePlayer):
  74.         self.player = player
  75.         self.remote = remote
  76.  
  77.     def allowed_to_move(self):
  78.         return running_time() - self.last_changed > 100
  79.  
  80.     def allowed_to_fire(self):
  81.         return running_time() - self.last_shot > 1000
  82.  
  83.     def act(self, frame: Image, msg):
  84.         if self.allowed_to_move():
  85.             for bullet in self.player_bullet_positions:
  86.                 bullet[1] -= 1
  87.                 if bullet[1] == 0 and bullet[0] == self.remote.last_position:
  88.                     self.remote.health -= 1
  89.             for bullet in self.enemy_bullet_positions:
  90.                 bullet[1] += 1
  91.                 if bullet[1] == 4 and bullet[0] == self.player.x:
  92.                     self.player.health -= 1
  93.             self.last_changed = running_time()
  94.  
  95.         self.player_bullet_positions = list(filter(lambda position: position[1] >= 0, self.player_bullet_positions))
  96.         self.enemy_bullet_positions = list(filter(lambda position: position[1] < 5, self.enemy_bullet_positions))
  97.  
  98.         if self.allowed_to_fire():
  99.             radio.send("FR")
  100.             self.player_bullet_positions.append([int(self.player.x), 3])
  101.             self.last_shot = running_time()
  102.  
  103.         if msg is not None and msg == 'FR' and self.remote.last_position is not None:
  104.             self.enemy_bullet_positions.append([int(self.remote.last_position), 1])
  105.  
  106.         for bullet in self.player_bullet_positions + self.enemy_bullet_positions:
  107.             frame.set_pixel(bullet[0], bullet[1], 5)
  108.  
  109.         return frame
  110.  
  111.  
  112. empty_frame = [
  113.     [0] * 5,
  114.     [0] * 5,
  115.     [0] * 5,
  116.     [0] * 5,
  117.     [0] * 5,
  118. ]
  119.  
  120. radio.on()
  121. radio.config(data_rate=radio.RATE_2MBIT, length=8)
  122.  
  123. player = Player()
  124. remote_player = RemotePlayer()
  125. bulletstorm = BulletStorm(player, remote_player)
  126.  
  127. while True:
  128.     message = radio.receive()
  129.     new_frame = Utils.build_img(empty_frame)
  130.     new_frame = player.act(new_frame)
  131.     new_frame = remote_player.act(new_frame, message)
  132.     new_frame = bulletstorm.act(new_frame, message)
  133.  
  134.     if player.health < 1 or message == 'won':
  135.         radio.send('lost')
  136.         display.clear()
  137.         display.scroll('You loose!')
  138.         display.show(Image.SAD)
  139.         break
  140.  
  141.     if remote_player.health < 1 or message == 'lost':
  142.         radio.send('won')
  143.         display.clear()
  144.         display.scroll('You won!')
  145.         display.show(Image.HAPPY)
  146.         break
  147.  
  148.     display.show(new_frame)
  149.  
  150.     if pin0.is_touched():
  151.         display.clear()
  152.         break
  153.  
  154. radio.off()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top