|
“彩色方块连连看”游戏(续)上期讲到相同的色块连接,链接见: ython一步一步教你用pyglet制作“彩色方块连连看”游戏-CSDN博客第八步续上期,接下来要实现相邻方块的连线:首先来进一步扩展行列的类:classRC: def__init__(self,r=0,c=0): self.r,self.c=r,c def__repr__(self): returnf'Rc({self.r},{self.c})' def__and__(self,other): returnself.r==other.randself.c==other.c def__or__(self,other): returnself.r==other.rorself.c==other.c def__eq__(self,other): returnself&other def__lt__(self,other): returnself.r==other.randself.c!=other.c def__gt__(self,other): returnself.r!=other.randself.c==other.c def__le__(self,other): returnself.r==other.randself.c-other.c def__ge__(self,other): returnself.c==other.candself.r-other.r def__xor__(self,other): returnselfother def__mod__(self,other): return[RC(self.r,other.c),RC(other.r,self.c)] def__truediv__(self,other): return1ifselfotherand(self>=other)=other)==1 def__sub__(self,other): ifselfother:return[RC(_,self.c)for_inrange(self.r+(self/other),other.r,self/other)] return[] def__mul__(self,other): ifselfother:returnnotany(Array[_+1][self.c+1]for_inrange(self.r+(self/other),other.r,self/other)) returnFalse由上面的类可知,self.rc*self.rc2就表示两点相邻,加时update方法中的if语句,就能实现相邻色块的连线并消去: defupdate(self,event): self.line.visible=False clock.unschedule(self.update) ifself.last.rect.color==self.last2.rect.colorandself.rc*self.rc2: self.last.hide();self.last2.hide() self.array[self.rc.r][self.rc.c]=self.array[self.rc2.r][self.rc2.c]=0 else: self.last.box.color=self.last2.box.color=Color('WHITE').rgba self.last,self.last2=None,None ifgame.success(): window.set_caption('彩色色块连连看——任务完成!') 代码:frompygletimport*fromcolorlibimport*W,H=800,600window=window.Window(W,H,caption='彩色色块连连看')gl.glClearColor(*Color('lightblue3').decimal)batch,group=graphics.Batch(),graphics.Group()row,col,space=6,8,5w,h=W//(col+2),H//(row+2)x0,y0=(W-(w+space)*col)//2,(H-(h+space)*row)//2COLOR=[]whilelen(COLOR)otherdef__mod__(self,other):return[RC(self.r,other.c),RC(other.r,self.c)]def__truediv__(self,other):return1ifselfotherand(self>=other)=other)==1def__sub__(self,other):ifselfother:return[RC(_,self.c)for_inrange(self.r+(self/other),other.r,self/other)]return[]def__mul__(self,other):ifselfother:returnnotany(Array[_+1][self.c+1]for_inrange(self.r+(self/other),other.r,self/other))returnFalseclassGame:def__init__(self):self.array=Arrayself.boxes=Boxesself.rc,self.rc2=RC(),RC()self.last,self.last2=None,Noneself.line=shapes.Line(0,0,0,0,width=5,color=Color('lightgold').rgba,batch=batch,group=group)self.line.visible=Falsedefon_mouse_click(self,x,y):ifself.line.visibleorself.success():returnr,c=(y-y0)//(h+space),(x-x0)//(w+space)ifrinrange(row)andcinrange(col)andself.boxes[r][c].on_mouse_over(x,y)andself.array[r][c]:ifself.lastisNoneandself.last2isNone:self.rc,self.last=RC(r,c),self.boxes[r][c]self.last.box.color=Color('RED').rgbaelifself.lastisnotNoneandself.last2isNone:self.rc2,self.last2=RC(r,c),self.boxes[r][c]self.last2.box.color=Color('RED').rgbaifself.rc==self.rc2:self.last.box.color=Color('WHITE').rgbaself.last,self.last2=None,Noneelse:self.line.x,self.line.y=self.getxy(r,c)self.line.x2,self.line.y2=self.getxy(self.rc.r,self.rc.c)self.line.visible=Trueclock.schedule_interval(self.update,0.3)return(r,c),Color(self.boxes[r][c].rect.color).namedefgetxy(self,row,col):returnx0+col*(w+space)+w//2,y0+row*(h+space)+h//2defupdate(self,event):self.line.visible=Falseclock.unschedule(self.update)ifsel
|
|