当前位置:   article > 正文

python 小游戏《2048》字符版非图形界面

python 小游戏《2048》字符版非图形界面

参考链接: 闲谈2048小游戏和数组的旋转及翻转和转置

目录

2048 

一、方阵类

二、随机插入1或2

三、 合并和递增

四、 判断和移动

五、 键盘控制

完整源代码

玩法过程


2048 

上回说到2048小游戏中数组的各种旋转、翻转的方法,就是为代码编程作准备的;有了这些再就加上二维数组各行列上元素的合并、能否被合并的判断、成功失败的判断等等;以及再加上键盘按键的控制,小游戏就基本完成了。

一、方阵类

方阵就是高宽相同的矩阵,2048用方阵就行了,写代码也省事一点,方阵的类如下:

>>> from random import sample
>>> class Matrix:
...     def __init__(self, order=4):
...         self.order = order
...         self.matrix = self.new()
...     def __repr__(self):
...         m, n = [], len(str(2**max(sum(self.matrix,[]))))
...         for mat in self.matrix:
...             m.append(', '.join(f'{2**x if x else 0:>{n}}' for x in mat))
...         return '],\n ['.join(m).join(['[[',']]'])
...     def new(self):
...         n = self.order
...         m = sample([0]*(n*n-2)+sample([0,1,1],2),n*n)
...         return  [m[i*n:i*n+n] for i in range(n)]
... 
...     
>>> Matrix()
[[0, 0, 2, 0],
 [0, 0, 0, 0],
 [0, 0, 2, 0],
 [0, 0, 0, 0]]
>>> Matrix()
[[0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 2, 0],
 [0, 0, 0, 0]]

在方阵中随机产生1~2个1,sample([0,1,1],2) 生成的1个还是2个,比例为2:1;

在__repr__方法中显示时,这些1作为2的指数,所以显示为2^1=2。

二、随机插入数字

    def insert(self):
        n = self.order
        m = [i for i,n in enumerate(sum(self.matrix,[])) if not n]
        if m:
            i = sample(m, 1)[0]
            self.matrix[i//n][i%n] = sum(sample([1,1,1,2],1))

或者:

    def insert(self):
        n = self.order
        m = [(i,j) for j in range(n) for i in range(n) if not self.matrix[i][j]]
        if m:
            i, j = sample(m, 1)[0]
            self.matrix[i][j] = sum(sample([1,1,1,2],1))

为加快数字的拼合速度,从5队开始,除了只插入1和2,可以考虑加入更大的数字:

self.matrix[i][j] = sum(sample([1,1,1,2]*3+([1,2,3,3]+[i for i in range(n+4,n,-1)] if n>4 else []),1))

如 n = 5 时,拟插入的各数字比例为:

>>> n = 5
>>> t = [1,1,1,2]*3+[1,2,3,3]+[i for i in range(n+4,n,-1)]
>>> for i in set(t):
...     print([i,t.count(i)/20])
... 
...     
[1, 0.5]
[2, 0.2]
[3, 0.1]
[6, 0.05]
[7, 0.05]
[8, 0.05]
[9, 0.05] 

三、 合并和递增

   ......     
   for i,array in enumerate(self.matrix):
            self.matrix[i] = Matrix.update(array)
    ......
    def update(array):
        split = lambda a: [_ for _ in a if _]+[_ for _ in a if not _]
        array = split(array)
        for i,a in enumerate(array):
            if i and a and a==array[i-1]:
                array[i-1] += 1
                array[i] = 0
        return split(array)

四、 判断和移动

略……写得有点复杂,可以到完整代码中阅读。

五、 键盘控制

引入keyboard库控制键盘,示例如下:

  1. import keyboard
  2. def keys0():
  3. print("Left key pressed")
  4. def keys1():
  5. print("Right key pressed")
  6. def keys2():
  7. print("Up key pressed")
  8. def keys3():
  9. print("Down key pressed")
  10. def restart():
  11. print("Enter key pressed")
  12. # 添加热键
  13. keyboard.add_hotkey('left', keys0)
  14. keyboard.add_hotkey('right', keys1)
  15. keyboard.add_hotkey('up', keys2)
  16. keyboard.add_hotkey('down', keys3)
  17. keyboard.add_hotkey('enter', restart)
  18. # 等待用户按下esc键
  19. print("Waiting for ESC to exit...")
  20. keyboard.wait('esc')
  21. # 在这里移除所有热键
  22. print("Removing all hotkeys...")
  23. keyboard.unhook_all_hotkeys()

Waiting for ESC to exit...
Left key pressed
Right key pressed
Up key pressed
Down key pressed
Enter key pressed
Removing all hotkeys...
>>> 

注:最后一行代码keyboard.unhook_all_hotkeys()很关键,一定都有否则即使按了ESC键退出程序,热键还是驻留在内存里,键盘还会响应keyboard.add_hotkey()添加的热键。


完整源代码

  1. import keyboard
  2. from random import sample
  3. class Matrix:
  4. def __init__(self, order=4):
  5. self.over = False
  6. self.order = order
  7. self.matrix = self.new()
  8. self.victory = False
  9. def __repr__(self):
  10. m, n = [], len(str(2**max(sum(self.matrix,[]))))
  11. for mat in self.matrix:
  12. m.append(', '.join(f'{2**x if x else 0:>{n}}' for x in mat))
  13. return '],\n ['.join(m).join(['[[',']]'])
  14. def new(self):
  15. n = self.order
  16. m = sample([0]*(n*n-2)+sample([0,1,1],2),n*n)
  17. return [m[i*n:i*n+n] for i in range(n)]
  18. def show(self):
  19. if self.over or self.victory:
  20. print('Enter to restart...')
  21. else:
  22. print(self)
  23. print()
  24. def insert(self):
  25. n = self.order
  26. m = [(i,j) for j in range(n) for i in range(n) if not self.matrix[i][j]]
  27. if m:
  28. i, j = sample(m, 1)[0]
  29. self.matrix[i][j] = sum(sample([1,1,1,2],1))
  30. def full(self):
  31. return all(sum(self.matrix,[]))
  32. def move(self, direction=0):
  33. if self.over or self.victory: return
  34. direction %= 4
  35. if direction == 0: #left
  36. if self.cannotmove(0): return
  37. elif direction == 1: #right
  38. if self.cannotmove(1): return
  39. self.matrix = self.flipH()
  40. elif direction == 2: #up
  41. if self.cannotmove(2): return
  42. self.matrix = self.rotL()
  43. elif direction == 3: #down
  44. if self.cannotmove(3): return
  45. self.matrix = self.rotR()
  46. for i,array in enumerate(self.matrix):
  47. self.matrix[i] = Matrix.update(array)
  48. if direction == 1:
  49. self.matrix = self.flipH()
  50. elif direction == 2:
  51. self.matrix = self.rotR()
  52. elif direction == 3:
  53. self.matrix = self.rotL()
  54. indexmax = max(sum(self.matrix,[]))
  55. if self.order==2 and indexmax==4 or self.order==3 and indexmax==7 or indexmax==self.order+7:
  56. self.victory = True
  57. print(self)
  58. print('Win! Enter to restart...')
  59. return
  60. self.insert()
  61. self.over = self.cannotmove()
  62. if self.over:
  63. print(self)
  64. print('Gave over!')
  65. def cannotmove(self, direction = 4):
  66. m, n = self.matrix, self.rotR()
  67. p, q = self.rotL(), self.flipH()
  68. if direction==0:
  69. return all(n[0]) and Matrix.cannotupdate(m)
  70. elif direction==1:
  71. return all(n[-1]) and Matrix.cannotupdate(q)
  72. elif direction==2:
  73. return all(m[0]) and Matrix.cannotupdate(n)
  74. elif direction==3:
  75. return all(m[-1]) and Matrix.cannotupdate(p)
  76. else:
  77. return (self.full() and self.cannotmove(0) and self.cannotmove(1)
  78. and self.cannotmove(2) and self.cannotmove(3))
  79. def cannotupdate(matrix):
  80. return all([m==Matrix.update(m) for m in matrix])
  81. def update(array):
  82. split = lambda a: [_ for _ in a if _]+[_ for _ in a if not _]
  83. array = split(array)
  84. for i,a in enumerate(array):
  85. if i and a and a==array[i-1]:
  86. array[i-1] += 1
  87. array[i] = 0
  88. return split(array)
  89. def flipH(self):
  90. m, n = self.matrix, self.order
  91. return [[m[i][n-j-1] for j in range(n)] for i in range(n)]
  92. def flipV(self):
  93. m, n = self.matrix, self.order
  94. return [[m[n-j-1][i] for i in range(n)] for j in range(n)]
  95. def rotL(self):
  96. m, n = self.matrix, self.order
  97. return [[m[j][n-i-1] for j in range(n)] for i in range(n)]
  98. def rotR(self):
  99. m, n = self.matrix, self.order
  100. return [[m[n-j-1][i] for j in range(n)] for i in range(n)]
  101. def move(i):
  102. mat.move(i)
  103. mat.show()
  104. def keys0():
  105. move(0)
  106. def keys1():
  107. move(1)
  108. def keys2():
  109. move(2)
  110. def keys3():
  111. move(3)
  112. def restart():
  113. global mat
  114. if mat.victory:
  115. mat.order += 1
  116. mat = Matrix(mat.order)
  117. mat.show()
  118. if mat.over:
  119. if mat.order>3:
  120. mat.order -= 1
  121. mat = Matrix(mat.order)
  122. mat.show()
  123. if __name__ == '__main__':
  124. print("《2048小游戏》")
  125. print("上下左右前头控制方向,按ESC退出...")
  126. mat = Matrix(2)
  127. mat.show()
  128. keyboard.add_hotkey('left', keys0)
  129. keyboard.add_hotkey('right', keys1)
  130. keyboard.add_hotkey('up', keys2)
  131. keyboard.add_hotkey('down', keys3)
  132. keyboard.add_hotkey('enter', restart)
  133. keyboard.wait('esc') # 等待用户按下esc键退出
  134. print('bye!')
  135. keyboard.unhook_all_hotkeys() # 退出后移除所有热键

玩法过程

《2048小游戏》
上下左右前头控制方向,按ESC退出...
[[0, 0],
 [0, 2]]

[[0, 0],
 [2, 2]]

[[0, 4],
 [0, 4]]

[[0, 2],
 [0, 8]]

[[2, 2],
 [8, 0]]

[[4, 2],
 [8, 0]]

[[4, 4],
 [8, 2]]

[[8, 2],
 [8, 2]]

[[ 0,  0],
 [16,  4]]
Win! Enter to restart...
Enter to restart...
[[0, 2, 0],
 [0, 0, 2],
 [0, 0, 0]]

[[0, 0, 0],
 [2, 0, 0],
 [0, 2, 2]]

[[4, 0, 0],
 [0, 0, 2],
 [0, 0, 4]]

[[0, 2, 0],
 [0, 0, 2],
 [4, 0, 4]]

[[0, 0, 2],
 [0, 0, 2],
 [4, 0, 8]]

[[0, 0, 2],
 [0, 2, 2],
 [0, 4, 8]]

[[0, 0, 0],
 [0, 2, 4],
 [2, 4, 8]]

[[2, 0, 0],
 [2, 4, 0],
 [2, 4, 8]]

[[2, 0, 0],
 [2, 0, 0],
 [4, 8, 8]]

[[ 0,  2,  2],
 [ 0,  0,  2],
 [ 0,  4, 16]]

[[ 0,  0,  0],
 [ 2,  2,  4],
 [ 0,  4, 16]]

[[ 0,  0,  0],
 [ 0,  4,  4],
 [ 4,  4, 16]]

[[ 2,  0,  0],
 [ 0,  0,  4],
 [ 4,  8, 16]]

[[ 2,  0,  0],
 [ 2,  0,  4],
 [ 4,  8, 16]]

[[ 2,  0,  0],
 [ 4,  0,  4],
 [ 4,  8, 16]]

[[ 0,  4,  0],
 [ 2,  0,  4],
 [ 8,  8, 16]]

[[ 0,  2,  4],
 [ 0,  2,  4],
 [ 0, 16, 16]]

[[ 2,  2,  4],
 [ 0,  2,  4],
 [ 0,  0, 32]]

[[ 0,  0,  0],
 [ 2,  0,  8],
 [ 2,  4, 32]]

[[ 0,  0,  0],
 [ 2,  0,  8],
 [ 4,  4, 32]]

[[ 0,  0,  2],
 [ 0,  2,  8],
 [ 0,  8, 32]]

[[ 0,  0,  2],
 [ 0,  2,  8],
 [ 2,  8, 32]]

[[ 2,  0,  4],
 [ 2,  8,  0],
 [ 2,  8, 32]]

[[ 0,  0,  2],
 [ 2,  0,  4],
 [ 4, 16, 32]]

[[ 2,  0,  2],
 [ 2,  4,  0],
 [ 4, 16, 32]]

[[ 0,  0,  4],
 [ 4,  4,  2],
 [ 4, 16, 32]]

[[ 2,  0,  4],
 [ 0,  4,  2],
 [ 8, 16, 32]]

[[ 2,  2,  4],
 [ 0,  4,  2],
 [ 8, 16, 32]]

[[ 0,  4,  4],
 [ 2,  4,  2],
 [ 8, 16, 32]]

[[ 2,  0,  4],
 [ 2,  8,  2],
 [ 8, 16, 32]]

[[ 0,  2,  4],
 [ 4,  8,  2],
 [ 8, 16, 32]]

[[ 2,  4,  2],
 [ 4,  8,  2],
 [ 8, 16, 32]]

[[ 2,  4,  2],
 [ 4,  8,  4],
 [ 8, 16, 32]]
Gave over!
Enter to restart...

注:二阶方阵玩出16就过关,三阶方阵玩出128就过关,四阶就要到2048才能过关,五阶则要到4096才过关,六阶以上类推。演示时玩到三阶方阵就over了,如果三阶方阵过关会自动升级到四阶方阵,以此类推。


本文完

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/191936
推荐阅读
相关标签
  

闽ICP备14008679号