-
Notifications
You must be signed in to change notification settings - Fork 16
/
showgood.py
executable file
·499 lines (431 loc) · 18.4 KB
/
showgood.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# show by 张颢丹
# 2015/5/27 3:40 冀锐 修改 实现扣血详细信息的显示
# 读取对局文件, 把对局可视化
# 如果带了命令行参数, 打开参数里的文件
# 否则根据文件中的文件名打开文件(第116行附近)
#
# 注: 需要和table放在同一个目录下
# 注: 只输入文件名, 不要带扩展名
# 例: '[W.T]T_idiot-VS-T_idiot'
# 不要在意里面的各种神秘数字
import shelve, time, sys, copy
import pygame
from pygame.locals import *
from table import *
# 这个参数用来调整时间流逝的速率
# game_speed=1时, 一来回需要3.6秒
game_speed = 1
pause_speed = 0
# 各种参数
x, y = 18, 10
s_size = (1024, 600)
x_n = s_size[0] // 2 // 18
y_n = s_size[1] // 2 // 10
n = x_n if x_n < y_n else y_n
center = (s_size[0] / 2, s_size[1] / 2)
# 球桌的四个角的坐标
table = (
(center[0] - x / 2 * n, center[1] - y / 2 * n),
(center[0] + x / 2 * n, center[1] - y / 2 * n),
(center[0] + x / 2 * n, center[1] + y / 2 * n),
(center[0] - x / 2 * n, center[1] + y / 2 * n),)
# 读取文件, 返回文件中的log类, 胜利者, 和胜利原因
def readlog(logname):
d = shelve.open(logname)
log = d['log']
winner = d['winner']
reason = d['reason']
tick_total = d['tick_total']
TICKTOTAL=tick_total
d.close()
return log, winner, reason, tick_total
# 把log类转换成字典
def getdata(alog):
d = {}
d['ball_pos'] = alog.ball.pos
d['ball_v'] = alog.ball.velocity
d['tick'] = alog.tick
d['player'] = {}
d['player'][alog.side.side] = alog.side
d['player'][alog.op_side.side] = alog.op_side
d['cards'] = alog.card.cards # 当前球桌上的散落道具,尚未被获取
d['card_tick'] = alog.card.card_tick # 道具出现时间的计时,0—CARD_FREQ
d['active_card'] = alog.card.active_card # 当前使用的道具('SELF'/'OPNT', card_code),'SELF'指跑位方
d['side'] = alog.side.side # 迎球方
d['op_side'] = alog.op_side.side # 跑位方
return d
# 把球桌坐标转换成pygame屏幕的坐标
def pos_trans(oldpos):
pos_x = int((0.0 + oldpos.x / (DIM[1] - DIM[0])) * n * x + center[0])
pos_y = int((0.5 - oldpos.y / (DIM[3] - DIM[2])) * n * y + center[1])
return (pos_x, pos_y)
# 画球桌
def draw_table(screen):
polygon_1 = (
(center[0] - x / 2 * n, center[1] - y / 2 * n),
(center[0] + x / 2 * n, center[1] - y / 2 * n),
(center[0] + x / 2 * n, center[1] - y / 2 * n - 10),
(center[0] - x / 2 * n, center[1] - y / 2 * n - 10),)
polygon_2 = (
(center[0] + x / 2 * n, center[1] + y / 2 * n),
(center[0] - x / 2 * n, center[1] + y / 2 * n),
(center[0] - x / 2 * n, center[1] + y / 2 * n + 10),
(center[0] + x / 2 * n, center[1] + y / 2 * n + 10),)
pygame.draw.polygon(screen, (0, 255, 0), polygon_1, 0)
pygame.draw.polygon(screen, (0, 255, 0), polygon_2, 0)
# 画球
def draw_ball(screen, ball_pos):
pos = pos_trans(ball_pos)
pygame.draw.circle(screen, (0, 0, 0), pos, 8, 0)
# 画球拍
def draw_player(screen, player,bias):
if not hasattr(bias,'real'):#不管了,不处理最后了!
bias=player.pos.y
if player.side == 'West':
color = (255, 0, 0)
poslist = ((player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0],
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] - 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0] - 10,
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] - 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0] - 10,
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] + 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0],
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] + 25))
else:
color = (0, 0, 255)
poslist = ((player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0],
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] - 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0] + 10,
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] - 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0] + 10,
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] + 25),
(player.pos.x / (DIM[1] - DIM[0]) * n * x + center[0],
-((bias) / (DIM[3] - DIM[2]) - 0.5) * n * y + center[1] + 25))
pos = pos_trans(player.pos)
pygame.draw.polygon(screen, color, poslist, 0)
# 画
def draw_all(screen, ball_pos, player_1, player_2,east,west):
draw_table(screen)
draw_ball(screen, ball_pos)
draw_player(screen, player_1,west)
draw_player(screen, player_2,east)
# 写信息
def writeinfo(screen, player, font):
screen.blit(font.render('W', True, (0, 0, 0)), (table[0][0] - 24 - 100, table[0][1] - 100))
screen.blit(font.render('E', True, (0, 0, 0)), (table[1][0] + 100, table[1][1] - 100))
screen.blit(font.render(player['West'].name, True, (0, 0, 0)), (table[0][0] - 24 - 100 - 20, table[0][1] - 50))
screen.blit(font.render(player['East'].name, True, (0, 0, 0)), (table[1][0] + 100 - 20, table[1][1] - 50))
screen.blit(font.render(str(int(player['West'].life)), True, (0, 0, 0)), (table[0][0] - 24, table[0][1] - 100))
screen.blit(font.render(str(int(player['East'].life)), True, (0, 0, 0)), (table[1][0] - 80, table[1][1] - 100))
# 写血量改变
def writeChange(screen, player, font):
screen.blit(font.render("bat:" + str(int(player['West'].bat_lf)), True, (0, 0, 0)),
(table[0][0] - 244, table[0][1]))
screen.blit(font.render("acc:" + str(int(player['West'].acc_lf)), True, (0, 0, 0)),
(table[0][0] - 244, table[0][1] + 25))
screen.blit(font.render("run:" + str(int(player['West'].run_lf)), True, (0, 0, 0)),
(table[0][0] - 244, table[0][1] + 50))
screen.blit(font.render("card:" + str(int(player['West'].card_lf)), True, (0, 0, 0)),
(table[0][0] - 244, table[0][1] + 75))
screen.blit(font.render("bat:" + str(int(player['East'].bat_lf)), True, (0, 0, 0)),
(table[1][0] + 180, table[1][1]))
screen.blit(font.render("acc:" + str(int(player['East'].acc_lf)), True, (0, 0, 0)),
(table[1][0] + 180, table[1][1] + 25))
screen.blit(font.render("run:" + str(int(player['East'].run_lf)), True, (0, 0, 0)),
(table[1][0] + 180, table[1][1] + 50))
screen.blit(font.render("card:" + str(int(player['East'].card_lf)), True, (0, 0, 0)),
(table[1][0] + 180, table[1][1] + 75))
def card_image_file(card):
if isinstance(card, Card):
return "%s.png" % card.code.lower()
elif isinstance(card, str):
return "%s.png" % card.lower()
else:
raise TypeError
# 画道具
def draw_card(screen, cards, font):
for card in cards:
x, y = pos_trans(card.pos)
image = pygame.image.load(card_image_file(card))
x -= image.get_width() / 2
y -= image.get_height() / 2
screen.blit(image, (x, y))
# 画道具箱
def draw_card_box(screen, player):
i = 0
for card in player['West'].card_box:
image = pygame.image.load(card_image_file(card)).convert_alpha()
screen.blit(image, (150 - image.get_width() / 2, 170 + i))
i += image.get_height() + 10
i = 0
for card in player['East'].card_box:
image = pygame.image.load(card_image_file(card)).convert_alpha()
screen.blit(image, (s_size[0] - 150 - image.get_width() / 2, 170 + i))
i += image.get_height() + 10
# 画道具使用历史
def draw_card_history(screen, player_card_history):
# TODO 跟draw_card_box类似,需要一个新的坐标容纳各自的使用道具历史
i = 0
for card in player_card_history['West']:
image = pygame.image.load(card_image_file(card[1])).convert_alpha()
screen.blit(image, (200 - image.get_width() / 2, 170 + i))
i += image.get_height() + 10
i = 0
for card in player_card_history['East']:
image = pygame.image.load(card_image_file(card[1])).convert_alpha()
screen.blit(image, (s_size[0] - 200 - image.get_width() / 2, 170 + i))
i += image.get_height() + 10
return
# 新增让球拍连续运动。变量名有些奇怪,请无视之。不保证正确,看个乐子吧…… by 李逸飞
TEMPPOS=Position(-900000,500000)
PREW=list()
PREW.append((0,TEMPPOS))
PREE=list()
PREE.append((0,TEMPPOS))
def gettrueW(tick):
for i in range(len(PREW)):
if tick<=PREW[i][0]:
y1=PREW[i-1][1]
y2=PREW[i][1]
delta=tick-PREW[i-1][0]
T=PREW[i][0]-PREW[i-1][0]
return y1.y+(y2.y-y1.y)*delta/T
def gettrueE(tick):
for i in range(len(PREE)):
if tick<=PREE[i][0]:
y1=PREE[i-1][1]
y2=PREE[i][1]
delta=tick-PREE[i-1][0]
T=PREE[i][0]-PREE[i-1][0]
return y1.y+(y2.y-y1.y)*delta/T
def processjump(S,T):
PREW.append(S[0])
PREE.append(T[0])
for i in range(len(S)-1):
if S[i+1][1]==PREW[-1][1]:
continue
else :
PREW.append(S[i+1])
for i in range(len(T)-1):
if T[i+1][1]==PREE[-1][1]:
continue
else :
PREE.append(T[i+1])
def preprocess(log,t_passed):
H=log[:]
PREW=list()
PREE=list()
over=False
d_current = getdata(H.pop(0))
d_next=getdata(H[0])
next_tick=1
tick = d_current['tick']
while not over:
player = d_current['player']
# 时间流逝和球的移动
tick += t_passed
PREW.append((tick,player['West'].pos))
PREE.append((tick,player['East'].pos))
# 半回合后读取下一次记录
if not over and tick >= next_tick:
d_current = d_next
# 判断是否为最后一次记录
if len(H) > 0:
d_next = getdata(H.pop(0))
next_tick = d_next['tick']
else:
over = True
processjump(PREW,PREE)
def main():
global game_speed
global pause_speed
# 判断有无命令行参数
if len(sys.argv) == 2:
logname = sys.argv[1]
# 兼容原来的命令行参数查找模式
namelist = [logname]
else:
# 这里对当前目录进行搜索,找到一个字节数不为0的dat文件
import os, re
file_list = os.listdir(os.getcwd())
# 编译正则表达式,寻找对应的文件名
r = re.compile(r'^\[[EW]\.[A-Z]\]T_[^-]+-VS-T_[^.]+\.(dat|db)$')
# 首先保证是文件而不是目录,且不为空
namelist = [] # 用来保存所有对战名称
for name in filter(lambda f: os.path.isfile(f) and os.path.getsize(f) != 0, file_list):
m = r.match(name)
if m is not None:
# 不为空,则拿到了一个正确的文件
logname = name[:name.rindex('.')] # 去除.dat/.db后缀
namelist.append(logname)
# else:
# 没找到,说明本目录下没有这个测试文件
# raise NameError("No Test File in this directory.")
while True:
try:
if not namelist:
raise NameError
for i in range(len(namelist)):
my_print('第', i, '个', namelist[i])
ssssss = int(input('请输入你想看的对战的序号,从0开始,到%d结束\n' % (len(namelist) - 1))) # 序号
logname = namelist[ssssss]
break
except ValueError as e:
# 输入了一个非数字
my_print('请输入合法数字!')
except IndexError as e:
# 列表越界
my_print('请输入范围内的数字(0-%d)' % (len(namelist) - 1))
except NameError as e:
my_print('没有测试文件!')
input('请输入回车键退出程序')
exit()
# 读出log, winner, reason
log, winner, reason, tick_total = readlog(logname)
backup = copy.copy(log)
over = False
# pygame初始化
pygame.init()
screen = pygame.display.set_mode(s_size)
font = pygame.font.SysFont("arial", 32)
small_font = pygame.font.SysFont("arial", 16)
clock = pygame.time.Clock()
# 初始化cardhistory
player_card_history = {'West': [], 'East': []}
clock.tick()
t_passed=clock.tick(100) * game_speed
# 数据预处理TODO!
preprocess(log,t_passed)
# 读取两轮数据
d_current = getdata(log.pop(0))
player = d_current['player']
ball_pos = copy.copy(d_current['ball_pos'])
ball_v = copy.copy(d_current['ball_v'])
tick = d_current['tick']
# 给使用道具一方(跑位方)加上当前道具
card_tick = d_current['card_tick']
if d_current['active_card'][0] is not None:
player_card_history[d_current['side']].append(d_current['active_card'])
next_tick = 0
if len(log) > 1:
d_next = getdata(log.pop(0))
next_tick = d_next['tick']
else:
over = True
clock.tick()
while True:
t_passed = clock.tick(100) * game_speed # 限制FPS
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
game_speed, pause_speed = pause_speed, game_speed
if event.key in range(K_0, K_9 + 1):
t = event.key - K_0
# 读取两轮数据
log = copy.copy(backup)
for i in range(len(log) // 10 * t):
log.pop(0)
d_current = getdata(log.pop(0))
player = d_current['player']
ball_pos = copy.copy(d_current['ball_pos'])
ball_v = copy.copy(d_current['ball_v'])
tick = d_current['tick']
# 给使用道具一方(跑位方)加上当前道具
card_tick = d_current['card_tick']
if d_current['active_card'][0] is not None:
player_card_history[d_current['side']].append(d_current['active_card'])
next_tick = 0
over = False
if len(log) > 1:
d_next = getdata(log.pop(0))
next_tick = d_next['tick']
else:
over = True
# 支持小键盘
if event.key in range(K_KP0, K_KP9 + 1):
t = event.key - K_KP0
# 读取两轮数据
log = copy.copy(backup)
for i in range(len(log) // 10 * t):
log.pop(0)
d_current = getdata(log.pop(0))
player = d_current['player']
ball_pos = copy.copy(d_current['ball_pos'])
ball_v = copy.copy(d_current['ball_v'])
tick = d_current['tick']
# 给使用道具一方(跑位方)加上当前道具
card_tick = d_current['card_tick']
if d_current['active_card'][0] is not None:
player_card_history[d_current['side']].append(d_current['active_card'])
next_tick = 0
over = False
if len(log) > 1:
d_next = getdata(log.pop(0))
next_tick = d_next['tick']
else:
over = True
# 画画
screen.fill((255, 255, 255))
writeinfo(screen, player, font)
draw_card(screen, d_current['cards'], font)
draw_card_box(screen, player)
draw_card_history(screen, player_card_history)
east=gettrueE(tick)
west=gettrueW(tick)
draw_all(screen, ball_pos, player['West'], player['East'],east,west)
writeChange(screen, player, small_font)
# 最后一次记录之后再走半回合
if over and tick >= next_tick + 1800:
tick = next_tick + 1800
screen.blit(font.render(reason, True, (0, 0, 0)), (center[0] - 50, center[1] - 220))
screen.blit(font.render("%s win!" % player[winner].name, True, (0, 0, 0)),
(center[0] - 60, center[1] - 270))
image = pygame.image.load('win.jpg').convert_alpha()
if winner == 'East':
screen.blit(image, (
table[1][0] + 100 - 20 - 40,
table[1][1] - 50 - 20)) # (table[0][0] - 24 - 100 - 20, table[0][1] - 50)
elif winner == 'West':
screen.blit(image, (
table[0][0] - 24 - 100 - 20 - 40,
table[0][1] - 50 - 20)) # (table[1][0] + 100 - 20, table[1][1] - 50)
t_passed = 0
# 时间流逝和球的移动
tick += t_passed
card_tick += t_passed
screen.blit(font.render("tick: %s" % (tick,), True, (0, 0, 0)), (20, 20))
screen.blit(font.render("cardtick:%s" % (card_tick,), True, (0, 0, 0)), (200, 20))
ball_pos.x += ball_v.x * t_passed
ball_pos.y += ball_v.y * t_passed
# 半回合后读取下一次记录
if not over and tick >= next_tick:
d_current = d_next
player = d_current['player']
ball_pos = copy.copy(d_current['ball_pos'])
ball_v = copy.copy(d_current['ball_v'])
tick = d_current['tick']
# 判断是否为最后一次记录
if len(log) > 1:
d_next = getdata(log.pop(0))
next_tick = d_next['tick']
else:
over = True
# 给使用道具一方(跑位方)加上当前道具
card_tick = d_current['card_tick']
if d_current['active_card'][0] is not None:
player_card_history[d_current['side']].append(d_current['active_card'])
while len(player_card_history[d_current['side']]) > 7:
player_card_history[d_current['side']].pop(0)
# 碰到上下墙壁时进行反弹
if ball_pos.y >= DIM[3]:
ball_pos.y = (DIM[3]) * 2 - ball_pos.y
ball_v.y = -ball_v.y
elif ball_pos.y <= DIM[2]:
ball_pos.y = (DIM[2]) * 2 - ball_pos.y
ball_v.y = -ball_v.y
# 更新画面
pygame.display.update()
if __name__ == '__main__':
main()