当前位置:   article > 正文

Python 一步一步教你用pyglet制作“彩色方块连连看”游戏(续)_python彩色

python彩色

“彩色方块连连看”游戏(续)

上期讲到相同的色块连接,链接见: Python 一步一步教你用pyglet制作“彩色方块连连看”游戏-CSDN博客

第八步

续上期,接下来要实现相邻方块的连线:

首先来进一步扩展 行列的类:

class RC:
    def __init__(self, r=0, c=0):
        self.r, self.c = r, c
    def __repr__(self):
        return f'Rc({self.r}, {self.c})'
    def __and__(self, other):
        return self.r == other.r and self.c == other.c
    def __or__(self, other):
        return self.r == other.r or self.c == other.c
    def __eq__(self, other):
        return self & other
    def __lt__(self, other):
        return self.r == other.r and self.c != other.c
    def __gt__(self, other):
        return self.r != other.r and self.c == other.c
    def __le__(self, other):
        return self.r == other.r and self.c - other.c
    def __ge__(self, other):
        return self.c == other.c and self.r - other.r
    def __xor__(self, other):
        return self < other or self > other
    def __mod__(self, other):
        return [RC(self.r, other.c), RC(other.r, self.c)]
    def __truediv__(self, other):
        return 1 if self<other and (self<=other)<0 or self>other and (self>=other)<0 else -1
    def __add__(self, other):
        return abs(self<=other)==1 or abs(self>=other)==1
    def __sub__(self, other):
        if self<other: return [RC(self.r,_) for _ in range(self.c+(self/other),other.c,self/other)]
        if self>other: return [RC(_,self.c) for _ in range(self.r+(self/other),other.r,self/other)]
        return []
    def __mul__(self, other):
        if self<other: return not any(Array[self.r+1][_+1] for _ in range(self.c+(self/other),other.c,self/other))
        if self>other: return not any(Array[_+1][self.c+1] for _ in range(self.r+(self/other),other.r,self/other))
        return False

由上面的类可知,self.rc*self.rc2就表示两点相邻,加时update方法中的if语句,就能实现相邻色块的连线并消去:

    def update(self, event):
        self.line.visible = False
        clock.unschedule(self.update)
        if self.last.rect.color==self.last2.rect.color and self.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
        if game.success():
            window.set_caption('彩色色块连连看——任务完成!') 

代码:

  1. from pyglet import *
  2. from colorlib import *
  3. W, H = 800, 600
  4. window = window.Window(W, H, caption='彩色色块连连看')
  5. gl.glClearColor(*Color('lightblue3').decimal)
  6. batch, group = graphics.Batch(),graphics.Group()
  7. row, col, space = 6, 8, 5
  8. w, h = W//(col+2), H//(row+2)
  9. x0, y0 = (W-(w+space)*col)//2, (H-(h+space)*row)//2
  10. COLOR = []
  11. while len(COLOR)<row*col//4:
  12. if (c:=randcolorTuple()) not in COLOR:
  13. COLOR.append(c)
  14. COLOR = sample(COLOR*4, row*col)
  15. Array, Boxes = [[[1]*col for _ in range(row)] for _ in range(2)]
  16. class Box:
  17. def __init__(self, x, y, w, h, color, batch=batch):
  18. self.x, self.y, self.w, self.h = x, y, w, h
  19. self.rect = shapes.Rectangle(x, y, w, h, color=color, batch=batch)
  20. self.box = shapes.Box(x, y, w, h, color=Color('WHITE').rgba, thickness=3, batch=batch)
  21. self.box.group = group
  22. def hide(self):
  23. self.box.batch = self.rect.batch = None
  24. def on_mouse_over(self, x, y):
  25. return self.x<=x<=self.x+self.w and self.y<=y<=self.y+self.h
  26. for r,arr in enumerate(Boxes):
  27. for c,_ in enumerate(arr):
  28. Boxes[r][c] = Box(x0+c*(w+space), y0+r*(h+space), w, h, COLOR[c+r*len(arr)])
  29. class RC:
  30. def __init__(self, r=0, c=0):
  31. self.r, self.c = r, c
  32. def __repr__(self):
  33. return f'Rc({self.r}, {self.c})'
  34. def __and__(self, other):
  35. return self.r == other.r and self.c == other.c
  36. def __or__(self, other):
  37. return self.r == other.r or self.c == other.c
  38. def __eq__(self, other):
  39. return self & other
  40. def __lt__(self, other):
  41. return self.r == other.r and self.c != other.c
  42. def __gt__(self, other):
  43. return self.r != other.r and self.c == other.c
  44. def __le__(self, other):
  45. return self.r == other.r and self.c - other.c
  46. def __ge__(self, other):
  47. return self.c == other.c and self.r - other.r
  48. def __xor__(self, other):
  49. return self < other or self > other
  50. def __mod__(self, other):
  51. return [RC(self.r, other.c), RC(other.r, self.c)]
  52. def __truediv__(self, other):
  53. return 1 if self<other and (self<=other)<0 or self>other and (self>=other)<0 else -1
  54. def __add__(self, other):
  55. return abs(self<=other)==1 or abs(self>=other)==1
  56. def __sub__(self, other):
  57. if self<other: return [RC(self.r,_) for _ in range(self.c+(self/other),other.c,self/other)]
  58. if self>other: return [RC(_,self.c) for _ in range(self.r+(self/other),other.r,self/other)]
  59. return []
  60. def __mul__(self, other):
  61. if self<other: return not any(Array[self.r+1][_+1] for _ in range(self.c+(self/other),other.c,self/other))
  62. if self>other: return not any(Array[_+1][self.c+1] for _ in range(self.r+(self/other),other.r,self/other))
  63. return False
  64. class Game:
  65. def __init__(self):
  66. self.array = Array
  67. self.boxes = Boxes
  68. self.rc, self.rc2 = RC(), RC()
  69. self.last, self.last2 = None, None
  70. self.line = shapes.Line(0, 0, 0, 0, width=5, color=Color('light gold').rgba, batch=batch, group=group)
  71. self.line.visible = False
  72. def on_mouse_click(self, x, y):
  73. if self.line.visible or self.success(): return
  74. r, c = (y-y0)//(h+space), (x-x0)//(w+space)
  75. if r in range(row) and c in range(col) and self.boxes[r][c].on_mouse_over(x, y) and self.array[r][c]:
  76. if self.last is None and self.last2 is None:
  77. self.rc, self.last = RC(r, c), self.boxes[r][c]
  78. self.last.box.color = Color('RED').rgba
  79. elif self.last is not None and self.last2 is None:
  80. self.rc2, self.last2 = RC(r, c), self.boxes[r][c]
  81. self.last2.box.color = Color('RED').rgba
  82. if self.rc == self.rc2:
  83. self.last.box.color = Color('WHITE').rgba
  84. self.last, self.last2 = None, None
  85. else:
  86. self.line.x, self.line.y = self.getxy(r, c)
  87. self.line.x2, self.line.y2 = self.getxy(self.rc.r, self.rc.c)
  88. self.line.visible = True
  89. clock.schedule_interval(self.update, 0.3)
  90. return (r, c), Color(self.boxes[r][c].rect.color).name
  91. def getxy(self, row, col):
  92. return x0+col*(w+space)+w//2, y0+row*(h+space)+h//2
  93. def update(self, event):
  94. self.line.visible = False
  95. clock.unschedule(self.update)
  96. if self
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/788525
推荐阅读
相关标签
  

闽ICP备14008679号