简单的2048小游戏
不多说,直接上图,这里并未实现GUI之类的,需要的话,可自行实现:
接下来就是代码模块,其中的2048游戏原来网络上有很多,我就不详细写上去了,都写在注释里面了。唯一要注意的就是需要先去了解一下矩阵的转置,这里会用到
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
import random board = [[ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ]] # 打印游戏界面 def display(board, score): print ( '{0:4} {1:4} {2:4} {3:4}' . format (board[ 0 ][ 0 ], board[ 0 ][ 1 ], board[ 0 ][ 2 ], board[ 0 ][ 3 ])) print ( '{0:4} {1:4} {2:4} {3:4}' . format (board[ 1 ][ 0 ], board[ 1 ][ 1 ], board[ 1 ][ 2 ], board[ 1 ][ 3 ])) print ( '{0:4} {1:4} {2:4} {3:4}' . format (board[ 2 ][ 0 ], board[ 2 ][ 1 ], board[ 2 ][ 2 ], board[ 2 ][ 3 ])) print ( '{0:4} {1:4} {2:4} {3:4}' . format (board[ 3 ][ 0 ], board[ 3 ][ 1 ], board[ 3 ][ 2 ], board[ 3 ][ 3 ]), ' 分数:' , score) # 初始化游戏,在4*4里面随机生成两个2 def init(board): # 游戏先都重置为0 for i in range ( 4 ): for j in range ( 4 ): board[i][j] = 0 # 随机生成两个2保存的位置 randomposition = random.sample( range ( 0 , 15 ), 2 ) board[ int (randomposition[ 0 ] / 4 )][randomposition[ 0 ] % 4 ] = 2 board[ int (randomposition[ 1 ] / 4 )][randomposition[ 1 ] % 4 ] = 2 def addSameNumber(boardList, direction): '''需要在列表中查找相邻相同的数字相加,返回增加的分数 :param boardList: 经过对齐非零的数字处理过后的二维数组 :param direction: direction == 'left'从右向左查找,找到相同且相邻的两个数字,左侧数字翻倍,右侧数字置0 direction == 'right'从左向右查找,找到相同且相邻的两个数字,右侧数字翻倍,左侧数字置0 :return: ''' addNumber = 0 # 向左以及向上的操作 if direction = = 'left' : for i in [ 0 , 1 , 2 ]: if boardList[i] = = boardList[i + 1 ] ! = 0 : boardList[i] * = 2 boardList[i + 1 ] = 0 addNumber + = boardList[i] return { 'continueRun' : True , 'addNumber' : addNumber} return { 'continueRun' : False , 'addNumber' : addNumber} # 向右以及向下的操作 else : for i in [ 3 , 2 , 1 ]: if boardList[i] = = boardList[i - 1 ] ! = 0 : boardList[i] * = 2 boardList[i - 1 ] = 0 addNumber + = boardList[i] return { 'continueRun' : True , 'addNumber' : addNumber} return { 'continueRun' : False , 'addNumber' : addNumber} def align(boardList, direction): '''对齐非零的数字 direction == 'left':向左对齐,例如[8,0,0,2]左对齐后[8,2,0,0] direction == 'right':向右对齐,例如[8,0,0,2]右对齐后[0,0,8,2] ''' # 先移除列表里面的0,如[8,0,0,2]->[8,2],1.先找0的个数,然后依照个数进行清理 # boardList.remove(0):移除列表中的某个值的第一个匹配项,所以[8,0,0,2]会移除两次0 for x in range (boardList.count( 0 )): boardList.remove( 0 ) # 移除的0重新补充回去,[8,2]->[8,2,0,0] if direction = = 'left' : boardList.extend([ 0 for x in range ( 4 - len (boardList))]) else : boardList[: 0 ] = [ 0 for x in range ( 4 - len (boardList))] def handle(boardList, direction): ''' 处理一行(列)中的数据,得到最终的该行(列)的数字状态值, 返回得分 :param boardList: 列表结构,存储了一行(列)中的数据 :param direction: 移动方向,向上和向左都使用方向'left',向右和向下都使用'right' :return: 返回一行(列)处理后加的分数 ''' addscore = 0 # 先处理数据,把数据都往指定方向进行运动 align(boardList, direction) result = addSameNumber(boardList, direction) # 当result['continueRun'] 为True,代表需要再次执行 while result[ 'continueRun' ]: # 重新对其,然后重新执行合并,直到再也无法合并为止 addscore + = result[ 'addNumber' ] align(boardList, direction) result = addSameNumber(boardList, direction) # 直到执行完毕,及一行的数据都不存在相同的 return { 'addscore' : addscore} # 游戏操作函数,根据移动方向重新计算矩阵状态值,并记录得分 def operator(board): # 每一次的操作所加的分数,以及操作后游戏是否触发结束状态(即数据占满位置) addScore = 0 gameOver = False # 默认向左 direction = 'left' op = input ( "请输入您的操作:" ) if op in [ 'a' , 'A' ]: # 方向向左 direction = 'left' # 一行一行进行处理 for row in range ( 4 ): addScore + = handle(board[row], direction)[ 'addscore' ] elif op in [ 'd' , 'D' ]: direction = 'right' for row in range ( 4 ): addScore + = handle(board[row], direction)[ 'addscore' ] elif op in [ 'w' , 'W' ]: # 向上相当于向左的转置处理 direction = 'left' board = list ( map ( list , zip ( * board))) # 一行一行进行处理 for row in range ( 4 ): addScore + = handle(board[row], direction)[ 'addscore' ] board = list ( map ( list , zip ( * board))) elif op in [ 's' , 'S' ]: # 向下相当于向右的转置处理 direction = 'right' board = list ( map ( list , zip ( * board))) # 一行一行进行处理 for row in range ( 4 ): addScore + = handle(board[row], direction)[ 'addscore' ] board = list ( map ( list , zip ( * board))) else : print ( "错误输入!请输入[W, S, A, D]或者对应小写" ) return { 'gameOver' : gameOver, 'addScore' : addScore, 'board' : board} # 每一次操作后都需要判断0的数量,如果满了,则游戏结束 number_0 = 0 for q in board: # count(0)是指0出现的个数,是扫描每一行的 number_0 + = q.count( 0 ) # 如果number_0为0,说明满了 if number_0 = = 0 : gameOver = True return { 'gameOver' : gameOver, 'addScore' : addScore, 'board' : board} # 说明还没有满,则在空的位置上加上一个2或者4,概率为3:1 else : addnum = random.choice([ 2 , 2 , 2 , 4 ]) position_0_list = [] # 找出0的位置,并保存起来 for i in range ( 4 ): for j in range ( 4 ): if board[i][j] = = 0 : position_0_list.append(i * 4 + j) # 在刚才记录的0的位置里面随便找一个,然后替换成生成的2或者4 randomposition = random.sample(position_0_list, 1 ) board[ int (randomposition[ 0 ] / 4 )][randomposition[ 0 ] % 4 ] = addnum return { 'gameOver' : gameOver, 'addScore' : addScore, 'board' : board} if __name__ = = '__main__' : print ( '输入:W(上) S(下) A(左) D(右).' ) # 初始化游戏界面,游戏分数 gameOver = False init(board) score = 0 # 游戏未结束,则一直运行 while gameOver ! = True : display(board, score) operator_result = operator(board) board = operator_result[ 'board' ] if operator_result[ 'gameOver' ] = = True : print ( "游戏结束,你输了!" ) print ( "你的最终得分:" , score) gameOver = operator_result[ 'gameOver' ] break else : # 加上这一步的分 score + = operator_result[ 'addScore' ] if score > = 2048 : print ( "牛啊牛啊,你吊竟然赢了!" ) print ( "你的最终得分:" , score) # 结束游戏 gameOver = True break |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_38778769/article/details/117285796