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

CSDN 2024-09-05 11:35:01 阅读 68

参考链接: 闲谈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库控制键盘,示例如下:

<code>import keyboard

def keys0():

print("Left key pressed")

def keys1():

print("Right key pressed")

def keys2():

print("Up key pressed")

def keys3():

print("Down key pressed")

def restart():

print("Enter key pressed")

# 添加热键

keyboard.add_hotkey('left', keys0)

keyboard.add_hotkey('right', keys1)

keyboard.add_hotkey('up', keys2)

keyboard.add_hotkey('down', keys3)

keyboard.add_hotkey('enter', restart)

# 等待用户按下esc键

print("Waiting for ESC to exit...")

keyboard.wait('esc')

# 在这里移除所有热键

print("Removing all hotkeys...")

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()添加的热键。


完整源代码

import keyboard

from random import sample

class Matrix:

def __init__(self, order=4):

self.over = False

self.order = order

self.matrix = self.new()

self.victory = False

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)]

def show(self):

if self.over or self.victory:

print('Enter to restart...')

else:

print(self)

print()

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))

def full(self):

return all(sum(self.matrix,[]))

def move(self, direction=0):

if self.over or self.victory: return

direction %= 4

if direction == 0: #left

if self.cannotmove(0): return

elif direction == 1: #right

if self.cannotmove(1): return

self.matrix = self.flipH()

elif direction == 2: #up

if self.cannotmove(2): return

self.matrix = self.rotL()

elif direction == 3: #down

if self.cannotmove(3): return

self.matrix = self.rotR()

for i,array in enumerate(self.matrix):

self.matrix[i] = Matrix.update(array)

if direction == 1:

self.matrix = self.flipH()

elif direction == 2:

self.matrix = self.rotR()

elif direction == 3:

self.matrix = self.rotL()

indexmax = max(sum(self.matrix,[]))

if self.order==2 and indexmax==4 or self.order==3 and indexmax==7 or indexmax==self.order+7:

self.victory = True

print(self)

print('Win! Enter to restart...')

return

self.insert()

self.over = self.cannotmove()

if self.over:

print(self)

print('Gave over!')

def cannotmove(self, direction = 4):

m, n = self.matrix, self.rotR()

p, q = self.rotL(), self.flipH()

if direction==0:

return all(n[0]) and Matrix.cannotupdate(m)

elif direction==1:

return all(n[-1]) and Matrix.cannotupdate(q)

elif direction==2:

return all(m[0]) and Matrix.cannotupdate(n)

elif direction==3:

return all(m[-1]) and Matrix.cannotupdate(p)

else:

return (self.full() and self.cannotmove(0) and self.cannotmove(1)

and self.cannotmove(2) and self.cannotmove(3))

def cannotupdate(matrix):

return all([m==Matrix.update(m) for m in matrix])

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)

def flipH(self):

m, n = self.matrix, self.order

return [[m[i][n-j-1] for j in range(n)] for i in range(n)]

def flipV(self):

m, n = self.matrix, self.order

return [[m[n-j-1][i] for i in range(n)] for j in range(n)]

def rotL(self):

m, n = self.matrix, self.order

return [[m[j][n-i-1] for j in range(n)] for i in range(n)]

def rotR(self):

m, n = self.matrix, self.order

return [[m[n-j-1][i] for j in range(n)] for i in range(n)]

def move(i):

mat.move(i)

mat.show()

def keys0():

move(0)

def keys1():

move(1)

def keys2():

move(2)

def keys3():

move(3)

def restart():

global mat

if mat.victory:

mat.order += 1

mat = Matrix(mat.order)

mat.show()

if mat.over:

if mat.order>3:

mat.order -= 1

mat = Matrix(mat.order)

mat.show()

if __name__ == '__main__':

print("《2048小游戏》")

print("上下左右前头控制方向,按ESC退出...")

mat = Matrix(2)

mat.show()

keyboard.add_hotkey('left', keys0)

keyboard.add_hotkey('right', keys1)

keyboard.add_hotkey('up', keys2)

keyboard.add_hotkey('down', keys3)

keyboard.add_hotkey('enter', restart)

keyboard.wait('esc') # 等待用户按下esc键退出

print('bye!')

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了,如果三阶方阵过关会自动升级到四阶方阵,以此类推。


本文完



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。