-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject 3.0.py
328 lines (218 loc) · 6.86 KB
/
Project 3.0.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import tkinter as tk
from random import randint
import random
import os
# 0: Easy, 1 : Medium, 2: Hard
level = 2
# 默认棋盘大小
board = [5, 6, 10]
numCell = [5 * 5, 6 * 6, 10 * 10]
players = 3
# 该谁投色子
nowPlayer = 0
# 每个玩家的位置,玩家编号 0 ~ players - 1 待赋值
playerLocation = []
end = 0
snakes = []
ladders = []
NumberOfSnakes = [3, 5, 7]
NumberOfLadder = [3, 5, 7]
# set
S = {}
# Task1
def showGameBoard():
os.system("cls")
print("awa")
# Task 2
def getRandomNumber():
return random.randint(1, 6)
# Task 3
# 一个在 x 跳到 y 走到哪
def jump(x, y):
return min(x + y, end)
color = ['red', 'yellow', 'blue', 'black', 'green']
## 圆的大小
S = 10
## 间隔
V = 2
dx = [0, 0, 0, S + V, S + V]
dy = [0, S + V, 2 * S + 2 * V, 0, S + V]
player_token = []
def generateSnakeAndLadderPostion():
for i in NumberOfSnakes:
while 1:
num = numCell[level]
c = [random.randint(0, num - 1), random.randint(0, num - 1)]
if c[0] == c[1] or c[0] < c[1] or c[0] in S or c[1] in S or c[0] == end:
continue
snakes.append(c)
S[c[0]] = 1
S[c[1]] = 1
break
for i in NumberOfLadder:
while 1:
num = numCell[level]
c = [random.randint(0, num - 1), random.randint(0, num - 1)]
if c[0] == c[1] or c[0] > c[1] or c[0] in S or c[1] in S:
continue
ladders.append(c)
S[c[0]] = 1
S[c[1]] = 1
break
# Task 4
def Winner():
res = []
for i in range(players):
if playerLocation[i] == end:
res.append(i)
return res
def draw_board(canvas, size):
"""
绘制棋盘格子
canvas: 画布对象
size: 棋盘大小
"""
num_cells = size * size
for i in range(1, num_cells):
if i % size == 0:
for j in range(size):
canvas.create_rectangle(j * 40, (i - 1) * 40, (j + 1) * 40, i * 40, fill='lightblue')
else:
for j in range(size - 1, -1, -1):
canvas.create_rectangle(j * 40, (i - 1) * 40, (j + 1) * 40, i * 40, fill='lightblue')
# 给 0 到 ~ 格子编号返回行列号
def get(u):
n = board[level]
x = u // n
y = u % n
if x % 2 == 1:
y = n - 1 - y
return [x, y]
def roll_dice(canvas, size, player_position):
"""
掷骰子,移动玩家
canvas: 画布对象
size: 棋盘大小
player_position: 玩家的位置,[行, 列]
"""
print("蛇", snakes)
print("梯子", ladders)
for i in range(players):
print(f'{i} is at {playerLocation[i]}')
global nowPlayer
print(f"It's {nowPlayer} turns, please click enter to get dice toll")
x = getRandomNumber()
print(f"骰子是 {x}")
u = playerLocation[nowPlayer]
v = jump(u, x)
print(f"{nowPlayer} move to {v}")
for i in snakes:
if v == i[0]:
print(f"{nowPlayer} 遇到蛇了, 掉到 {i[1]}")
v = i[1]
for i in ladders:
if v == i[0]:
print(f"{nowPlayer} 遇到梯子了, 升到 {i[1]}")
v = i[1]
playerLocation[nowPlayer] = v
loc = get(v)
move_player(canvas, size, getXYbyPosition(loc), nowPlayer)
nowPlayer = (nowPlayer + 1) % players
w = Winner()
if len(w) > 0:
print(w)
print("Wins")
def getXYbyPosition(player_position):
"""
player_position: 玩家的位置,[行, 列]
"""
x, y = player_position
x_pixel = y * 40 + 5
y_pixel = x * 40 + 5
return [x_pixel, y_pixel]
def move_player(canvas, size, player_position, player):
"""
移动玩家
canvas: 画布对象
size: 棋盘大小
player_position: 玩家的位置,[行, 列]
steps: 移动的步数
"""
num_cells = size * size
x, y = player_position
global player_token
canvas.delete(player_token[player])
i = player
player_token[i] = canvas.create_oval(x + dx[i], y + dy[i], x + S + dx[i], y + S + dy[i], fill = color[i])
canvas.update()
canvas.after(500) # 可视化时的暂停时间
# def choose(player_number_var, difficulty_var):
# """
# 选择玩家和难度级别
#
# player_number_var: 玩家选择的变量对象
# difficulty_var: 难度选择的变量对象
# """
# print("Selected Player:", player_number_var.get())
# print("Selected Difficulty:", difficulty_var.get())
def draw():
global playerLocation
playerLocation = [0] * players
root = tk.Tk()
size = board[level]
canvas = tk.Canvas(root, width=size * 40, height=size * 40, bg='white')
canvas.pack()
draw_board(canvas, size)
player_position = [0, 0]
global player_token
print(players)
for i in range(players):
x, y = getXYbyPosition([0, 0])
player_token.append(canvas.create_oval(x + dx[i], y + dy[i], x + S + dx[i], y + S + dy[i], fill = color[i]))
canvas.update()
# 掷骰子按钮(待关联)
roll_button = tk.Button(root, text='Roll Dice', command=lambda: roll_dice(canvas, size, player_position))
roll_button.pack()
# 不想玩儿了按钮
def close_window():
root.destroy()
Quit_button = tk.Button(root, text="QUIT", command=close_window)
Quit_button.pack()
root.mainloop()
def start():
st = tk.Tk()
# 玩家选择
player_number_var = tk.StringVar()
player_number_var.set("1 Players") # 默认值
player_menu = tk.OptionMenu(st, player_number_var, "1 Players", "2 Players", "3 Players", "4 Players", "5 Players")
player_menu.pack()
difficulty_var = tk.StringVar()
difficulty_var.set("level 1") # Default value
difficulty_menu = tk.OptionMenu(st, difficulty_var, "level 1", "level 2", "level 3")
difficulty_menu.pack()
# 完成设置按钮(最后要能开始进入board界面)
Start_button = tk.Button(st, text='START', command=lambda: choose(player_number_var, difficulty_var))
Start_button.pack()
# 难度选择
def choose(player_number_var, difficulty_var):
"""
选择玩家和难度级别
player_number_var: 玩家选择的变量对象
difficulty_var: 难度选择的变量对象
"""
level_text = difficulty_menu.cget('text')
global level
level = int(level_text.strip("level")) - 1 # 提取级别数字并减去1以匹配索引
size = board[level]
u = int(player_number_var.get().split(" ")[0])
print("Selected Player:", u)
print("Selected Difficulty:", level_text)
print("Board Size:", size)
global players
players = u
global end
end = numCell[level] - 1
st.destroy()
draw()
st.mainloop()
start()