python - kivy button text="" logic error -
i new python , kivy, trying learn python bymaking small minesweeper game, however, feel logic in below code correct, somehow seems not work: complete file follows:
from kivy.uix.button import button kivy.uix.gridlayout import gridlayout kivy.uix.boxlayout import boxlayout import random class spot(button): ''' classdocs ''' def __init__(self, **kwargs): ''' constructor ''' super(spot,self).__init__(**kwargs) self.ismine=false self.text="x" if 'id' in kwargs: self.id=kwargs['id'] #else: # self.text="x" class minesholder(gridlayout): def __init__(self,**kwargs): super(minesholder,self).__init__(**kwargs) class game(boxlayout): spots={} mines=[] def __init__(self,**kwargs): super(game,self).__init__(**kwargs) self.m=minesholder(rows=5, cols=5) self.add_widget(self.m) self.attachtogrid() def attachtogrid(self): self.m.clear_widgets() self.spots.clear() r in range(0,5): c in range(0,5): idd=str(r)+","+str(c) self.spots[idd]=idd s = spot(id=idd) self.m.add_widget(s) s.bind(on_press=self.spottouched) print(idd) self.createmines() def createmines(self): self.mines.clear() count=0 while (count <= 10): c=str(random.randint(0,4))+','+str(random.randint(0,4)) print(c) if self.mines.count(c)==0: self.mines.append(c) count+=1 def spottouched(self,spotted): #if self.mines.count(str(spotted.id))==1: # spotted.text="bomb" #else: spotted.text="" k in self.mines: if k==spotted.id: spotted.text="bomb" else: spotted.text="" the issue last 4 lines, when remove "spotted.text=""", code working perfect, when keep text="" code not working more, despite of having 11 bombs 1 getting detected, out text="", bombs getting detected correctly (text="bomb" works).
each time spottouched() called, loop through each mine , set text accordingly. let's have 2 bombs - let's call bombs ['bomb-a', 'bomb-b'].
now touch button id 'bomb-a'. spottouched() loops through mines. first mine in list 'bomb-a' - sets text "bomb". loops - second mine in list 'bomb-b', text set "". mine show "bomb" text last mine in list.
try instead:
def spottouched(self, spotted): spotted.text = "bomb" if spotted.id in self.mines else ""
Comments
Post a Comment