diff --git a/LD3320/code/GUI_lib.py b/LD3320/code/GUI_lib.py new file mode 100644 index 0000000000000000000000000000000000000000..ac4e21ccf5475b70747db07e96db9915fb1e1806 --- /dev/null +++ b/LD3320/code/GUI_lib.py @@ -0,0 +1,216 @@ +from machine import SPI, Pin +import st7789, utime + +spi=SPI(0,baudrate=40000000,polarity=1,phase=0,bits=8,endia=0,sck=Pin(6),\ + mosi=Pin(8)) +display=st7789.ST7789(spi,240,240,reset=Pin(11,func=Pin.GPIO,dir=Pin.OUT),\ + dc=Pin(7,func=Pin.GPIO,dir=Pin.OUT)) + +# useful functions and variables: +cl:function = st7789.color565 +fl:function = display.fill +fr:function = display.fill_rect +re:function = display.rect +hl:function = display.hline +vl:function = display.vline +scrprt:function = display.draw_string +ms:function = utime.sleep_ms +sub_xpos:tuple[int] = (0, 80, 160) +sub_ypos:tuple[int] = (0, 222 + 1) +MenuItem_ypos:tuple[int] = (18, 86, 154) + +#ZERO_WHITE = cl(245, 247, 250) +#ZERO_GRAY = cl(225, 227, 230) # OK? +#ZERO_LCYAN = cl(91, 231, 196) +#ZERO_DCYAN = cl(71, 211, 176) # OK? +#ZERO_LBLUE = cl(80, 193, 233) +#ZERO_DBLUE = cl(60, 173, 213) # OK? +#ZERO_LPURPLE = cl(122, 87, 209) +#ZERO_DPURPLE = cl(102, 67, 189) +skin_set:dict[int, tuple] = {1 : (cl(80,193,233),cl(60,173,213),cl(245,247,250),\ +cl(225,227,230),cl(91,231,196),cl(71,211,176),cl(122,87,209),cl(102,67,189))} +color_tuple:tuple = skin_set[1] # 可被修改,标准颜色列表 + +def scr_fill(r:int=255,g:int=255,b:int=255,screen=display): + screen.fill(cl(r,g,b)) + +def scrpst(x,y,s,color=cl(0,0,0),size=2): + scrprt(x,y,s,color=color,bg=color,size=size) + +class Home(): + #主界面上的菜单选项,目前只支持 4 个空位 + def __init__(self): + self.new_length = (160, 200, 240) + self.new_point = (((0, 0), (0, 0), (0, 0)), ((80, 0), (40, 0), (0, 0)),\ + ((0, 80), (0, 40), (0, 0)), ((80, 80), (40, 40), (0, 0))) + self.menu = [0, 0, 0, 0] + self.wallpaper:bytearray = 0 # 壁纸作为图片预先缓存至bytearray中 + def click(self, position:int): # “点击”某一菜单按钮,此时播放 点击动画 -> 切换动画 + # 点击动画 + for l in range(0, 3): + fr(self.new_point[position][l][0], self.new_point[position][l][1],\ + self.new_length[l], self.new_length[l],\ + color_tuple[0 if position == 0 or position == 3 else 4]) + ms(50) + self.menu[position].first_show() + def show(self): # 显示图标和文字 + # 显示壁纸 -> 内存不足,没有足够空间提供图片显示,准备尝试分形几何的基础图形 + #fl(cl(255, 255, 255)) # -> 取消显示纯色图案,改为四色图案交替 + # 显示每个四个空位中每个空位的图标和文字 + fr(0, 0, 120, 120, color_tuple[0]) + scrpst(20, 20, "Game", color_tuple[2], 4) + + fr(120, 0, 120, 120, color_tuple[4]) + scrpst(140, 20, "Con", color_tuple[2], 4) + scrpst(140, 60, "trol", color_tuple[2], 4) + + fr(0, 120, 120, 120, color_tuple[4]) + scrpst(20, 140, "Tool", color_tuple[2], 4) + + fr(120, 120, 120, 120, color_tuple[0]) + scrpst(140, 140, "Set", color_tuple[2], 4) + scrpst(140, 180, "ting", color_tuple[2], 4) + +class Menu(): + global color_tuple + def __init__(self): + # 只存储 icon 和 text 而不是 具体实例 以节省内存 + self.ItemList = [] # 菜单子选项列表 list[list[int, str]] + self.ItemNum:int = 0 # 菜单有几个子选项 + self.ShownItemFirstIndex:int = 0 # 可以显示的子选项的第一个下标 + self.ShownItemList = [0, 0, 0] + self.HighLightP:int = 0 # 菜单高亮子选项的下标,只能为 0, 1, 2 + # 预先设置 3 个子选项,可修改为要显示的 3 个 + self.ShownItemList[0] = MenuItem(0, " ", 0) # 预先设置 3 个 + self.ShownItemList[1] = MenuItem(0, " ", 1) + self.ShownItemList[2] = MenuItem(0, " ", 2) + def add_Item(self, icon, text): + self.ItemList.append([icon, text]) + self.ShownItemList[self.ItemNum].set_index(self.ItemNum) + self.ItemNum += 1 + def process(self): # 预先处理,包括根据子选项数量确定显示分布等等 + if self.ItemNum == 0: # 没有元素,结束预处理 + return + for temp in range(0, 3 if self.ItemNum >= 3 else self.ItemNum): + self.ShownItemList[temp].set_icon(self.ItemList[temp][0]) + self.ShownItemList[temp].set_text(self.ItemList[temp][1]) + def first_show(self): + #最开始的样式:显示 3 个 子项目与 2 个分隔符,第一个子项目高亮 + self.HighLightP = 0 + self.ShownItemList[0].highlight() + self.ShownItemList[1].show() + self.ShownItemList[2].show() + hl(0, 85, 240, color_tuple[3]) + hl(0, 86, 240, color_tuple[3]) + hl(0, 153, 240, color_tuple[3]) + hl(0, 154, 240, color_tuple[3]) + def click(self): + self.ShownItemList[self.HighLightP].click() + def up(self): + #向上选取 + if (self.HighLightP > 0): # 目前选择的不是第一个,不需要修改显示表 + # 接下来高亮的是上一个元素:(注:绝对下标顺序:第一个显示下标+高亮下标-1) + # 1. 取消高亮该元素 + self.ShownItemList[self.HighLightP].show() + # 2. 将待高亮元素下标指向前一个 + self.HighLightP -= 1 + # 3. 高亮该元素 + self.ShownItemList[self.HighLightP].highlight() + else: # 目前选择的是第一个元素,可能需要修改显示表 + if (self.ShownItemFirstIndex > 0): # 上面还有元素可以显示 + pass # 目前数量不会出现这种情况 + else: # 已经到头了,上面没有元素可以显示了 + return # 直接结束 + def down(self): + #向下选取 + if (self.HighLightP < 2 and self.HighLightP < self.ItemNum - 1): + # 目前选择的不是最后一个,不需要修改显示list + # 1. 取消高亮该元素 + self.ShownItemList[self.HighLightP].show() + # 2. 将待高亮元素下标指向后一个 + self.HighLightP += 1 + # 3. 高亮该元素 + self.ShownItemList[self.HighLightP].highlight() + pass + else: # 目前选取的是最后一个,可能需要修改显示list + if (self.ShownItemFirstIndex < self.ItemNum - 2): # 下面没有元素可以显示了 + pass + else: # 下面还有元素可以显示,需要修改显示list + pass # 目前数量不会出现这种情况 + +class MenuItem(): + #打开某一菜单后内部的子选项,比如打开“游戏”菜单后有2048子选项 + def __init__(self, icon:int, text:str, position:int): + self.icon = icon + self.text = text + self.position:int = MenuItem_ypos[position] # 目前只支持 0, 1, 2 个空位 + self.i = 0 + # 基底颜色,一般为白色之类 + # 外框颜色,一般为淡灰色、淡主题色之类,起到视觉上的分隔作用 + self.size_list = [240, 68] # 固定大小 + def set_index(self, index:int): + self.i = index + def set_icon(self, icon:int): + self.icon = icon + def set_text(self, text:str): + self.text = text + def show(self): + # 显示该项,显示基底->显示图标与文字 + # 显示基底 + fr(0, self.position, 240, 68, color_tuple[2]) + # 显示图标与文字 + scrpst(25, self.position + 25, self.text, color_tuple[6], 3) + def highlight(self): + # 高亮该项,告知用户目前“选择”的是这个,但还未点击进入。 + # 修改基底 + fr(0, self.position, 240, 68, color_tuple[6]) + # 重显示图标与文字 注意文字颜色发生改变 + scrpst(25, self.position + 25, self.text, color_tuple[2], 3) + def click(self): + # 点击进入,动画:激活高亮项(前置条件)-> 播放点击动画 + # 修改基底 + fr(0, self.position, 240, 68, color_tuple[7]) + # 重新显示图标与文字 注意文字位置发生轻微改变 + scrpst(27, self.position + 27, self.text, color_tuple[2], 3) + # 延时 + utime.sleep_ms(100) + # 恢复显示 + self.highlight() +class Unit(): + # 部件类,通常实例化为上栏、下栏,显示在图层较顶部 + def __init__(self, position:tuple): + self.position = position # 内含左上角xy坐标 + #self.size = [240, 18] # 如果是上、下栏,大小固定为 240 * 18 + #self.main_color = color[0] # 主体颜色 + #self.outline_color = color[1] # 外框颜色 + self.subpart = [0] * 3 + def change_size(self, size): + # 更改部件大小 + self.size = size # tuple[int,int] + def show(self): + # 显示该部件 + fr(self.position[0], self.position[1], 240, 18, color_tuple[0]) + +class SubUnit(Unit): + # 子部件类,通常被当作位于部件上的小模块(按钮、图文显示之类) + def __init__(self, icon:int, text:str, parent_position:tuple, subposition:int): + self.icon = icon # 内容0:图标 + self.text = text # 内容1:文字 + self.parent_position = parent_position + self.position = subposition # 位置 只有0、1、2三种可能 + def click(self): + # 播放点击动画:自身动画->切换动画 + # 自身动画 + self.show_img(color_tuple[5]) + self.show_str([37, 4]) + utime.sleep_ms(100) + self.show_img(color_tuple[4]) + self.show_str([35, 2]) + # 切换动画需要看情况 + def show_str(self, microchange:tuple): + # 显示文字 + scrpst(self.parent_position[0] + sub_xpos[self.position] + microchange[0], + self.parent_position[1] + microchange[1], self.text, color_tuple[2]) + def show_img(self, color): + # 显示图像(按钮有按钮图像) + fr(self.parent_position[0] + sub_xpos[self.position], self.parent_position[1], 80, 18, color) \ No newline at end of file diff --git a/LD3320/code/Main_1.py b/LD3320/code/Main_1.py new file mode 100644 index 0000000000000000000000000000000000000000..6c618886fdd2d87a57077e1c9aad17c55f29c525 --- /dev/null +++ b/LD3320/code/Main_1.py @@ -0,0 +1,148 @@ +import GUI_lib +from micropython import const +import st7789, gc +from machine import UART, Pin + +spi = GUI_lib.spi + +display = GUI_lib.display +display.init() +display.fill(st7789.color565(255, 255, 255)) + +uart_1 = UART(1, baudrate=9600, parity=0, rx=Pin(5), tx=Pin(0), timeout=99999) + +cl:function = GUI_lib.cl +fl:function = GUI_lib.fl +fr:function = GUI_lib.fr +re:function = GUI_lib.re +hl:function = GUI_lib.hl +vl:function = GUI_lib.vl +scrprt:function = GUI_lib.scrprt +scrpst:function = GUI_lib.scrpst + +ms:function = GUI_lib.ms + +color_tuple = GUI_lib.color_tuple + +def show_bars(): + up_bar.show() + up_bar.subpart[0].show_str((10, 2)) + down_bar.show() + down_bar.subpart[0].show_img(color_tuple[4]) + down_bar.subpart[0].show_str((35, 2)) + down_bar.subpart[2].show_img(color_tuple[4]) + down_bar.subpart[2].show_str((35, 2)) + +up_bar = GUI_lib.Unit((0, 0)) +#up_bar.show() + +up_bar.subpart[0] = GUI_lib.SubUnit(0, "15:10", (0, 0), 0) +#up_bar.subpart[0].show_str((10, 2)) + +down_bar = GUI_lib.Unit((0, 222)) +#down_bar.show() + +down_bar.subpart[0] = GUI_lib.SubUnit(0, "X", (0, 222), 0) +#down_bar.subpart[0].show_img(color_tuple[4]) +#down_bar.subpart[0].show_str((35, 2)) + +down_bar.subpart[2] = GUI_lib.SubUnit(0, "O", (0, 222), 2) +#down_bar.subpart[2].show_img(color_tuple[4]) +#down_bar.subpart[2].show_str((35, 2)) + +home = GUI_lib.Home() + +GameMenu = GUI_lib.Menu() +GameMenu.add_Item(0, "2 0 4 8") +GameMenu.add_Item(0, "U p D o w n") +GameMenu.process() + +ControlMenu = GUI_lib.Menu() +ControlMenu.add_Item(0, "W h e r e") +ControlMenu.add_Item(0, "W h a t") +ControlMenu.process() + +ToolMenu = GUI_lib.Menu() +ToolMenu.add_Item(0, "C l o c k") +ToolMenu.add_Item(0, "Calendar") +ToolMenu.process() + +SettingMenu = GUI_lib.Menu() +SettingMenu.add_Item(0, "T i m e") +SettingMenu.add_Item(0, "D a t e") +SettingMenu.process() + +home.menu[0] = GameMenu +home.menu[1] = ControlMenu +home.menu[2] = ToolMenu +home.menu[3] = SettingMenu + +print(gc.mem_free()) + +cmdlevel = -1 +chnum = 0 +cmdtype = [] # 0:主菜单,1功能,2游戏,3控制,4设置 +homeshown = False +trans = {1:2, 2:0, 3:1, 4:3} +is_new = True + +while True: + if not uart_1.bufempty(): + newinfo = str(bytes(uart_1.readline(), 'utf8'))[2:-3] + print(newinfo) + if (newinfo == '0'): # 主菜单 + cmdlevel = 0 + if not homeshown: + home.show() + homeshown = True + elif (newinfo == '1'): # 功能 + if cmdlevel == 0: + cmdtype.append(1) + homeshown = False + home.menu[2].first_show() + show_bars() + cmdlevel = 1 + elif (newinfo == '2'): # 游戏 + if cmdlevel == 0: + cmdtype.append(2) + homeshown = False + home.menu[0].first_show() + show_bars() + cmdlevel = 1 + elif (newinfo == '3'): # 控制 + if cmdlevel == 0: + cmdtype.append(3) + homeshown = False + home.menu[1].first_show() + show_bars() + cmdlevel = 1 + elif (newinfo == '4'): # 设置 + if cmdlevel == 0: + cmdtype.append(4) + homeshown = False + home.menu[3].first_show() + show_bars() + cmdlevel = 1 + elif (newinfo == '5'): # 上 + if cmdlevel == 1: + chnum = 0 if chnum == 0 else chnum - 1 + home.menu[trans[cmdtype[-1]]].up() + elif (newinfo == '6'): # 下 + if cmdlevel == 1: + chnum = 1 if chnum == 1 else chnum + 1 + home.menu[trans[cmdtype[-1]]].down() + elif (newinfo == '9'): # 确认 + if cmdlevel == 1: + down_bar.subpart[2].click() + elif (newinfo == '10'): # 取消 + if cmdlevel == 1: + down_bar.subpart[0].click() + elif (newinfo == '11'): # 选择 + if cmdlevel == 1: + home.menu[trans[cmdtype[-1]]].click() + elif (newinfo == '14'): # 返回 + if cmdlevel == 1: + cmdtype.pop() + cmdlevel = 0 + home.show() + homeshown = True \ No newline at end of file diff --git a/LD3320/code/Main_2.py b/LD3320/code/Main_2.py new file mode 100644 index 0000000000000000000000000000000000000000..12c72bc52d84ed6b52260355bd74391142fbd902 --- /dev/null +++ b/LD3320/code/Main_2.py @@ -0,0 +1,327 @@ +import GUI_lib +from micropython import const +import utime, urandom, st7789, gc +from machine import UART, Pin + +spi = GUI_lib.spi + +display = GUI_lib.display +display.init() +display.fill(st7789.color565(255, 255, 255)) + +uart_1 = UART(1, baudrate=9600, parity=0, rx=Pin(5), tx=Pin(0), timeout=99999) + +cl:function = GUI_lib.cl +fl:function = GUI_lib.fl +fr:function = GUI_lib.fr +re:function = GUI_lib.re +hl:function = GUI_lib.hl +vl:function = GUI_lib.vl +scrprt:function = GUI_lib.scrprt +scrpst:function = GUI_lib.scrpst + +ms:function = GUI_lib.ms + +color_tuple = GUI_lib.color_tuple + +Room_change_x:dict[int,int] = {2:25, 4:25, 8:25, 16:20, 32:20, 64:20, 128:15, 256:15, 512:15, 1024:10, 2048:10} +Room_change_y:int = const(15) + +class Room(): + def __init__(self, y, x, score): + self.real_y, self.des_y = y, y + self.real_x, self.des_x = x, x + self.board_y = y // 60 + self.board_x = x // 60 + self.now_score, self.des_score = score, score + self.wbdes = False + self.ismoving = False + def show(self, y:int = None, x:int = None, state:int = 1): + if self.now_score == 0: return + + if (y == None): y = self.real_y + if (x == None): x = self.real_x + + fr(x, y, 60, 60, color_tuple[2 if state == 1 else 0]) + scrpst(x + Room_change_x[self.now_score], y + Room_change_y, str(self.now_score), color_tuple[0 if state == 1 else 2]) + def hidden(self, x:int = None, y:int = None): + if (x == None): x = self.real_x + if (y == None): y = self.real_y + + fr(x, y, 60, 60, color_tuple[7]) +class Board(): + def __init__(self): + self.all_score = 0 + self.board = [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]] + for y in range(0, 4): + for x in range(0, 4): + self.board[y][x] = Room(y * 60, x * 60, 0) + def check_not_fill(self): + for y in range(0, 4): + for x in range(0, 4): + if self.board[y][x].now_score == 0: return True + return False + def check_cannot_move(self): + for y in range(0, 4): + for x in range(0, 3): + if self.board[y][x].now_score == self.board[y][x + 1].now_score: return False + for y in range(0, 3): + for x in range(0, 4): + if self.board[y][x].now_score == self.board[y + 1][x].now_score: return False + return True + def get_all_score(self): + for y in range(0, 4): + for x in range(0, 4): + self.all_score += self.board[y][x].now_score + def check_win(self): + for y in range(0, 4): + for x in range(0, 4): + if self.board[y][x].now_score == 2048: return True + return False + def gameover(self, is_win:bool): + fr(0, 80, 240, 80, color_tuple[0]) + scrpst(3, 92, "{} All Score:".format("Win!" if is_win == True else "QAQ."), color_tuple[6], 3) + scrpst(100, 132, str(self.all_score), color_tuple[6], 3) + def show_bg(self): + fl(color_tuple[7]) + def get_deslist_movcount(self, l:list): + mov_count = 0 + for y in range(0, 4): + for x in range(0, 4): + room1 = self.board[y][x] + if (room1.wbdes): l.append(room1) + if (room1.real_y != room1.des_y): + room1.ismoving = True + mov_count += 1 + return mov_count + def add_room(self, y:int = None, x:int = None, score:int = None) -> bool: + + if score == None: score = 2 * (urandom.randint(0, 1) + 1) + if y == None: y = urandom.randint(0, 3) + if x == None: x = urandom.randint(0, 3) + + b = self.board[y][x] + if (b.now_score == 0): + b.now_score, b.des_score = score, score + b.show(state = 0) + ms(50) + b.show() + return True + else: + for y in range(0, 4): + for x in range(0, 4): + b = self.board[y][x] + if (b.now_score == 0): + b.now_score, b.des_score = score, score + b.show(state = 0) + ms(50) + b.show() + return True + return False + def swap(self, y1, x1, y2, x2): + if (y1 != y2 or x1 != x2): + self.board[y1][x1],self.board[y2][x2]=self.board[y2][x2],self.board[y1][x1] + def re_deal(self): + b = self.board + for y in range(0, 4): + for x in range(0, 4): + room1 = b[y][x] + room1.board_y, room1.board_x = y, x + room1.real_y, room1.des_y = y * 60, y * 60 + room1.real_x, room1.des_x = x * 60, x * 60 + room1.now_score = room1.des_score + room1.wbdes = False + room1.ismoving = False + room1.show() + def up(self): + b = self.board + for x in range(0, 4): + temp_list = [] + for y in range(0, 4): + if b[y][x].now_score != 0: temp_list.append(y) + l = len(temp_list) + for i in range(0, l): + self.swap(i, x, temp_list[i], x) + for i in range(0, l): + b[i][x].des_y = i * 60 + y = 0 + while y < l - 1: + room1 = b[y][x] + room2 = b[y + 1][x] + if room1.now_score == room2.now_score: + room2.des_score = room2.now_score * 2 + room2.des_y = room1.des_y + room1.wbdes = True + room1.des_score = 0 + for tempi in range(y, l - 1): + self.swap(tempi, x, tempi + 1, x) + b[tempi][x].des_y = (tempi) * 60 + l -= 1 + y += 1 + + des_list = [] + mov_count = self.get_deslist_movcount(des_list) + des_count = len(des_list) + while (des_count > 0 or mov_count > 0): + for y in range(1, 4): + for x in range(0, 4): + for fix_y in range(0, 4): + if (b[fix_y][x].board_y == y): break + room1 = b[fix_y][x] + if room1.now_score == 0: continue + if (room1.des_y < room1.real_y): + room1.hidden() + room1.real_y -= 5 + room1.show() + else: + if (room1.ismoving): + room1.ismoving = False + mov_count -= 1 + for temp_room in des_list: + if temp_room.wbdes and (room1.real_y == temp_room.real_y): + room1.now_score = room1.des_score + room1.show() + temp_room.wbdes = False + des_count -= 1 + self.re_deal() + def down(self): + b = self.board + for x in range(0, 4): + temp_list = [] + for y in range(3, -1, -1): + if b[y][x].now_score != 0: temp_list.append(y) + l = len(temp_list) + for i in range(0, l): + self.swap(3 - i, x, temp_list[i], x) + for i in range(0, l): + b[3 - i][x].des_y = (3 - i) * 60 + y = 3 + while y > 3 - (l - 1): + room1, room2 = b[y][x], b[y - 1][x] + if room1.now_score == room2.now_score: + room2.des_score, room2.des_y = room2.now_score * 2, room1.des_y + room1.wbdes, room1.des_score = True, 0 + for tempi in range(y, 3 - (l - 1), -1): + self.swap(tempi, x, tempi - 1, x) + b[tempi][x].des_y = (tempi) * 60 + l -= 1 + y -= 1 + + des_list = [] + mov_count = self.get_deslist_movcount(des_list) + des_count = len(des_list) + while (des_count > 0 or mov_count > 0): + for y in range(2, -1, -1): + for x in range(0, 4): + for fix_y in range(0, 4): + if (b[fix_y][x].board_y == y): break + room1 = b[fix_y][x] + if room1.now_score == 0: continue + if (room1.des_y > room1.real_y): + room1.hidden() + room1.real_y += 5 + room1.show() + else: + if (room1.ismoving): + room1.ismoving = False + mov_count -= 1 + for temp_room in des_list: + if temp_room.wbdes and (room1.real_y == temp_room.real_y): + room1.now_score = room1.des_score + room1.show() + temp_room.wbdes = False + des_count -= 1 + self.re_deal() + def left(self): + b = self.board + for y in range(0, 4): + temp_list = [] + for x in range(0, 4): + if b[y][x].now_score != 0: temp_list.append(x) + l = len(temp_list) + for i in range(0, l): + self.swap(y, i, y, temp_list[i]) + for i in range(0, l): + b[y][i].des_x = i * 60 + x = 0 + while x < l - 1: + room1, room2 = b[y][x], b[y][x + 1] + if room1.now_score == room2.now_score: + room2.des_score, room2.des_x = room2.now_score * 2, room1.des_x + room1.wbdes, room1.des_score = True, 0 + for tempi in range(x, l - 1): + self.swap(y, tempi, y, tempi + 1) + b[y][tempi].des_x = (tempi) * 60 + l -= 1 + x += 1 + + des_list = [] + mov_count = self.get_deslist_movcount(des_list) + des_count = len(des_list) + while (des_count > 0 or mov_count > 0): + for x in range(1, 4): + for y in range(0, 4): + for fix_x in range(0, 4): + if (b[y][fix_x].board_x == x): break + room1 = b[y][fix_x] + if room1.now_score == 0: continue + if (room1.des_x < room1.real_x): + room1.hidden() + room1.real_x -= 5 + room1.show() + else: + if (room1.ismoving): + room1.ismoving = False + mov_count -= 1 + for temp_room in des_list: + if temp_room.wbdes and (room1.real_x == temp_room.real_x): + room1.now_score = room1.des_score + room1.show() + temp_room.wbdes = False + des_count -= 1 + self.re_deal() + +board1 = Board() +board1.show_bg() +board1.add_room(0, 0, 2) +board1.add_room(0, 1, 2) + +def gamecheck(): + global board1 + if board1.check_cannot_move(): + board1.get_all_score() + board1.gameover(board1.check_win()) + +setted = False + +while True: + if not uart_1.bufempty(): + newinfo = str(bytes(uart_1.readline(), 'utf8'))[2:-3] + print(newinfo) + if not setted: + try: + urandom.seed(utime.time() + int(newinfo)) + except: + urandom.seed(utime.time()) + setted = True + if (newinfo == '5'): # 上 + board1.up() + board1.add_room() + board1.add_room() + gamecheck() + elif (newinfo == '6'): # 下 + board1.down() + board1.add_room() + board1.add_room() + gamecheck() + elif (newinfo == '7'): # 左 + board1.left() + board1.add_room() + board1.add_room() + gamecheck() + elif (newinfo == '8'): # 右 + print("no 'right' methozd now") + print(gc.mem_free()) \ No newline at end of file diff --git a/LD3320/img/2048.png b/LD3320/img/2048.png new file mode 100644 index 0000000000000000000000000000000000000000..e9a96868cb15457e64547f021bba981f85c54073 Binary files /dev/null and b/LD3320/img/2048.png differ diff --git a/LD3320/img/menu.jpg b/LD3320/img/menu.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c9838027b1b1718d1b3ca2816aff59ae2c37cd71 Binary files /dev/null and b/LD3320/img/menu.jpg differ diff --git a/LD3320/readme.md b/LD3320/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..4b33023df459bc2f393d16cb128acbee4aad2754 --- /dev/null +++ b/LD3320/readme.md @@ -0,0 +1,28 @@ +# 语音识别 +## 案例展示 + +![menu](img\menu.jpg) + +![2048](img\2048.png) +# 物理连接 +## 传感器选择 +传感器选择LD3320语音识别模块 +## 传感器接线 +注意,需要额外接入5V电源 + +|Waffle Nano| 传 感 器 | +|-----------|-----------| +|GO0 |RX | +|GO5 |TX | + +|额 外 电 源| 传 感 器 | +|----------|----------| +|5V |5V | +|GND |GND | + +## 传感器库使用 +由于LD3320内置STC11L08XE处理器,每次串口传输的数据都可以通过预编程来给定易于处理的格式,故而该传感器没有使用专门的库,所有处理都交由主程序。 + +# 代码案例复现 + +首先需要上传 `.\code` 目录下的 `GUI_lib.py` 文件,然后烧录 `.\code` 目录下的 `Main_1.py` 文件或者 `Main_2.py` 文件 \ No newline at end of file