本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下
一、运行截图:
二、代码
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
|
# 用数组定义一个棋盘,棋盘大小为 15×15
# 数组索引代表位置,
# 元素值代表该位置的状态:0代表没有棋子,1代表有黑棋,-1代表有白棋。
from tkinter import *
from tkinter.messagebox import *
class chess( object ):
def __init__( self ):
#############
# param #
#######################################
self .row, self .column = 15 , 15
self .mesh = 25
self .ratio = 0.9
self .board_color = "#cdba96"
self .header_bg = "#cdc0b0"
self .btn_font = ( "黑体" , 12 , "bold" )
self .step = self .mesh / 2
self .chess_r = self .step * self .ratio
self .point_r = self .step * 0.2
self .matrix = [[ 0 for y in range ( self .column)] for x in range ( self .row)]
self .is_start = false
self .is_black = true
self .last_p = none
###########
# gui #
#######################################
self .root = tk()
self .root.title( "gobang by young" )
self .root.resizable(width = false, height = false)
self .f_header = frame( self .root, highlightthickness = 0 , bg = self .header_bg)
self .f_header.pack(fill = both, ipadx = 10 )
self .b_start = button( self .f_header, text = "开始" , command = self .bf_start, font = self .btn_font)
self .b_restart = button( self .f_header, text = "重来" , command = self .bf_restart, state = disabled, font = self .btn_font)
self .l_info = label( self .f_header, text = "未开始" , bg = self .header_bg, font = ( "楷体" , 18 , "bold" ), fg = "white" )
self .b_regret = button( self .f_header, text = "悔棋" , command = self .bf_regret, state = disabled, font = self .btn_font)
self .b_lose = button( self .f_header, text = "认输" , command = self .bf_lose, state = disabled, font = self .btn_font)
self .b_start.pack(side = left, padx = 20 )
self .b_restart.pack(side = left)
self .l_info.pack(side = left, expand = yes, fill = both, pady = 10 )
self .b_lose.pack(side = right, padx = 20 )
self .b_regret.pack(side = right)
self .c_chess = canvas( self .root, bg = self .board_color, width = ( self .column + 1 ) * self .mesh,
height = ( self .row + 1 ) * self .mesh, highlightthickness = 0 )
self .draw_board()
self .c_chess.bind( "<button-1>" , self .cf_board)
self .c_chess.pack()
self .root.mainloop()
# 画x行y列处的网格
def draw_mesh( self , x, y):
# 一个倍率,由于tkinter操蛋的gui,如果不加倍率,悔棋的时候会有一点痕迹,可以试试把这个改为1,就可以看到
ratio = ( 1 - self .ratio) * 0.99 + 1
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 先画背景色
self .c_chess.create_rectangle(center_y - self .step, center_x - self .step,
center_y + self .step, center_x + self .step,
fill = self .board_color, outline = self .board_color)
# 再画网格线,这里面a b c d是不同的系数,根据x,y不同位置确定,需要一定推导。
a, b = [ 0 , ratio] if y = = 0 else [ - ratio, 0 ] if y = = self .row - 1 else [ - ratio, ratio]
c, d = [ 0 , ratio] if x = = 0 else [ - ratio, 0 ] if x = = self .column - 1 else [ - ratio, ratio]
self .c_chess.create_line(center_y + a * self .step, center_x, center_y + b * self .step, center_x)
self .c_chess.create_line(center_y, center_x + c * self .step, center_y, center_x + d * self .step)
# 有一些特殊的点要画小黑点
if ((x = = 3 or x = = 11 ) and (y = = 3 or y = = 11 )) or (x = = 7 and y = = 7 ):
self .c_chess.create_oval(center_y - self .point_r, center_x - self .point_r,
center_y + self .point_r, center_x + self .point_r, fill = "black" )
# 画x行y列处的棋子,color指定棋子颜色
def draw_chess( self , x, y, color):
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 就是画个圆
self .c_chess.create_oval(center_y - self .chess_r, center_x - self .chess_r,
center_y + self .chess_r, center_x + self .chess_r,
fill = color)
# 画整个棋盘
def draw_board( self ):
[ self .draw_mesh(x, y) for y in range ( self .column) for x in range ( self .row)]
# 在正中间显示文字
def center_show( self , text):
width, height = int ( self .c_chess[ 'width' ]), int ( self .c_chess[ 'height' ])
self .c_chess.create_text( int (width / 2 ), int (height / 2 ), text = text, font = ( "黑体" , 30 , "bold" ), fill = "red" )
# 开始的时候设置各个组件,变量的状态,初始化matrix矩阵,初始化棋盘,初始化信息
def bf_start( self ):
self .set_btn_state( "start" )
self .is_start = true
self .is_black = true
self .matrix = [[ 0 for y in range ( self .column)] for x in range ( self .row)]
self .draw_board()
self .l_info.config(text = "黑方下棋" )
# 重来跟开始的效果一样
def bf_restart( self ):
self .bf_start()
# 用last_p来标识上一步的位置。先用网格覆盖掉棋子,操作相应的变量,matrix[x][y]要置空,只能悔一次棋
def bf_regret( self ):
if not self .last_p:
showinfo( "提示" , "现在不能悔棋" )
return
x, y = self .last_p
self .draw_mesh(x, y)
self .matrix[x][y] = 0
self .last_p = none
self .trans_identify()
# 几个状态改变,还有显示文字,没什么说的
def bf_lose( self ):
self .set_btn_state( "init" )
self .is_start = false
text = self .ternary_operator( "黑方认输" , "白方认输" )
self .l_info.config(text = text)
self .center_show( "蔡" )
# canvas的click事件
def cf_board( self , e):
# 找到离点击点最近的坐标
x, y = int ((e.y - self .step) / self .mesh), int ((e.x - self .step) / self .mesh)
# 找到该坐标的中心点位置
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 计算点击点到中心的距离
distance = ((center_x - e.y) * * 2 + (center_y - e.x) * * 2 ) * * 0.5
# 如果距离不在规定的圆内,退出//如果这个位置已经有棋子,退出//如果游戏还没开始,退出
if distance > self .step * 0.95 or self .matrix[x][y] ! = 0 or not self .is_start:
return
# 此时棋子的颜色,和matrix中该棋子的标识。
color = self .ternary_operator( "black" , "white" )
tag = self .ternary_operator( 1 , - 1 )
# 先画棋子,在修改matrix相应点的值,用last_p记录本次操作点
self .draw_chess(x, y, color)
self .matrix[x][y] = tag
self .last_p = [x, y]
# 如果赢了,则游戏结束,修改状态,中心显示某方获胜
if self .is_win(x, y, tag):
self .is_start = false
self .set_btn_state( "init" )
text = self .ternary_operator( "黑方获胜" , "白方获胜" )
self .center_show(text)
return
# 如果游戏继续,则交换棋手
self .trans_identify()
def is_win( self , x, y, tag):
# 获取斜方向的列表
def direction(i, j, di, dj, row, column, matrix):
temp = []
while 0 < = i < row and 0 < = j < column:
i, j = i + di, j + dj
i, j = i - di, j - dj
while 0 < = i < row and 0 < = j < column:
temp.append(matrix[i][j])
i, j = i - di, j - dj
return temp
four_direction = []
# 获取水平和竖直方向的列表
four_direction.append([ self .matrix[i][y] for i in range ( self .row)])
four_direction.append([ self .matrix[x][j] for j in range ( self .column)])
# 获取斜方向的列表
four_direction.append(direction(x, y, 1 , 1 , self .row, self .column, self .matrix))
four_direction.append(direction(x, y, 1 , - 1 , self .row, self .column, self .matrix))
# 一一查看这四个方向,有没有满足五子连珠
for v_list in four_direction:
count = 0
for v in v_list:
if v = = tag:
count + = 1
if count = = 5 :
return true
else :
count = 0
return false
# 设置四个按钮是否可以点击
def set_btn_state( self , state):
state_list = [normal, disabled, disabled, disabled] if state = = "init" else [disabled, normal, normal, normal]
self .b_start.config(state = state_list[ 0 ])
self .b_restart.config(state = state_list[ 1 ])
self .b_regret.config(state = state_list[ 2 ])
self .b_lose.config(state = state_list[ 3 ])
# 因为有很多和self.black相关的三元操作,所以就提取出来
def ternary_operator( self , true, false):
return true if self .is_black else false
# 交换棋手
def trans_identify( self ):
self .is_black = not self .is_black
text = self .ternary_operator( "黑方下棋" , "白方下棋" )
self .l_info.config(text = text)
if __name__ = = '__main__' :
chess()
|
三、增加复盘和保存棋谱功能
直接贴可以运行的源码,添加了两个按键和一个页面。
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# 用数组定义一个棋盘,棋盘大小为 15×15
# 数组索引代表位置,
# 元素值代表该位置的状态:0代表没有棋子,1代表有黑棋,-1代表有白棋。
from tkinter import *
from tkinter.messagebox import *
import os
tag_black = "1"
tag_empty = "."
tag_white = "0"
root_dir = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "." )
class chess( object ):
def bf_save( self ):
path = os.path.join(root_dir, "record.txt" )
file = open (path, "w" )
for i in range ( len ( self .record)):
x, y = self .record[i]
file .write( "{}: [{}, {}]\n" . format ( "黑方" if i % 2 = = 0 else "白方" , x, y))
file .close()
def init_matrix( self ):
return [[tag_empty for y in range ( self .column)] for x in range ( self .row)]
def __init__( self ):
#############
# param #
#######################################
self .row, self .column = 15 , 15
self .mesh = 25
self .ratio = 0.9
self .board_color = "#cdba96"
self .header_bg = "#cdc0b0"
self .btn_font = ( "黑体" , 12 , "bold" )
self .step = self .mesh / 2
self .chess_r = self .step * self .ratio
self .point_r = self .step * 0.2
self .matrix = self .init_matrix()
self .is_start = false
self .is_black = true
self .record = []
###########
# gui #
#######################################
self .root = tk()
self .root.title( "gobang by young" )
self .root.resizable(width = false, height = false)
self .f_header = frame( self .root, highlightthickness = 0 , bg = self .header_bg)
self .b_start = button( self .f_header, text = "开始" , command = self .bf_start, font = self .btn_font)
self .b_restart = button( self .f_header, text = "重来" , command = self .bf_restart, state = disabled, font = self .btn_font)
self .l_info = label( self .f_header, text = "未开始" , bg = self .header_bg, font = ( "楷体" , 18 , "bold" ), fg = "white" )
self .b_regret = button( self .f_header, text = "悔棋" , command = self .bf_regret, state = disabled, font = self .btn_font)
self .b_lose = button( self .f_header, text = "认输" , command = self .bf_lose, state = disabled, font = self .btn_font)
self .c_chess = canvas( self .root, bg = self .board_color, width = ( self .column + 1 ) * self .mesh,
height = ( self .row + 1 ) * self .mesh, highlightthickness = 0 )
self .draw_board()
self .c_chess.bind( "<button-1>" , self .cf_board)
self .b_record = button( self .root, text = "复 盘" , command = self .bf_record, font = self .btn_font, bg = "lightblue" )
self .b_save = button( self .root, text = "保 存" , command = self .bf_save, font = self .btn_font, bg = "lightblue" )
self .f_header.pack(side = top, fill = both, ipadx = 10 )
self .b_start.pack(side = left, padx = 20 )
self .b_restart.pack(side = left)
self .l_info.pack(side = left, expand = yes, fill = both, pady = 10 )
self .b_lose.pack(side = right, padx = 20 )
self .b_regret.pack(side = right)
self .c_chess.pack(side = top)
self .b_record.pack(side = top, expand = yes, fill = x)
self .b_save.pack(side = top, expand = yes, fill = x)
self .root.mainloop()
# 画x行y列处的网格
def draw_mesh( self , x, y):
# 一个倍率,由于tkinter操蛋的gui,如果不加倍率,悔棋的时候会有一点痕迹,可以试试把这个改为1,就可以看到
ratio = ( 1 - self .ratio) * 0.99 + 1
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 先画背景色
self .c_chess.create_rectangle(center_y - self .step, center_x - self .step,
center_y + self .step, center_x + self .step,
fill = self .board_color, outline = self .board_color)
# 再画网格线,这里面a b c d是不同的系数,根据x,y不同位置确定,需要一定推导。
a, b = [ 0 , ratio] if y = = 0 else [ - ratio, 0 ] if y = = self .row - 1 else [ - ratio, ratio]
c, d = [ 0 , ratio] if x = = 0 else [ - ratio, 0 ] if x = = self .column - 1 else [ - ratio, ratio]
self .c_chess.create_line(center_y + a * self .step, center_x, center_y + b * self .step, center_x)
self .c_chess.create_line(center_y, center_x + c * self .step, center_y, center_x + d * self .step)
# 有一些特殊的点要画小黑点
if ((x = = 3 or x = = 11 ) and (y = = 3 or y = = 11 )) or (x = = 7 and y = = 7 ):
self .c_chess.create_oval(center_y - self .point_r, center_x - self .point_r,
center_y + self .point_r, center_x + self .point_r, fill = "black" )
# 画x行y列处的棋子,color指定棋子颜色
def draw_chess( self , x, y, color):
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 就是画个圆
self .c_chess.create_oval(center_y - self .chess_r, center_x - self .chess_r,
center_y + self .chess_r, center_x + self .chess_r,
fill = color)
# 画整个棋盘
def draw_board( self ):
[ self .draw_mesh(x, y) for y in range ( self .column) for x in range ( self .row)]
# 在正中间显示文字
def center_show( self , text):
width, height = int ( self .c_chess[ 'width' ]), int ( self .c_chess[ 'height' ])
self .c_chess.create_text( int (width / 2 ), int (height / 2 ), text = text, font = ( "黑体" , 30 , "bold" ), fill = "red" )
def bf_record( self ):
record( self .record)
pass
# 开始的时候设置各个组件,变量的状态,初始化matrix矩阵,初始化棋盘,初始化信息
def bf_start( self ):
self .set_btn_state( "start" )
self .is_start = true
self .is_black = true
self .matrix = self .init_matrix()
self .draw_board()
self .record = []
self .l_info.config(text = "黑方下棋" )
# 重来跟开始的效果一样
def bf_restart( self ):
self .record = []
self .bf_start()
# 用last_p来标识上一步的位置。先用网格覆盖掉棋子,操作相应的变量,matrix[x][y]要置空,只能悔一次棋
def bf_regret( self ):
if len ( self .record) = = 0 :
showinfo( "提示" , "现在不能悔棋" )
return
x, y = self .record[ - 1 ]
self .draw_mesh(x, y)
self .matrix[x][y] = tag_empty
self .record = self .record[: - 1 ]
self .trans_identify()
# 几个状态改变,还有显示文字,没什么说的
def bf_lose( self ):
self .set_btn_state( "init" )
self .is_start = false
text = self .ternary_operator( "黑方认输" , "白方认输" )
self .l_info.config(text = text)
self .center_show( "蔡" )
def go_chess( self , x, y):
# 此时棋子的颜色,和matrix中该棋子的标识。
color = self .ternary_operator( "black" , "white" )
tag = self .ternary_operator(tag_black, tag_white)
# 先画棋子,在修改matrix相应点的值,用last_p记录本次操作点
self .draw_chess(x, y, color)
self .matrix[x][y] = tag
self .record.append([x, y])
# 如果赢了,则游戏结束,修改状态,中心显示某方获胜
if self .is_win(x, y, tag):
self .is_start = false
self .set_btn_state( "init" )
text = self .ternary_operator( "黑方获胜" , "白方获胜" )
self .center_show(text)
return
# 如果游戏继续,则交换棋手
self .trans_identify()
# canvas的click事件
def cf_board( self , e):
# 找到离点击点最近的坐标
x, y = int ((e.y - self .step) / self .mesh), int ((e.x - self .step) / self .mesh)
# 找到该坐标的中心点位置
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 计算点击点到中心的距离
distance = ((center_x - e.y) * * 2 + (center_y - e.x) * * 2 ) * * 0.5
# 如果距离不在规定的圆内,退出//如果这个位置已经有棋子,退出//如果游戏还没开始,退出
if distance > self .step * 0.95 or self .matrix[x][y] ! = tag_empty or not self .is_start:
return
self .go_chess(x, y)
def is_win( self , x, y, tag):
# 获取斜方向的列表
def direction(i, j, di, dj, row, column, matrix):
temp = []
while 0 < = i < row and 0 < = j < column:
i, j = i + di, j + dj
i, j = i - di, j - dj
while 0 < = i < row and 0 < = j < column:
temp.append(matrix[i][j])
i, j = i - di, j - dj
return temp
four_direction = []
# 获取水平和竖直方向的列表
four_direction.append([ self .matrix[i][y] for i in range ( self .row)])
four_direction.append([ self .matrix[x][j] for j in range ( self .column)])
# 获取斜方向的列表
four_direction.append(direction(x, y, 1 , 1 , self .row, self .column, self .matrix))
four_direction.append(direction(x, y, 1 , - 1 , self .row, self .column, self .matrix))
# 一一查看这四个方向,有没有满足五子连珠
for v_list in four_direction:
if tag * 5 in "".join(v_list):
return true
return false
# 设置四个按钮是否可以点击
def set_btn_state( self , state):
state_list = [normal, disabled, disabled, disabled] if state = = "init" else [disabled, normal, normal, normal]
self .b_start.config(state = state_list[ 0 ])
self .b_restart.config(state = state_list[ 1 ])
self .b_regret.config(state = state_list[ 2 ])
self .b_lose.config(state = state_list[ 3 ])
# 因为有很多和self.black相关的三元操作,所以就提取出来
def ternary_operator( self , true, false):
return true if self .is_black else false
# 交换棋手
def trans_identify( self ):
self .is_black = not self .is_black
text = self .ternary_operator( "黑方下棋" , "白方下棋" )
self .l_info.config(text = text)
def print_process( self ):
pass
class record( object ):
def __init__( self , record):
#############
# param #
#######################################
self .row, self .column = 15 , 15
self .mesh = 25
self .ratio = 0.9
self .board_color = "#cdba96"
self .header_bg = "#cdc0b0"
self .btn_font = ( "黑体" , 12 , "bold" )
self .step = self .mesh / 2
self .chess_r = self .step * self .ratio
self .point_r = self .step * 0.2
###########
self .is_black = true
self .index = - 1
self .record = record
###########
# gui #
#######################################
self .root = tk()
self .root.title( "复盘" )
self .root.resizable(width = false, height = false)
self .root.bind( "<key>" , self .kf_step)
self .f_header = frame( self .root, highlightthickness = 0 , bg = self .header_bg)
self .l_info = label( self .f_header, text = "未开始" , bg = self .header_bg, font = ( "楷体" , 18 , "bold" ), fg = "white" )
self .c_chess = canvas( self .root, bg = self .board_color, width = ( self .column + 1 ) * self .mesh,
height = ( self .row + 1 ) * self .mesh, highlightthickness = 0 )
self .draw_board()
self .f_header.pack(fill = both, ipadx = 10 )
self .l_info.pack(side = left, expand = yes, fill = both, pady = 10 )
self .c_chess.pack()
self .root.mainloop()
def kf_step( self , e):
if e.keycode not in [ 37 , 39 ]:
return
if e.keycode = = 37 :
if self .index = = - 1 :
self .l_info.config(text = "已经走到最开始了" )
return
x, y = self .record[ self .index]
self .draw_mesh(x, y)
if self .index = = 0 :
self .l_info.config(text = "未开始" )
else :
self .l_info.config(text = "黑方走棋" if self .is_black else "白方走棋" )
self .is_black = not self .is_black
self .index - = 1
if self .index > 0 :
x, y = self .record[ self .index]
color = "white" if self .is_black else "black"
self .draw_chess(x, y, color, "red" )
elif e.keycode = = 39 :
if self .index = = len ( self .record) - 1 :
self .l_info.config(text = "已经走到最末尾了" )
return
self .index + = 1
x, y = self .record[ self .index]
color = "black" if self .is_black else "white"
self .draw_chess(x, y, color, "red" )
if self .index > 0 :
x, y = self .record[ self .index - 1 ]
color = "white" if self .is_black else "black"
self .draw_chess(x, y, color)
self .l_info.config(text = "黑方走棋" if self .is_black else "白方走棋" )
self .is_black = not self .is_black
else :
pass
# 画x行y列处的网格
def draw_mesh( self , x, y):
# 一个倍率,由于tkinter操蛋的gui,如果不加倍率,悔棋的时候会有一点痕迹,可以试试把这个改为1,就可以看到
ratio = ( 1 - self .ratio) * 0.99 + 1
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 先画背景色
self .c_chess.create_rectangle(center_y - self .step, center_x - self .step,
center_y + self .step, center_x + self .step,
fill = self .board_color, outline = self .board_color)
# 再画网格线,这里面a b c d是不同的系数,根据x,y不同位置确定,需要一定推导。
a, b = [ 0 , ratio] if y = = 0 else [ - ratio, 0 ] if y = = self .row - 1 else [ - ratio, ratio]
c, d = [ 0 , ratio] if x = = 0 else [ - ratio, 0 ] if x = = self .column - 1 else [ - ratio, ratio]
self .c_chess.create_line(center_y + a * self .step, center_x, center_y + b * self .step, center_x)
self .c_chess.create_line(center_y, center_x + c * self .step, center_y, center_x + d * self .step)
# 有一些特殊的点要画小黑点
if ((x = = 3 or x = = 11 ) and (y = = 3 or y = = 11 )) or (x = = 7 and y = = 7 ):
self .c_chess.create_oval(center_y - self .point_r, center_x - self .point_r,
center_y + self .point_r, center_x + self .point_r, fill = "black" )
# 画x行y列处的棋子,color指定棋子颜色
def draw_chess( self , x, y, color, outline = "black" ):
center_x, center_y = self .mesh * (x + 1 ), self .mesh * (y + 1 )
# 就是画个圆
self .c_chess.create_oval(center_y - self .chess_r, center_x - self .chess_r,
center_y + self .chess_r, center_x + self .chess_r,
fill = color, outline = outline)
# 画整个棋盘
def draw_board( self ):
[ self .draw_mesh(x, y) for y in range ( self .column) for x in range ( self .row)]
# 在正中间显示文字
def center_show( self , text):
width, height = int ( self .c_chess[ 'width' ]), int ( self .c_chess[ 'height' ])
self .c_chess.create_text( int (width / 2 ), int (height / 2 ), text = text, font = ( "黑体" , 30 , "bold" ), fill = "red" )
# 开始的时候设置各个组件,变量的状态,初始化matrix矩阵,初始化棋盘,初始化信息
if __name__ = = '__main__' :
chess()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_28969139/article/details/81008389