【DigiKey“智造万物,快乐不停”创意大赛】MAIX BIT KIT上的“九子贤棋”
[复制链接]
“九子贤棋”又名LuckyStar,是中国西部的一种在五角星形棋盘上玩的益智游戏。
开始时在五角星形棋盘的每个直线交叉点各放一枚棋子,然后按玩家意愿,移除一个棋子。之后,玩家可以像跳棋那样,移动一颗棋子到空白的位置,同时可以移除移动过程中跳过的棋子。移动棋子必须沿着直线,一次必须跳过一枚棋子,且只能跳过一枚棋子。当按前述规则无法移动任何棋子时,游戏结束。如果游戏结束时,棋盘上只剩下一枚棋子,则玩家获胜。
做出来了:
- class OneAppStats():
- INIT = 0
- RUNNING = 1
- SLEEP = 2
- RESTORE = 3
- EXIT = 4
- KILL = 5
- KILLED = 6
- BACK = 7
-
-
- class OneApp(object):
- def __init__(self, name=None):
- self.name = name
- self.data = {}
- self.stat = OneAppStats.INIT
- self.childApps =[]
- def setup(self, **kw):
- for key in kw:
- self.data[key]=kw[key]
- self.stat = OneAppStats.RUNNING
- def loop(self):
- print('__getitem__', key)
- return self.data[key] if key in self.data else None
- def beforePaint(self):
- pass
- def rePaint(self):
- pass
- def afterPaint(self):
- pass
- def _paint(self):
- self.beforePaint()
- self.rePaint()
- self.afterPaint()
- def onEvent(self,evtName,*args,**kw):
- print('x11',evtName,len(self.childApps), self.name , 'onEvent:',evtName,'*args',args,'**kw',kw)
- for childrenApp in self.childApps:
- if childrenApp.stat in (OneAppStats.RUNNING,OneAppStats.SLEEP,OneAppStats.RESTORE,OneAppStats.BACK):
-
- childrenApp.onEvent(*args,**kw)
- def _removeApp(self,app):
- app.stat = OneAppStats.KILLED
- self.childApps.remove(app)
- del app
- def _countChildApp(self):
- cntAliveChildren = 0
- for childrenApp in self.childApps[::]:
- if childrenApp.stat == OneAppStats.INIT :
- cntAliveChildren = cntAliveChildren +1
- elif childrenApp.stat == OneAppStats.RUNNING :
- flag = childrenApp.loop()
- if flag in (OneAppStats.EXIT,OneAppStats.KILL):
-
- self._removeApp(childrenApp)
- else:
- cntAliveChildren = cntAliveChildren +1
- elif childrenApp.stat in (OneAppStats.EXIT,OneAppStats.KILL):
- self._removeApp(childrenApp)
- print('_removeApp',childrenApp,len(self.childApps) )
- return cntAliveChildren
- def _loop(self):
- """
- cntAliveChildren = 0
- for childrenApp in self.childApps[::]:
- if childrenApp.stat == OneAppStats.INIT :
- cntAliveChildren = cntAliveChildren +1
- elif childrenApp.stat == OneAppStats.RUNNING :
- flag = childrenApp.loop()
- if flag in (OneAppStats.EXIT,OneAppStats.KILL):
- self._removeApp(childrenApp)
- else:
- cntAliveChildren = cntAliveChildren +1
- elif childrenApp.stat in (OneAppStats.EXIT,OneAppStats.KILL):
- self._removeApp(childrenApp)
- print('_removeApp',childrenApp,len(self.childApps) )
- """
- cntAliveChildren = self._countChildApp()
- return OneAppStats.BACK if cntAliveChildren>0 else OneAppStats.RUNNING
- def loop(self):
- self._loop()
- def _startChildApp(self,defClass,appName=None):
- childrenApp = defClass(appName)
- self.childApps.append(childrenApp)
- return childrenApp
-
- def doHideback(self):
- self.stat = OneAppStats.RUNNING
- return self.stat
- def doSleep(self):
- self.stat = OneAppStats.SLEEP
- return self.stat
- def doRestore(self):
- self.stat = OneAppStats.RESTORE
-
-
- self.stat = OneAppStats.RUNNING
-
-
- return self.stat
- def doKill(self):
- self.stat = OneAppStats.KILL
- return self.stat
- def doStop(self):
-
-
- self.stat = OneAppStats.EXIT
-
-
- return self.stat
-
- import time
-
- DEBUG_DELAY = 0.5
-
-
-
- MSG_LEFT = 160
- MSG_TOP = 10
- '''
- 0 show_msg("huanying ",NONE);
- 1 show_msg("remove",p );
- 2 show_msg("pickup", game_piece_id);
- 3 show_msg('You Win',NONE);
- 4 show_msg('Game Over',NONE);
- 5 show_msg("move ", game_piece_id );
- 6 show_msg("release ", pieces[point_id] );
- '''
- class lucky9starClass():
- def __init__(self):
- self.btnFunCnt = 1+1
- self.btnCnt = 10 + self.btnFunCnt
- self.msg_id = 0
- self.msg_num = None
-
- self.edges=( (0,3,6,8),(0,2,5,7),(1,2,3,4),(1,5,9,8),(4,6,9,7) )
-
- self.point_in_edges=[list() for _ in range(10)]
- for i,edge in enumerate(self.edges):
- for j in edge:
- self.point_in_edges[j].append(i)
-
- self.point_in_edges=tuple([ tuple(x) for x in self.point_in_edges])
-
- self.points = ((120,18),(15,93),(93,93),(146,93),(225,93),(78,142),(161,142),(56,221),(183,221),(120,172))
-
- self.pieces=list(range(1,10+1))
-
-
- self.pieces,self.positions = self.init_pieces()
- self.game_point_id,self.game_piece_id=0,0
- self.game_state=0
-
-
- self.selIDX = 0
-
- print("init-1")
-
-
-
- def setScreen(self,obj_screen):
- self.screen = obj_screen
- self.imgBuf = image.Image()
-
-
- def init_pieces(self):
-
-
- v_pieces=list(range(1,10+1))
-
- v_positions=[[]]+list(range(10))
- return (v_pieces,v_positions)
-
-
- def test_move(self,v_pieces,v_positions,start_point,target_point):
-
-
-
- for e in self.edges:
- if start_point in e and target_point in e :
- i=e.index(start_point)
- j=e.index(target_point)
-
-
-
-
- if 0<=i<4 and 0<=j<4 and not v_pieces[target_point] and abs(i-j)==2 and v_pieces[e[(i+j)//2]]:
- return e[(i+j)//2]
- return False
-
-
- def piece_pickup(self,v_pieces,v_positions,the_point):
-
- tmp_piece=v_pieces[the_point]
- v_positions[tmp_piece]=-1
- v_pieces[the_point]=0
- return tmp_piece
-
-
- def piece_putdown(self,v_pieces,v_positions,the_piece,the_point):
-
- v_positions[the_piece]=the_point
- v_pieces[the_point]=the_piece
-
-
- def move_point_to_point(self,v_pieces,v_positions,start_point,target_point):
-
- i = self.test_move(v_pieces,v_positions,start_point,target_point)
- if i:
- p = self.piece_pickup(v_pieces,v_positions,start_point)
- self.piece_putdown(v_pieces,v_positions,p,target_point)
- return self.piece_pickup(v_pieces,v_positions,i)
- return None
-
-
- def test_is_game_start(self,v_pieces,v_positions):
-
- return min(v_pieces)>0
-
-
- '''
- 1
- 2--3-4--5
- 6 7
- / a \
- 8 9
- '''
- def uart_debug_out_pieces(self,v_pieces):
- print("~~~ Pieces BEGIN ~~~")
- print(" %d"%v_pieces[0])
- print("%d--%d--%d--%d"%(v_pieces[1],v_pieces[2],v_pieces[3],v_pieces[4]))
- print(" %d %d"%(v_pieces[5],v_pieces[6]))
- print(" / %d \\"%v_pieces[9])
- print("%d %d"%(v_pieces[7],v_pieces[8]))
- print("~~~ Pieces END ~~~")
-
-
- def get_piece_targets(self,v_pieces,v_positions,piece):
- targets=[]
- position_point=v_positions[piece]
-
- for edge_id in self.point_in_edges[position_point]:
- edge=self.edges[edge_id]
- idx = edge.index(position_point)
-
- for tmp_idx in ( idx-2,idx+2):
-
- if 0<=tmp_idx<4 and self.test_move(v_pieces,v_positions,position_point,edge[tmp_idx] ):
- targets.append(edge[tmp_idx])
- return targets
-
-
-
- def test_all_piece_can_not_move(self,v_pieces,v_positions):
-
- return 0==sum([ len(self.get_piece_targets(v_pieces,v_positions,p)) for p in self.pieces if p>0 ] )
-
-
- def count_exists_piece_num(self,v_pieces,v_positions):
- return len([ 1 for p in v_pieces if p>0 ])
-
-
- def solution(self,v_pieces,v_positions,steps=[]):
- import random
- if 10==self.count_exists_piece_num(v_pieces,v_positions):
-
-
- for i_piece in (random.choice([1,2,5,8,9]),random.choice([3,4,6,7,10])):
- new_pieces,new_positions,new_steps=v_pieces[::],v_positions[::],steps[::]
- tmp_point = v_positions[i_piece]
- self.piece_pickup(new_pieces,new_positions,tmp_point )
- new_steps.append( (i_piece,tmp_point,tmp_point) )
- result = self.solution(new_pieces,new_positions,new_steps)
- return result
- elif self.test_all_piece_can_not_move(v_pieces,v_positions):
-
- if 1==self.count_exists_piece_num(v_pieces,v_positions):
- print("成功解开",steps)
- return steps
- else:
-
- return False
- else:
-
- success_flag = False
- tmp_list = list(range(1,10+1))
- random.shuffle(tmp_list)
- for i_piece in tmp_list:
- tmp_point = v_positions[i_piece]
- if tmp_point>=0 :
- tgts = self.get_piece_targets(v_pieces,v_positions,i_piece)
- for target_point in tgts:
- new_pieces,new_positions,new_steps=v_pieces[::],v_positions[::],steps[::]
- self.move_point_to_point(new_pieces,new_positions,tmp_point,target_point)
- new_steps.append( (i_piece,tmp_point,target_point) )
- result = self.solution(new_pieces,new_positions,new_steps)
- if result:
- success_flag = result
- return success_flag
- return success_flag
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def select_a_point(self,point_id):
-
- self.game_point_id=point_id
- self.game_piece_id=0
- if point_id!=None and self.test_is_game_start(self.pieces,self.positions):
- p = self.piece_pickup(self.pieces,self.positions,point_id)
-
-
- self.game_point_id=None
-
- self.msg_id = 1
- self.msg_num = p
- return 0
- elif point_id!=None :
- self.game_piece_id=self.pieces[point_id]
-
- self.msg_id = 2
- self.msg_num = self.game_piece_id
- return self.game_piece_id
- else :
- return 0
-
-
-
-
-
-
-
-
- def release_a_point(self,point_id):
-
- if point_id==None:
-
- point_id=self.game_point_id
- if point_id!=None and self.game_piece_id>0:
-
- self.game_piece_id = 0
- elif self.game_point_id!=None and self.game_piece_id>0:
- self.uart_debug_out_pieces(self.pieces)
- if self.test_move(self.pieces,self.positions,self.game_point_id,point_id):
-
- p = self.move_point_to_point(self.pieces,self.positions,self.game_point_id,point_id)
-
-
-
-
-
-
- f = self.test_all_piece_can_not_move(self.pieces,self.positions)
- if f :
- print(">>> all_piece_can_not_move,%d"%f)
- self.uart_debug_out_pieces(self.pieces)
- if 1==self.count_exists_piece_num(self.pieces,self.positions):
-
- self.msg_id = 3
- self.msg_num = None
- print(">>> You Win")
- else:
-
- self.msg_id = 4
- self.msg_num = None
- print(">>> Game Over");
- else:
-
- self.msg_id = 5
- self.msg_num = self.game_piece_id
- self.game_piece_id=0
- else:
- point_id=self.game_point_id
- if point_id!=None and self.pieces[point_id]>0:
-
- self.game_piece_id = 0
-
- self.msg_id = 6
- self.msg_num = self.pieces[point_id]
-
- def map_value(self,value,vin_min,vin_max,vout_min,vout_max,FullScale=False,ForceInteger=True):
- func = int if ForceInteger else lambda x:x
- if value <=vin_min:
- return func(vout_min)
- elif value>=vin_max:
- return func(vout_max)
- else:
- scale_max = vout_max +1 if FullScale else vout_max
- return func( vout_min + (value-vin_min)*(scale_max - vout_min)/(vin_max - vin_min) )
-
- def confirm_select(self):
-
- print( 'confirm_select(self)' , self.selIDX )
- if self.selIDX==0:
-
- self.pieces,self.positions = self.init_pieces()
- self.game_state=0
- self.msg_id = 0
- self.msg_num = None
- elif self.selIDX >=1 and self.selIDX <=10 :
- print('confirm_select:',self.selIDX)
- if self.game_state==0:
- i = self.select_a_point(self.selIDX-1)
- print(i)
- if i!=0:
- self.game_state=1
- print("confirm_select:try to pickup a piece",self.game_state,self.game_point_id,self.game_piece_id)
- elif self.game_state==1:
- self.release_a_point(self.selIDX-1)
- self.game_state=0
- print("confirm_select:try to putdown a piece",self.game_state,self.game_point_id,self.game_piece_id)
- elif self.selIDX == 10 +1 :
-
- return False
- return True
-
- """
- ##### DRAW LIB begin #####
- from char_cn import *
- def drawXBM(self,x,y,w,h,buffer):
- #fb = framebuf.FrameBuffer(bytearray(buffer), w, h, framebuf.MVLSB)
- #oled.blit(fb, x, y)
- self.drawBuf(buffer,x,y,w,h)
-
- ### draw Bitmap Buffer
- def drawBuf(self,buffer,x,y,w,h):
- fb = framebuf.FrameBuffer(buffer, w, h, framebuf.MVLSB)
- oled.blit(fb, x, y)
-
- ### draw piece without focus
- def draw_piece_without_focus(self,x,y):
- #p = bytearray(b'\x38\x7c\x7c\x7c\x38')
- #self.drawBuf(p,x,y,5,8)
- #USE:piece
- #p = bytearray(b'\x0e\x1f\x1f\x1f\x0e')
- #self.drawBuf(p,x,y+2,5,5)
- imgBuf.draw_circle( (x,y, 5) , color=lcd.BLUE, thickness=1, fill=True )
-
- ### draw piece focus
- def draw_piece_focus(self,x,y):
- #imgBuf.draw_line(x+3, y+2 , x+3+ 3, y+2 , color=lcd.YELLOW) #oled.hline(x+3, y+2 , 3, 1)
- #imgBuf.draw_line(x+3, y+2 , x , y+2+3, color=lcd.YELLOW) #oled.line( x+3, y+2 , x , y+2+3, 1)
- #imgBuf.draw_line(x , y+2+3, x ,y+2+3+3, color=lcd.YELLOW) #oled.vline(x , y+2+3, 3, 1)
- #imgBuf.draw_line(x+5, y+2 , x+8, y+2+3, color=lcd.YELLOW) #oled.line( x+5, y+2 , x+8, y+2+3, 1)
- #imgBuf.draw_line(x+8, y+2+3, x+8,y+2+3+3, color=lcd.YELLOW) #oled.vline(x+8, y+2+3, 3, 1)
- #imgBuf.draw_line(x , y+7 , x+3, y+2+8, color=lcd.YELLOW) #oled.line( x , y+7 , x+3, y+2+8, 1)
- #imgBuf.draw_line(x+3, y+2+8, x+3+ 3, y+2+8, color=lcd.YELLOW) #oled.hline(x+3, y+2+8, 3, 1)
- #imgBuf.draw_line(x+5, y+2+8, x+8, y+2+5, color=lcd.YELLOW) #oled.line( x+5, y+2+8, x+8, y+2+5, 1)
- imgBuf.draw_circle( (x,y, 7) , color=lcd.YELLOW, thickness=1, fill=False )
-
- ### draw piece with focus
- def draw_piece_with_focus(self,x,y):
- self.draw_piece_without_focus(x,y)
- self.draw_piece_focus(x-2,y-2)
-
- ### draw chinese char
- def draw_chinese_char(self,x,y,c):
- p = bytearray(c)
- self.drawBuf(p,x,y,8,8)
-
- ### draw half width char
- def draw_half_width_char(self,x,y,c):
- p = bytearray(c)
- self.drawBuf(p,x,y,4,8)
-
- ##### DRAW LIB end #####
-
- def update_number(self, x, y,num):
- i = 0
- if num >= 10 :
- print(" >pos:",x+i," , n:",int(num / 10))
- ##u8g2.drawXBM(x + i, 11, 4, 7, CHAR_NUM_BITMAP_ARRAY[ int(num / 10) ])
- self.draw_half_width_char( x + i, y, CHAR_NUM_BITMAP_ARRAY[ int(num / 10) ] )
- i += 4
- #
- if num>=0 :
- print(" >pos:",x+i," , n:",int(num % 10) )
- #//u8g2.drawXBM(x + i, 11, 4, 7, CHAR_NUM_BITMAP_ARRAY[ int(num % 10) ])
- self.draw_half_width_char( x + i, y, CHAR_NUM_BITMAP_ARRAY[ int(num % 10) ] )
- #print(" ,n:",msg_num)
- else :
- print("ALERT: num < 0")
- """
-
- def update_message(self):
-
-
- self.imgBuf.draw_rectangle(MSG_LEFT+0,MSG_TOP+0,40,7, color=lcd.YELLOW, thickness=1 , fill=True )
-
- if self.msg_id == 0:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "WELCOM!", scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 1:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "remove %d"%self.msg_num, scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 2:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "pickup %d"%self.msg_num , scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 3:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "You Win" , scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 4:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "Game Over" , scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 5:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "move %d"%self.msg_num , scale=1,color=lcd.BLACK)
-
- elif self.msg_id == 6:
-
- self.imgBuf.draw_string(MSG_LEFT+ 0, MSG_TOP, "release %d"%self.msg_num , scale=1,color=lcd.BLACK)
-
-
- def update_screen(self):
-
- self.imgBuf.clear()
- self.imgBuf.draw_rectangle(0, 0, 320, 240 , color=lcd.WHITE, thickness=1 , fill=True )
-
-
-
-
- for i in range(5):
-
- self.imgBuf.draw_line( self.points[self.edges[i][0]][0], self.points[self.edges[i][0]][1], self.points[self.edges[i][3]][0], self.points[self.edges[i][3]][1], color=lcd.BLACK)
-
-
-
-
-
- for i in range(1,10+1):
-
- if self.positions[i] >= 0 :
-
-
- self.imgBuf.draw_circle(self.points[self.positions[i]][0], self.points[self.positions[i]][1], 11 , color=lcd.RED , thickness=1 , fill= True )
-
-
-
-
-
-
-
-
-
- self.update_message();
-
-
-
-
-
-
-
- self.imgBuf.draw_rectangle(MSG_LEFT - 6, MSG_TOP + 27, 43, 13 , color=lcd.YELLOW, thickness=1 , fill=True )
-
-
-
-
- self.imgBuf.draw_string( MSG_LEFT -4, MSG_TOP +30 -1, "replay", scale=1,color=lcd.PURPLE)
-
- self.imgBuf.draw_rectangle(MSG_LEFT -6+60, MSG_TOP + 27, 43, 13 , color=lcd.YELLOW, thickness=1 , fill=True )
- self.imgBuf.draw_string( MSG_LEFT -4+60, MSG_TOP +30 -1, "EXIT", scale=1,color=lcd.PURPLE)
-
-
-
- if 0 == self.selIDX :
-
- self.imgBuf.draw_rectangle( MSG_LEFT - 8 , MSG_TOP + 25 , 47, 17 ,1 , color=lcd.GREEN, thickness=2 , fill=False )
- elif self.selIDX >=1 and self.selIDX <=10 :
- j = self.selIDX - 1
- if 0 != self.pieces[j] :
- pass
- else:
-
-
-
-
- pass
-
-
-
-
- self.imgBuf.draw_circle(self.points[j][0], self.points[j][1], 15 , color=lcd.GREEN , thickness=2 , fill= False )
- elif self.selIDX == 10 +1 :
-
- self.imgBuf.draw_rectangle( MSG_LEFT - 8+60 , MSG_TOP + 25 , 47, 17 ,1 , color=lcd.GREEN, thickness=2 , fill=False )
- else:
- print('update_screen',self.selIDX,'__ERROR__')
-
-
- self.screen.display(self.imgBuf)
-
-
- def main_loop(self):
-
-
-
-
- self.update_screen()
-
-
-
- def prv_click(self):
-
- i = self.selIDX
- i-=1
- if i<0:
- i=self.btnCnt-1
- self.selIDX = i
- print('prv_click' , self.selIDX )
-
- def nxt_click(self):
-
- i = self.selIDX
- i+=1
- if i>=self.btnCnt:
- i=0
- self.selIDX = i
- print('nxt_click' , self.selIDX )
-
- def btn_so_click(self):
-
- print("当前状态",self.pieces,self.positions)
- flag = self.solution(self.pieces[::],self.positions[::],[])
- if type([])==type(flag):
- if len(flag)>0:
- show_msg("有解:%d->%d"%(flag[0][1],flag[0][2]))
- else:
- show_msg("已解")
- else:
- show_msg("无解")
- print("求解结束",flag)
-
-
-
-
-
-
- class appOnePlayer(OneApp,lucky9starClass):
- def setup(self, **kw):
- self.selIdx = 0
- super().setup(**kw)
- self.game = lucky9starClass()
- self.game.setScreen(self.data['screen'] )
-
- def rePaint(self):
- self.game.update_screen()
-
- def loop(self):
- OneApp.loop(self)
- try:
- self.game.main_loop()
-
- except:
- pass
- """
- cnt = self.data['cnt']
- if cnt<20:
- print('app[',self.name,'] cnt:',cnt,self.name)
- self.data['cnt']=cnt+1
- time.sleep(DEBUG_DELAY)
- return self.stat
- elif 20==cnt:
- print('app1 stop:',cnt)
- return self.doStop()
- """
-
- def onEvent(self,evtName,*args,**kw):
- print('appOnePlayer-onEvent',self.name , 'onEvent:',evtName,'*args',args,'**kw',kw)
- if 'btn1_pressed'==evtName:
- self.game.nxt_click()
- elif 'btn2_pressed'==evtName:
- b_not_exit = self.game.confirm_select()
- print( 'b_not_exit',b_not_exit )
- if not b_not_exit :
- print( self.name, 'self.doStop()' )
- self.doStop()
-
-
-
-
- class appSimple(OneApp):
- def loop(self):
- OneApp.loop(self)
- cnt = self.data['cnt']
- if cnt<10:
- print('app[',self.name,'] cnt:',cnt,self.name)
- self.data['cnt']=cnt+1
- time.sleep(DEBUG_DELAY)
- return self.stat
- elif 20==cnt:
- print('app1 stop:',cnt)
- return self.doStop()
-
- def onEvent(self,evtName,*args,**kw):
- print( 'appSimple-onEvent',self.name , 'onEvent:',evtName,'*args',args,'**kw',kw)
-
-
-
-
- class SysApp(OneApp):
- def setup(self, **kw):
- self.selIdx = 0
- super().setup(**kw)
- def loop(self):
- if self.stat == OneAppStats.RUNNING:
-
-
- self._paint()
- OneApp.loop(self)
- return self.stat
- def rePaint(self):
- cntChild=self._countChildApp()
-
- if cntChild ==0:
- screen = self.data['screen']
- img = image.Image()
- img.clear()
- img.draw_rectangle(0, 0, 320, 240 , color=lcd.WHITE, thickness=1 , fill=True )
- l,t=100,60
- img.draw_string(l , t + 0*60, "ONE PLAYER", scale=2,color=lcd.BLACK)
- img.draw_string(l , t + 1*60, "TWO PLAYER", scale=2,color=lcd.BLACK)
- img.draw_string(l , t + 2*60, "AI PLAYER", scale=2,color=lcd.BLACK)
- img.draw_rectangle(l-15, t + self.selIdx * 60 -5 , 150 , 5+ 24 +5 , color=lcd.BLACK, thickness=1 , fill=False )
- screen.display(img)
- else:
- for childrenApp in self.childApps:
- if childrenApp.stat == OneAppStats.RUNNING :
-
- childrenApp._paint()
- def onEvent(self,evtName,*args,**kw):
- print(self.name , 'onEvent:',evtName,'*args',args,'**kw',kw)
- if len(self.childApps)>0:
- print("super().onEvent(self,evtName,*args,**kw)")
- super().onEvent(self,evtName,*args,**kw)
- else:
- if evtName=='btn2_pressed':
- if self.selIdx == 0:
- app = self._startChildApp(appOnePlayer,'ONE PLAYER')
- app.setup(**{'cnt':0,'screen':self.data['screen']})
- print('start ONE PLAYER')
-
-
-
-
- elif self.selIdx == 1:
- app = self._startChildApp(appSimple,'TWO PLAYER')
- app.setup(**{'cnt':0,'screen':self.data['screen']})
- print('start TWO PLAYER')
-
-
-
-
- elif self.selIdx == 2:
- app = self._startChildApp(appSimple,'AI PLAYER')
- app.setup(**{'cnt':0,'screen':self.data['screen']})
- print('start AI PLAYER')
-
-
-
-
- else:
- print('error idx',self.selIdx)
- if evtName=='btn1_pressed':
- if self.selIdx==2:
- self.selIdx=0
- else:
- self.selIdx = self.selIdx +1
- print('self.selIdx',self.selIdx)
- utime.sleep_ms(300)
-
- def call_object_method(obj,method_name,*args,**kv):
- if method_name in dir(obj):
- getattr(obj,method_name)(method_name,*args,**kv)
-
- import lcd,image
- import utime
- from Maix import GPIO
- from fpioa_manager import fm
-
- lcd.init(type=1, freq=15000000, color=lcd.BLACK, invert = 0, lcd_type = 0)
- lcd.clear()
-
- root = SysApp('root')
- root.setup(**{'screen':lcd})
-
- fm.register(22, fm.fpioa.GPIOHS0)
- key22 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_NONE)
- fm.register(23, fm.fpioa.GPIOHS1)
- key23 = GPIO(GPIO.GPIOHS1, GPIO.IN, GPIO.PULL_NONE)
-
- last_ticks=0
-
- def test_irq22(pin_num):
- global last_ticks
-
-
- disable_buttons_irq()
- utime.sleep_ms(500)
- tmp_last_ticks = utime.ticks()
- print("key1?", pin_num,'val22',key22.value(),'val23',key23.value(), 'at',tmp_last_ticks,last_ticks,"\n")
- if last_ticks+500<tmp_last_ticks :
-
- root.onEvent('btn1_pressed',pin_num)
- last_ticks = tmp_last_ticks
-
-
- enable_buttons_irq()
-
- def test_irq23(pin_num):
- global last_ticks
-
-
- disable_buttons_irq()
- utime.sleep_ms(500)
- tmp_last_ticks = utime.ticks()
- print("key2?", pin_num,'val22',key22.value(),'val23',key23.value(), 'at',tmp_last_ticks,last_ticks,"\n")
- if last_ticks+500<tmp_last_ticks :
-
- root.onEvent('btn2_pressed',pin_num)
- last_ticks = tmp_last_ticks
-
-
- enable_buttons_irq()
-
-
- def enable_buttons_irq():
- key22.irq(test_irq22, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT,7)
- key23.irq(test_irq23, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT,7)
-
- def disable_buttons_irq():
- key22.disirq()
- key23.disirq()
-
- enable_buttons_irq()
-
- while root.stat==OneAppStats.RUNNING:
- print('root loop',root.loop() )
- time.sleep(DEBUG_DELAY)
- print('1111')
-
-
-
-
-
还是从这里进入。用绿色键选第一项:
然后按红色键进入游戏。绿色的圈表示当前选中的项目:
按绿色键,移动选到第行第2个。按红色键消去:
然后绿色键,移动选到第行第4个。按红色键选中后,再绿色键,移动到第行第2个。按红色键,就可以让右边的棋子跳到左边,同时消去第3个棋子:
以此类推,如果能够把棋子消除到只剩1个,就成功了。
顺便说一下,“replay”的作用是重置棋盘,“EXIT”是退出到上一级菜单。
|