赞
踩
原文链接:
有个网友留言要把原文中的方块改成圆圈,再要加入消去的分数。大致效果如下:
以下就把原文的代码作几步简单的修改:
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类中增加分数属性:
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 替换为 .cir ; .box 替换为 .arc
class Box类名也可以修改一下,不作修改也不影响代码的运行。
大致就以上几步就完成了修改:
- from pyglet import *
- from colorlib import *
- from pointlib import Point
- from pyglet.window import key
-
- W, H = 800, 600
- window = window.Window(W, H)
- gl.glClearColor(*Color('lightblue3').decimal)
- batch, batch
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。