Logic Flow
Example of Code
Import Module
from nanoleafapi import Nanoleaf, NanoleafDigitalTwin, RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE
import random
Defining Components
panel_id_list = nl.get_ids()
GREY = (192,192,192)
color_list = [RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE, GREY]
dots_list = []
ghosts_list = []
power_pellet_list = []
pacman = ""
pacman_move_list_x = []
pacman_move_list_y = []
lives = ""
ghosts_move_list = []
end_game = ""
Game Setting Function
Generate a new game setting.
def game_setting():
panel_id_list = nl.get_ids()
for i in panel_id_list:
digital_twin.set_color(i, WHITE)
dots_list.append(i)
for i in random.sample(dots_list, 4):
digital_twin.set_color(i, random.choice([RED, ORANGE, GREEN, PINK, PURPLE]))
del dots_list(i)
ghosts_list.append((i, "Normal"))
for i in random.sample(dots_list, 5):
digital_twin.set_color(i, GREY)
del dots_list(i)
power_pillet_list.append(i)
for i in random.sample(dots_list, 1):
digital_twin.set_color(i, YELLOW)
del dots_list(i)
pacman = i
digital_twin.sync()
Touch Event Listener
Listen to the touch event.
event_dict = {}
def event_function(event):
print(event)
event_dict.update(event)
nl.register_event(event_function, [4])
Pacman move Function
def takeSecond(elem):
return elem[1]
def pacman_move():
nanoleaf_info = nl.get_info()
nanoleaf_layout = nanoleaf_info['panelLayout']['layout']['positionData']
touch_id = event_dict['events'][0]['panelId']
pacman_x = nanoleaf_layout[pacman]["x"]
pacman_y = nanoleaf_layout[pacman]["y"]
if nanoleaf_layout[pacman]["x"] == nanoleaf_layout[touch_id]["x"] or nanoleaf_layout[pacman]["y"] == nanoleaf_layout[touch_id]["y"]:
for i in nanoleaf_layout:
if nanoleaf-layout[pacman]["y"] > nanoleaf_layout[touch_id]["y"]:
if nanoleaf_layout[i]["x"] == nanoleaf[pacman]["x"] and nanoleaf_layout[i]["y"] in range(nanoleaf_layout[touch_id]["y"]:nanoleaf_layout[pacman]["y"] + 100:
pacman_move_list_y.append(i, nanoleaf_layout[i]["y"])
pacman_move_list_y.sort(key=takeSecond, reverse=True)
if nanoleaf-layout[pacman]["y"] < nanoleaf_layout[touch_id]["y"]:
if nanoleaf_layout[i]["x"] == nanoleaf[pacman]["x"] and nanoleaf_layout[i]["y"] in range(nanoleaf_layout[pacman]["y"]:nanoleaf_layout[touch_id]["y"] + 100:
pacman_move_list_y.append(i)
pacman_move_list_y.sort(key=takeSecond)
if nanoleaf-layout[pacman]["x"] > nanoleaf_layout[touch_id]["x"]:
if nanoleaf_layout[i]["y"] == nanoleaf[pacman]["y"] and nanoleaf_layout[i]["x"] in range(nanoleaf_layout[touch_id]["x"]:nanoleaf_layout[pacman]["x"] + 100:
pacman_move_list_x.append(i)
pacman_move_list_x.sort(key=takeSecond, reverse=True)
if nanoleaf-layout[pacman]["x"] < nanoleaf_layout[touch_id]["x"]:
if nanoleaf_layout[i]["y"] == nanoleaf[pacman]["y"] and nanoleaf_layout[i]["x"] in range(nanoleaf_layout[pacman]["x"]:nanoleaf_layout[touch_id]["x"] + 100:
pacman_move_list_x.append(i)
pacman_move_list_x.sort(key=takeSecond)
for i in pacman_move_list_x[0]:
digital_twin_set_color(i, YELLOW)
digital_twin_set_color(pacman, (0,0,0))
pacman = i
del pacman_move_list_x[0]
for i in pacman_move_list_y[0]:
digital_twin_set_color(i, YELLOW)
digital_twin_set_color(pacman, (0,0,0))
pacman = i
if pacman in dots_list:
del dots_list(pacman)
if pacman in ghosts_list:
if ghosts_list[pacman][1] == "Normal"
lives == lives - 1
Ghosts Move Function
def ghosts_move():
for i in ghosts_list:
ghost_color = digital_twin_get_color(i[0])
for j in nanoleaf_layout:
if nanoleaf_layout[i][0]["x"] - nanoleaf_layout[j]["x"] == 100 or nanoleaf_layout[i][0]["x"] - nanoleaf_layout[j]["x"] == -100 or nanoleaf_layout[i][0]["y"] - nanoleaf_layout[j]["y"] == 100 or nanoleaf_layout[i][0]["y"] - nanoleaf_layout[j]["y"] = -100:
ghosts_move_list.append(j)
if i in dots_list:
digital_twin_set_color([i][0], GREY)
if i in power_pellet_list
digital_twin_set_color(i[0], WHITE)
else:
digital_twin_set_color(i[0], (0,0,0))
[i][0] = random.choice(ghosts_move_list)
digital_twin_set_color([i][0], ghost_color)
Power Pellet Move Function
def power_pellet_move()
if pacman in power_pellet_list:
for i in ghost_list:
i[1] = "Weak"
i[2] = digital_twin_get_color(i[0])
digital_twin_set_color(i[0],BLUE)
del power_pellet_list(pacman)
time.sleep(5)
for i in ghost_list:
i[1] = "Normal"
i[2] = digital_twin_get_color(i[0],i[2])
for i in ghosts_list:
if i[1] == "Weak" and pacman in ghosts_list[0]:
digital_twin_set_color(ghosts_list[pacman][0], ghosts_list[pacman][2])
ghost_list[pacman][1] = "Normal"
Game Initiation
dots_list = []
ghosts_list = []
power_pillet_list = []
Pacman = ""
lives = 4
game_setting()
Game Structure
while True:
if end_game == 0:
pacman_move()
power_pillet_move()
ghosts_move()
if lives == 0
print("Lose")
if len(dots_list) == 0:
print("Win")
pause