-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
326 lines (125 loc) · 4.83 KB
/
game.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
# -*- coding: cp936 -*-
#1-导入库
import pygame
from random import randint
#tkMessageBox 用来弹出对话框
import tkMessageBox
from pygame.locals import *
#2-初始化游戏
pygame.init()
width,height=480,480
#显示 样式 创建屏幕保存到变量中
screen=pygame.display.set_mode((width,height))
#标题
pygame.display.set_caption("jingziqi")
#插入图片
background=pygame.image.load("bjt.png")
#定义
empty = 0
black=(0,0,0)
red=(255,0,0)
blue=(0,0,255)
white=(255,255,255)
#创建绘制棋盘的函数
def draw_game():
#导入背景图
screen.blit(background,(0,0))
#画线
#用法:pygame.draw.line(显示,颜色,开始位置,结束位置,宽度)
pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
pygame.draw.line(screen, black, (0, 160), (480, 160), 5)
pygame.draw.line(screen, black, (0, 320), (480, 320), 5)
#遍历列表中的元素及他们的下标 row横col竖 row col是下标
for row, line in enumerate(state):
for col, val in enumerate(line):
if val == -1:
#画x
upper_left = (col * 160 + 5, row * 160 + 5)
lower_right = (col * 160 + 155, row * 160 + 155)
pygame.draw.line(screen, red, upper_left, lower_right, 5)
upper_right = (col * 160 + 155, row * 160 + 5)
lower_left = (col * 160 + 5, row * 160 + 155)
pygame.draw.line(screen, red, upper_right, lower_left, 5)
elif val == 1:
#创建一个矩形.在矩形里画圆
rect = (col * 160 + 5, row * 160 + 5, 150, 150)
pygame.draw.ellipse(screen, blue, rect, 5)
else:
assert val == empty
continue
pygame.display.flip()
def draw_O():
#随机函数
while True:
row = randint(0,2)
col = randint(0,2)
#当空格为空的时候画圆
if state[row][col] == 0:
state[row][col] = 1
break
draw_game()
pygame.display.flip()
def is_won():
for val in range(3):
# 检查匹配的行三个图形是否都相同且不等于空
if state[0][val] == state[1][val] == state[2][val] != empty:
return state[0][val]
# 检查匹配的列三个图形是否都相同不等于空
if state[val][0] == state[val][1] == state[val][2] != empty:
return state[val][0]
#判断 \ 中三个图形是否都相同
if state[0][0] == state[1][1] == state[2][2] != empty:
return state[1][1]
#判断 / 中三个图形是否都相同
if state[0][2] == state[1][1] == state[2][0] != empty:
return state[1][1]
#初始化棋盘
def begin():
global state
state = [[empty] * 3,[empty] * 3,[empty] * 3]
draw_game()
#首先初始化 pygame.display.flip()
begin()
#主循环
while True:
event=pygame.event.wait()
#初始化
pos = None
temp = 0
#接收到退出事件后退出程序
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
#加入了按键功能
elif event.type == KEYDOWN:
if event.key == K_a:
begin()
draw_game()
pygame.display.flip()
elif event.key == K_s:
pygame.event.post(pygame.event.Event(QUIT))
#接受鼠标点击事件
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
#event.pos[0]代表x轴坐标 event.pos[1]代表y轴坐标
pos = (event.pos[1]/160, event.pos[0]/160)
row,col=pos
#if pygame.mouse.get_rel()==(0,0):
# continue
#加一个条件让它只能在空的时候画x
if state[row][col]==0:
state[row][col] = -1
else:
continue
print pos
draw_game()
draw_O()
#判断属性接受返回值
if is_won() == -1:
tkMessageBox.showinfo(title='win',message='win')
pygame.quit()
exit(0)
elif is_won() == 1:
tkMessageBox.showinfo(title='lose',message='lose')
pygame.quit()
exit(0)