原文链接:
彩色圆圈“连连看”
有个网友留言要把原文中的方块改成圆圈,再要加入消去的分数。大致效果如下:
以下就把原文的代码作几步简单的修改:
Box类的修改
class Box:
def __init__(self, x, y, w, h, color, batch=batch):
self.x, self.y, self.w, self.h = x, y, w, h
self.rect = shapes.Rectangle(x, y, w, h, color=color, batch=batch)
self.box = shapes.Box(x, y, w, h, color=Color('WHITE').rgba, thickness=3, batch=batch)
def hide(self):
self.box.batch = self.rect.batch = None
def show(self):
self.box.batch = self.rect.batch = batch
def on_mouse_over(self, x, y):
return self.x<=x<=self.x+self.w and self.y<=y<=self.y+self.h
把矩形及方框用圆圈和圆弧来代替:
self.rect = shapes.Circle(x+w//2, y+h//2, min(w,h)//2, color=color, batch=batch)
self.box = shapes.Arc(x+w//2, y+h//2, min(w,h)//2, color=Color('WHITE').rgba, batch=batch)
Game类的修改
Game类中增加分数属性:
class Game:
def __init__(self):
initMatrix(row, col)
self.score = 0
self.rc, self.rc2 = Point(), Point()
self.array, self.arces = Array, Boxes
update方法中增加分数和显示
def update(self, event):
clock.unschedule(self.update)
if self.last1.cir.color==self.last2.cir.color and matrix.connect(self.rc, self.rc2):
self.hide()
sound1.play()
self.score += 10
window.set_caption(window.caption.split('分数:')[0] + f'分数:{self.score}')
......
点击事件的修改
在on_mouse_press事件中增加分数的显示:
@window.event
def on_mouse_press(x, y, dx, dy):
global score
if (ret := game.on_mouse_click(x, y)):
window.set_caption(f'彩色方块连连看——坐标:{ret[0]} 颜色:{ret[1]} 分数:{game.score}')
部分代码的替代
在源码全文中搜索并替代: .rect