-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy path2048小游戏.py
265 lines (217 loc) · 5.83 KB
/
2048小游戏.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
import curses
import copy
from curses import wrapper
import os
stdscr = curses.initscr()
# 分数
score = 0
# 判断是否获胜
win = 0
#
FILENAME = 'out.npy'
# 是否更改棋盘
# 初始化
def init():
# 初始化棋盘
# 初始棋盘 2 或 4 的随机数字
if FILENAME not in os.listdir():
np.save(FILENAME, 0)
init_board = choice(np.zeros((4, 4), dtype=np.int))
return init_board
# 游戏进程
def game(board, stdscr, rscore):
global score
global change
# curses.noecho()
# 屏幕不显示用户输入的字符
curses.noecho()
while 1:
# stdscr.getch()
# 读取用户输入的字符
order = stdscr.getch()
# move()对用户移动的响应
current_board, change = move(order, board)
# change 为 1 随机产生 2 或 4
if change:
current_board = choice(board)
# 打印棋盘
print_board(stdscr, current_board, rscore)
# 当棋盘被填满,判断是否游戏结束
if (current_board != 0).all():
fail(current_board)
# win 为 1 打印获胜提示
if win:
stdscr.addstr('You win')
# 随机产生 2 或 4
def choice(board):
udict = {}
# 统计0的个数
count = 0
for i in range(4):
for j in range(4):
# board[i,j] 为 0
# eg. {0:(1,3),1:(2,1),3:(3,2)}
# 根据 key 可以获得元素 0 在棋盘上的位置
if not board[i, j]:
udict[count] = (i, j)
count += 1
# np.random.randint(0, count)
# 产生 [0,count) 范围内的随机数
random_number = np.random.randint(0, count)
# np.random.choice([2,2,2,4])
# 随机选取列表 [2,2,2,4] 中的元素
two_or_four = np.random.choice([2, 2, 2, 4])
# 更改棋盘上 0 元素为随机数
board[udict[random_number]] = two_or_four
return board
# 基础移动
def basic(board):
global score
global win
# 以右移为基础移动
for i in range(4):
flag = 1
while flag:
flag = 0
j = 2
while j >= 0:
if board[i, j] != 0:
if board[i, j + 1] == board[i, j]:
board[i, j + 1] = 2 * board[i, j]
if board[i, j + 1] == 2048:
win = 1
board[i, j] = 0
score += 100
flag = 1
elif board[i, j + 1] == 0:
temp = board[i, j]
board[i, j] = board[i, j + 1]
board[i, j + 1] = temp
flag = 1
j -= 1
return board
# 右移
def move_right(board):
return basic(board)
# 上移
def move_up(board):
# 逆置 + 转置
board = board[::-1, ::-1].T
board = basic(board)
board = board[::-1, ::-1].T
return board
# 左移
def move_left(board):
# 逆置
board = board[::-1, ::-1]
board = basic(board)
board = board[::-1, ::-1]
return board
# 下移
def move_down(board):
# 转置
board = board.T
board = basic(board)
board = board.T
return board
# 移动
def move(order, board):
# ord 求码值
global score
global win
change = 1
tempboard = copy.deepcopy(board)
# 退出游戏
if order == ord('q'):
save_score(score)
exit()
# 重置游戏
elif order == ord('r'):
win = 0
save_score(score)
score = 0
stdscr.clear()
wrapper(main)
# 胜利后,只有退出和重置游戏
elif win:
change = 0
newboard = tempboard
return newboard, change
# 上下左右移动
elif order == ord('w'):
newboard = move_up(board)
elif order == ord('s'):
newboard = move_down(board)
elif order == ord('a'):
newboard = move_left(board)
elif order == ord('d'):
newboard = move_right(board)
# 按其他键程序不响应
else:
newboard = board
if (newboard == tempboard).all():
change = 0
return newboard, change
# 游戏失败
def fail(board):
# 判断是否失败
global score
diff1 = board[:, 1:] - board[:, :-1]
diff2 = board[1:, :] - board[:-1, :]
inter = (diff1 != 0).all() and (diff2 != 0).all()
if inter:
save_score(score)
stdscr.addstr('You lose')
# # 排行榜
# def ranking(score):
# rank_score = np.load(FILENAME)
# if rank_score < score:
# rank_score = score
# np.save(FILENAME, rank_score)
# return rank_score
# 加载最高分
def load_score():
rank_score = np.load(FILENAME)
return rank_score
# 保存最高分
def save_score(score):
rscore = load_score()
if score > rscore:
np.save(FILENAME, score)
#
def compare_score(score, rscore):
if score > rscore:
rscore = score
return rscore
# 打印棋盘
def print_board(stdscr, board, rscore):
global score
rscore = compare_score(score, rscore)
# stdscr.clear()
# 清除屏幕
stdscr.clear()
stdscr.addstr('得分:' + str(score) + '\n')
stdscr.addstr('历史最高:' + str(rscore) + '\n')
for i in range(4):
stdscr.addstr('-' * 22 + '\n')
for j in range(4):
stdscr.addstr('|')
if board[i, j]:
stdscr.addstr('{:^4d}'.format(board[i, j]))
else:
stdscr.addstr(' '.format())
stdscr.addstr('|')
stdscr.addstr('\n')
stdscr.addstr('-' * 22 + '\n')
# 主程序
def main(stdscr):
# 初始化程序
init_board = init()
rscore = load_score()
# 打印棋盘
print_board(stdscr, init_board, rscore)
# 游戏主进程
game(init_board, stdscr, rscore)
if __name__ == "__main__":
wrapper(main)