Logic Flow
Example of Code
Import Module
from nanoleafapi import Nanoleaf, NanoleafDigitalTwin, RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE
Import time
Import random
Defining components
panel_id_list = nl.get_ids()
color_list = [RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE]
touch_id_list = []
touch_id_1_x_100_check_list = []
touch_id_1_x_200_check_list = []
touch_id_1_y_100_check_list = []
touch_id_1_y_200_check_list = []
touch_id_2_x_100_check_list = []
touch_id_2_x_200_check_list = []
touch_id_2_y_100_check_list = []
touch_id_2_y_200_check_list = []
clear_block_list = []
blank_block_list = []
block_id_copy_list = []
move = ""
Touch Listener
Listen to the touch event.
event_dict = {}
def event_function(event):
print(event)
event_dict.update(event)
nl.register_event(event_function, [4])
Crush Check Function
Check whether the blocks can be swapped and match the colors into consecutive patterns.
def crush_check():
nanoleaf_info = nl.get_info()
nanoleaf_layout = nanoleaf_info['panelLayout']['layout']['positionData']
touch_id = event_dict[event_dict['events'][0]['panelId']]
if len(touch_id_list) < 2:
touch_id_list.append(touch_id)
if len(touch_id_list) == 2:
touch_id_1 = touch_id_list[0]
touch_id_2 = touch_id_list[1]
touch_id_1_color = digital_twin.get_color(touch_id_1)
touch_id_1_try_color = digital_twin.get_color(touch_id_2)
touch_id_2_color = digital_twin.get_color(touch_id_2)
touch_id_2_try_color = digital_twin.get_color(touch_id_1)
for i in nanoleaf_layout:
if nanoleaf_layout[i]["panelId"] == touch_id_1:
touch_id_1_x = nanoleaf_layout[i]["x"]
touch_id_1_y = nanoleaf_layout[i]["y"]
if nanoleaf_layout[i]["panelID"] == touch_id_2:
touch_id_2_x = nanoleaf_layout[i]["x"]
touch_id_2_y = nanoleaf_layout[i]["y"]
for i in nanoleaf_layout:
if i nanoleaf_layout[i]["x"] == touch_id_1_x:
if touch_id_1_y - nanoleaf_layout[i]["y"] == 100 or nanoleaf_layout[i]["y"] - touch_id_1_y == 100:
touch_id_1_x_100_check_list = touch_id_1_x_100_check_list.append(i)
if touch_id_1_y - nanoleaf_layout[i]["y"] == 200 or nanoleaf_layout[i]["y"] - touch_id_1_y == 200:
touch_id_1_x_200_check_list = touch_id_1_x_200_check_list.append(i)
if i nanoleaf_layout[i]["y"] == touch_id_1_y:
if touch_id_1_x - nanoleaf_layout[i]["x"] == 100 or nanoleaf_layout[i]["x"] - touch_id_1_x == 100:
touch_id_1_y_100_check_list = touch_id_1_y_100_check_list.append(i)
if touch_id_1_x - nanoleaf_layout[i]["x"] == 200 or nanoleaf_layout[i]["x"] - touch_id_1_x == 200:
touch_id_1_y_200_check_list = touch_id_1_y_200_check_list.append(i)
if i nanoleaf_layout[i]["x"] == touch_id_2_x:
if touch_id_2_y - nanoleaf_layout[i]["y"] == 100 or nanoleaf_layout[i]["y"] - touch_id_2_y == 100:
touch_id_2_x_100_check_list = touch_id_2_x_100_check_list.append(i)
if touch_id_2_y - nanoleaf_layout[i]["y"] == 200 or nanoleaf_layout[i]["y"] - touch_id_2_y == 200:
touch_id_2_x_200_check_list = touch_id_2_x_200_check_list.append(i)
if i nanoleaf_layout[i]["y"] == touch_id_2_y:
if touch_id_2_x - nanoleaf_layout[i]["x"] == 100 or nanoleaf_layout[i]["x"] - touch_id_2_x == 100:
touch_id_2_y_100_check_list = touch_id_2_y_100_check_list.append(i)
if touch_id_2_x - nanoleaf_layout[i]["x"] == 200 or nanoleaf_layout[i]["x"] - touch_id_2_x == 200:
touch_id_2_y_200_check_list = touch_id_2_y_200_check_list.append(i)
for i in touch_id_1_x_100_check_list:
if touch_id_1_try_color == digital_twin.get_color(i):
for j in touch_id_1_x_200_check_list:
if touch_id_1_try_color == digital_twin.get_color(j) and (nanoleaf_layout[i]["y"] - nanoleaf_layout[j]["y"] == 100 or nanoleaf_layout[j]["y"] - nanoleaf_layout[i]["y"] == 100):
clear_block_list = [touch_id_1, i, j]
for i in touch_id_2_x_100_check_list:
if touch_id_2_try_color == digital_twin.get_color(i):
for j in touch_id_2_x_200_check_list:
if touch_id_2_try_color == digital_twin.get_color(j) and (nanoleaf_layout[i]["y"] - nanoleaf_layout[j]["y"] == 100 or nanoleaf_layout[j]["y"] - nanoleaf_layout[i]["y"] == 100):
clear_block_list = [touch_id_2, i, j]
if len(clear_block_list) != 0:
for i in clear_block_list:
digital_twin.set_color(touch_id_1, touch_id_2_color)
digital_twin.set_color(touch_id_2, touch_id_1_color)
digital_twin.set_color(i,(0,0,0))
clear_block_list = []
move = move + 1
touch_id_list = []
Drop Down Function
Blocks drop down if there is space, and fill with random colors to the empty blocks on the top.
def takeSecond(elem):
return elem[1]
def drop_down():
blank_block_list = []
nanoleaf_info = nl.get_info()
nanoleaf_layout = nanoleaf_info['panelLayout']['layout']['positionData']
for i in nl.get_ids():
if digital_twin.get_color(i) == (0,0,0):
blank_block_list.append(i)
score = score + len(blank_block_list)
for i in blank_block_list:
blank_block_x = nanoleaf_layout[i]["x"]
blank_block_y = nanoleaf_layout[i]["y"]
for i in nanoleaf_latout:
if nanoleaf_layout[i]["x"] == blank_block_x and nanoleaf_layout[i]["y"] > blank_block_y:
block_id_copy_list.append((i,nanoleaf_layout[i]["y"]))
block_id_copy_list.sort(key=takeSecond)
for i in block_id_copy_list:
block_color_copy_list.append(digital_twin.get_color(i))
for i in block_id_copy_list:
try:
digital_twin.set_color(i,block_color_copy_list[i+1])
except IndexError:
digital_twin.set_color(i,(0,0,0))
digital_twin.set_color(block_id_copy_list[-1], random.choice(color_list))
block_id_copy_list = []
Score Display Function
Display the score by changing the color in the same amounts of crush blocks to light blue.
def score_display():
for i in panel_id_list(range(score)):
digital_twin.set_color(i, LIGHT_BLUE)
digital_twin.sync()
Game Initiation
for i in nl.get_ids():
digital_twin.set_color(i, random.choice(color_list))
move = 0
score = 0
Game Structure
while True:
crush_check()
if (0,0,0) in digital_twin.get_all_colors():
drop_down()
if move == 15:
score_display()
pause