> 技术文档 > Python pyglet制作彩色圆圈“连连看”游戏_基于python的五彩连球游戏研究与开发

Python pyglet制作彩色圆圈“连连看”游戏_基于python的五彩连球游戏研究与开发

原文链接:

Python 一步一步教你用pyglet制作“彩色方块连连看”游戏(续)-CSDN博客文章浏览阅读1.6k次,点赞75次,收藏55次。上期讲到相同的色块连接,链接见: Python 一步一步教你用pyglet制作“彩色方块连连看”游戏-CSDN博客续上期,接下来要实现相邻方块的连线:首先来进一步扩展 行列的类......https://blog.csdn.net/boysoft2002/article/details/137063657

彩色圆圈“连连看”

有个网友留言要把原文中的方块改成圆圈,再要加入消去的分数。大致效果如下:

以下就把原文的代码作几步简单的修改:

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