def games_menu(): """ Fonction representant le menu qui contient les applications d'alertes :return: (str) Le nom du state dans lequel le programme doit etre """ game_menu = menu.Menu() game_menu.add_item(menu.MenuItem("ressources/images/snake.png", "snake")) game_menu.add_item(menu.MenuItem("ressources/images/simon.png", "simon")) game_menu.add_item( menu.MenuItem("ressources/images/morpion.png", "morpion")) game_menu.add_item(menu.MenuItem("ressources/images/dice.png", "dice")) game_menu.add_item( menu.MenuItem("ressources/images/nihon.png", "romaji_QCM")) game_menu.show_selected_item_image() while True: direction = userinput.get_joystick_direction() if direction is not None: if direction == "middle": sense.clear() return game_menu.get_selected_item().get_target_state() elif direction == "down": return "home_screen" else: game_menu.navigate(direction)
def tools_menu(): """ Fonction representant le menu qui contient les applications d'alertes :return: (str) Le nom du state dans lequel le programme doit etre """ tools_menu = menu.Menu() tools_menu.add_item( menu.MenuItem("ressources/images/humidity.png", "alert_humidity")) tools_menu.add_item( menu.MenuItem("ressources/images/pressure.png", "alert_pressure")) tools_menu.add_item( menu.MenuItem("ressources/images/thermometre.png", "alert_temperature")) tools_menu.add_item(menu.MenuItem("ressources/images/map.png", "map")) tools_menu.add_item( menu.MenuItem("ressources/images/compass.png", "compass")) tools_menu.show_selected_item_image() while True: direction = userinput.get_joystick_direction() if direction is not None: if direction == "middle": sense.clear() return tools_menu.get_selected_item().get_target_state() elif direction == "down": return "home_screen" else: tools_menu.navigate(direction)
def start_screen(): """ Fonction representant l'ecran de depart :return: (None) """ is_shown = True B = [0, 0, 0] # Black/Nothing i = 0 while is_shown: if i % 10000 == 0: i = 0 C = mathutil.random_color() # C pour couleur du ? logo_pi = [ B, B, B, B, B, B, B, B, B, C, C, C, C, C, C, B, C, B, C, B, B, C, B, B, B, B, C, B, B, C, B, B, B, B, C, B, B, C, B, B, B, B, C, B, B, C, B, C, B, B, C, B, B, C, C, B, B, B, B, B, B, B, B, B ] sense.set_pixels(logo_pi) if userinput.get_joystick_direction() is not None: is_shown = False sense.clear() i += 1
def play(self, player): """ lance le jeu et permet le mouvement du joueur "player" :param player: (int 1 ou 2 ) si 1 = joueur 1 si 2 = joueur 2 :return: None """ color = (0, 0, 0) if player == 1: color = self.p1 elif player == 2: color = self.p2 coo = 1 boo = True self.direction_morpion = "right" while True: while boo or 9 < coo < 1: while 9 < coo < 1: if 9 < coo < 1: if coo < 1: coo += 1 elif coo > 9: coo -= 1 if self.index[coo][2] == 0: boo = False else: if self.first_spawn: if coo > 9: coo += 1 elif coo < 1: coo -= 1 self.first_spawn = False else: coo = self.direction(coo, self.direction_morpion) self.coloration(color, coo) time.sleep(0.5) self.coloration(N, coo) event = userinput.get_joystick_direction() if event is not None: a = self.direction(coo, event) if a != "selected": coo = a boo = True self.direction_morpion = event else: self.coloration(color, coo) if color == self.p1: self.index[coo][2] = 1 return self.score(self.p1_score, coo, self.p1) elif color == self.p2: self.index[coo][2] = 1 return self.score(self.p2_score, coo, self.p2)
def launch(): """ Lance l'application du jet de dé. """ dice = (D1, D2, D3, D4, D5, D6) sense.set_pixels(random.choice(dice)) while True: if userinput.is_shaking(): sense.set_pixels(random.choice(dice)) time.sleep(1) if userinput.get_joystick_direction() is not None: break
def world_map(): image = mathutil.image_to_xy_list( sense.load_image("ressources/images/world_map.png", False)) canvas = Canvas(image) camera = Camera(len(image)) while True: direction = userinput.get_joystick_direction() if direction == "middle": break else: camera.move(direction, 1) camera.set_visible_pixels(canvas.get_pixels(camera.get_position())) camera.show_visible_pixels()
def temperature(): O = [0, 0, 0] # Noir/Vide Y = [255, 165, 0] # Jaune R = [255, 0, 0] # Rouge G = [0, 255, 0] # Vert B = [0, 0, 255] # Bleu warning_sign = [ O, O, O, R, R, O, O, O, O, O, R, O, O, R, O, O, O, O, R, Y, Y, R, O, O, O, R, O, Y, Y, O, R, O, O, R, O, Y, Y, O, R, O, R, O, O, O, O, O, O, R, R, O, O, Y, Y, O, O, R, R, R, R, R, R, R, R, R ] sense = SenseHat() while True: direction = userinput.get_joystick_direction() if direction is not None: break time.sleep(1) # Delai entre chaque display. temp = sense.get_temperature() # Si la temperature est trop elevee, # une alerte est lancee if temp > 40: sense.set_pixels(warning_sign) time.sleep(1) sense.clear() else: if temp > 30: # Chaud color = R elif 20 < temp <= 30: # Tiede color = G else: # Froid color = B temp_str = round(temp) sense.show_message(str(temp_str) + "C", 0.05, color)
def humidity(): X = [0, 0, 0] # Noir/Vide Y = [255, 165, 0] # Jaune R = [255, 0, 0] # Rouge G = [0, 255, 0] # Green O = [255, 127, 80] # Orange warning_sign = [ X, X, X, R, R, X, X, X, X, X, R, X, X, R, X, X, X, X, R, Y, Y, R, X, X, X, R, X, Y, Y, X, R, X, X, R, X, Y, Y, X, R, X, R, X, X, X, X, X, X, R, R, X, X, Y, Y, X, X, R, R, R, R, R, R, R, R, R ] while True: direction = userinput.get_joystick_direction() if direction is not None: break time.sleep(1) # Delai entre chaque display. hum = sense.get_humidity() print("Humidity: %s %%rH" % hum) # Humidite pourrait creer # des virus, des irritations respiratoires # et de l'eczema. if hum < 30: sense.set_pixels(warning_sign) time.sleep(1) sense.clear() else: # Moisissure if hum > 90: color = R # Acariens elif hum > 60 and hum < 90: color = O # Niveau correct d'humidite elif hum > 30 and hum <= 60: color = G hum_str = round(hum) sense.show_message(str(hum_str) + " rH", 0.1, color)
def pressure(): R = [255, 0, 0] # Rouge O = [0, 0, 0] # Noir/Vide Y = [255, 165, 0] # Jaune G = [0, 255, 0] # Vert warning_sign = [ O, O, O, R, R, O, O, O, O, O, R, O, O, R, O, O, O, O, R, Y, Y, R, O, O, O, R, O, Y, Y, O, R, O, O, R, O, Y, Y, O, R, O, R, O, O, O, O, O, O, R, R, O, O, Y, Y, O, O, R, R, R, R, R, R, R, R, R ] while True: direction = userinput.get_joystick_direction() if direction is not None: break time.sleep(1) # Delai entre chaque display. pressure = sense.get_pressure() print("Pressure: %s Millibars" % pressure) # Si la pression est trop elevee, # une alerte est lancee. if pressure < 61.8: sense.set_pixels(warning_sign) time.sleep(1) sense.clear() else: # Pression atmospherique la plus elevee sur Terre. if pressure > 1083.8: color = R else: # Bonne pression de l'air color = G temp_str = round(pressure) sense.show_message(str(temp_str) + "hPa", 0.05, color)
def compass(): sense.set_pixels(boussole) sense.set_pixels(hand) while True: orientation = sense.get_orientation_degrees() yaw = orientation["yaw"] # when yaw is 0 it should indicate the North if yaw < 22.5 or yaw > 337.5: sense.set_pixels(hand_north) # when yaw is 180 it should indicate the South if yaw > 157.5 and yaw < 202.5: sense.set_pixels(hand_south) # when yaw is 90 it should indicate the East if yaw > 67.5 and yaw < 122.5: sense.set_pixels(hand_east) # when yaw is 45 it should indicate the North East if yaw > 22.5 and yaw < 67.5: sense.set_pixels(hand_north_east) # when yaw is 45 it should indicate the South East if yaw > 122.5 and yaw < 157.5: sense.set_pixels(hand_south_east) # when yaw is 270 it should indicate the West if yaw > 247.5 and yaw < 292.5: sense.set_pixels(hand_west) # when yaw is 270 it should indicate the North West if yaw > 292.5 and yaw < 337.5: sense.set_pixels(hand_north_west) # when yaw is 270 it should indicate the South West if yaw > 202.5 and yaw < 247.5: sense.set_pixels(hand_south_west) if userinput.get_joystick_direction() is not None: break
def application_menu(): """ Fonction representant le menu qui contient les applications :return: (str) Le nom du state dans lequel le programme doit etre """ application_menu = menu.Menu() application_menu.add_item(menu.MenuItem("ressources/images/image_gallery.png", "image_gallery")) application_menu.add_item(menu.MenuItem("ressources/images/shutdown.png", "shutdown")) application_menu.add_item(menu.MenuItem("ressources/images/tools.png", "tools_menu")) application_menu.add_item(menu.MenuItem("ressources/images/game.png", "game_menu")) application_menu.show_selected_item_image() while True: direction = userinput.get_joystick_direction() if direction is not None: if direction == "middle": sense.clear() return application_menu.get_selected_item().get_target_state() else: application_menu.navigate(direction)
def run(self): """ lance le snake :return: / """ sense.clear() sense.set_pixel(self.cherry_coo[0], self.cherry_coo[1], 255, 0, 0) while self.work: self.move() time.sleep(self.time) event = userinput.get_joystick_direction() if event is not None: if (self.direction == "right" and event == "left") or \ (self.direction == "left" and event == "right") or \ (self.direction == "up" and event == "down") or \ (self.direction == "down" and event == "up"): pass elif self.direction != "middle": self.direction = event
def run_romaji_qcm(): # Initialisation de variables sense = SenseHat() has_picked_an_answer = False chosen_answers = [] color_of_answers = { "left": [0, 0, 255], "up": [255, 0, 0], "right": [0, 255, 0], "down": [255, 255, 0] } romaji_answers = [{ "left": "ga", "up": "su", "right": "ne", "down": "se" }, { "left": "ja", "up": "mya", "right": "fu", "down": "no" }, { "left": "nama", "up": "sa", "right": "zu", "down": "ro" }, { "left": "mo", "up": "oi", "right": "wa", "down": "e" }, { "left": "ra", "up": "a", "right": "ri", "down": "hi" }] correct_answers = ["su", "no", "nama", "mo", "ri"] def japanese_characters(): """ Cree des images 8x8 noires et blanches de caracteres japonais pour le QCM. :return: (list) Une liste des caracteres japonais qui sont chacun representes par une matrice 8x8 de tuples RGB """ W = (255, 255, 255) # white O = (0, 0, 0) # nothing/black # Les noms de variable sont les romaji du caract?re japonais # ? su = [ O, O, O, O, W, O, O, O, O, O, W, W, W, W, W, O, O, W, O, O, W, O, O, O, O, O, O, W, W, O, O, O, O, O, W, O, W, O, O, O, O, O, O, W, W, O, O, O, O, O, O, O, W, O, O, O, O, O, W, W, O, O, O, O, ] # ? no = [ O, O, O, O, O, O, O, O, O, O, W, W, W, W, O, O, O, W, O, W, O, O, W, O, O, W, O, W, O, O, W, O, O, W, O, W, O, O, W, O, O, W, O, W, O, O, W, O, O, W, W, O, O, O, W, O, O, O, O, O, W, W, O, O, ] # ? nama = [ O, W, O, W, O, O, O, O, O, W, W, W, W, W, W, O, O, W, O, W, O, O, O, O, O, O, O, W, O, O, O, O, O, W, W, W, W, W, W, O, O, O, O, W, O, O, O, O, O, O, O, W, O, O, O, O, W, W, W, W, W, W, W, W, ] # ? mo = [ O, O, W, O, O, O, O, O, O, O, W, O, O, O, O, O, O, W, W, W, W, O, O, O, O, O, W, O, O, O, O, O, O, W, W, W, W, O, W, O, O, O, W, O, O, O, W, O, O, O, W, O, O, O, W, O, O, O, O, W, W, W, O, O ] # ? ri = [ O, O, O, O, O, O, O, O, O, O, O, O, O, W, O, O, O, O, W, O, O, W, O, O, O, O, W, O, O, W, O, O, O, O, O, O, O, W, O, O, O, O, O, O, O, W, O, O, O, O, O, W, W, O, O, O, O, O, O, O, O, O, O, O ] return [su, no, nama, mo, ri] for i in range(len(japanese_characters())): # Affichage du caractere japonais sur l'ecran pdt 2s. sense.set_pixels(japanese_characters()[i]) time.sleep(2) has_picked_an_answer = False while not has_picked_an_answer: # Affichage du menu central invitant l'utilisateur a # naviguer a l'aide du joystick dans une des 4 directions. sense.load_image("ressources/images/central_menu.png") joystick_direction = userinput.get_joystick_direction() if joystick_direction in romaji_answers[i].keys(): # Affichage de la proposition de reponse. sense.show_message(romaji_answers[i][joystick_direction], 0.05, color_of_answers[joystick_direction]) # Selection de cette proposition de reponse. if userinput.get_joystick_direction() == "middle": # Ajout de la reponse choisie dans la liste `chosen_answers`. chosen_answers.append( romaji_answers[i][joystick_direction]) # Feedback visuel comme quoi le choix a bien ete enregistre. sense.load_image("ressources/images/pencil.png") # Ce time.sleep() sert a garder le feedback visuel a l'ecran # pendant plus d'une fraction de seconde. time.sleep(2) # 'Cassage' de la boucle while. has_picked_an_answer = True # Affichage du score final # en comptant le nb de bonnes reponses sense.show_message( "Score: " + str(len(set(chosen_answers).intersection(correct_answers))) + "/" + str(len(japanese_characters())), 0.05)
def home_screen(): """ Fonction representant l'ecran d'accueil :return: None """ is_shown = True G = [40, 198, 65] # Vert B = [0, 0, 0] # Noir/Vide W = [255, 255, 255] # Blanc image_index = 0 images = [[ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, W, W, B, B, G, G, B, B, W, W, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, B, W, W, B, G, G, B, B, B, W, W, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, B, B, W, W, G, G, B, B, B, B, W, W, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, B, W, W, B, G, G, B, B, B, W, W, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, W, W, B, B, G, G, B, B, W, W, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, W, W, B, B, B, G, G, B, W, W, B, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, W, W, B, B, B, B, G, G, W, W, B, B, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, W, W, B, B, B, G, G, B, W, W, B, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ], [ G, G, G, G, G, G, G, G, G, G, G, B, B, G, G, G, G, G, B, B, B, B, G, G, G, B, B, W, W, B, B, G, G, B, B, W, W, B, B, G, G, G, B, B, B, B, G, G, G, G, G, B, B, G, G, G, G, G, G, G, G, G, G, G ]] while is_shown: # Recommencer l'animation a zero quand la derniere frame est atteinte. if image_index >= len(images): image_index = 0 # Arreter l'animation et l'effacer de l'ecran # quand l'agent interagit avec le joystick direction = userinput.get_joystick_direction() if direction == "left" or direction == "right": is_shown = False sense.clear() # Affichage de la frame actuelle pendant # 0.25 secondes avant de passer a la prochaine frame. sense.set_pixels(images[image_index]) time.sleep(.25) image_index += 1