```python
#coding=utf-8
import turtle
#a=int(input("xxx:"))
a=50
b=a*8
t=turtle.Pen()
t.hideturtle()
t.speed(0)
t.penup()
t.goto(-b/2,-b/2)
t.pendown()
for i in range(4):
t.forward(b)
t.left(90)
t.penup()
for i in range(8):
t.goto(-b/2+a*i,-b/2)
t.setheading(90)
t.pendown()
t.forward(b)
t.backward(b)
t.penup()
t.goto(b/2,b/2-a)
t.setheading(0)
for i in range(1,9):
for j in range(1,9):
t.pendown()
#t.color("black", "black")
t.begin_fill()
if (i+j)%2==0:
t.fillcolor("gray")
else:
t.fillcolor("white")
for x in range(5):
t.backward(a)
t.right(90)
t.left(90)
t.end_fill()
t.penup()
t.goto(b/2,b/2-a-a*i)
t.hideturtle()
turtle.register_shape("./pics/wp.gif")
wpa=turtle.Pen()
wpb=turtle.Pen()
wpc=turtle.Pen()
wpd=turtle.Pen()
wpe=turtle.Pen()
wpf=turtle.Pen()
wpg=turtle.Pen()
wph=turtle.Pen()
sets=[wpa,wpb,wpc,wpd,wpe,wpf,wpg,wph]
for i in range(len(sets)):
sets[i].shape("./pics/wp.gif")
sets[i].penup()
sets[i].goto(-b/2+a/2+a*(i),-b/2+a/2+a)
bpa=turtle.Pen()
bpb=turtle.Pen()
bpc=turtle.Pen()
bpd=turtle.Pen()
bpe=turtle.Pen()
bpf=turtle.Pen()
bpg=turtle.Pen()
bph=turtle.Pen()
bsets=[bpa,bpb,bpc,bpd,bpe,bpf,bpg,bph]
turtle.register_shape("./pics/bp3.gif")
for i in range(len(bsets)):
bsets[i].shape("./pics/bp3.gif")
bsets[i].penup()
bsets[i].goto(-b/2+a/2+a*(i),b/2-a/2-a)
bra=turtle.Pen()
brh=turtle.Pen()
turtle.register_shape("./pics/br.gif")
bra.shape("./pics/br.gif"); brh.shape("./pics/br.gif")
bra.penup(); brh.penup()
bra.goto(-b/2+a/2,b/2-a/2);brh.goto(b/2-a/2,b/2-a/2)
wra=turtle.Pen()
wrh=turtle.Pen()
turtle.register_shape("./pics/wr.gif")
wra.shape("./pics/wr.gif"); wrh.shape("./pics/wr.gif")
wra.penup(); wrh.penup()
wra.goto(-b/2+a/2,-b/2+a/2);wrh.goto(b/2-a/2,-b/2+a/2)
bnb=turtle.Pen()
bng=turtle.Pen()
turtle.register_shape("./pics/bn.gif")
bnb.shape("./pics/bn.gif");bng.shape("./pics/bn.gif")
bnb.penup();bng.penup()
bnb.goto(-b/2+a/2+a,b/2-a/2);bng.goto(b/2-a/2-a,b/2-a/2)
wnb=turtle.Pen()
wng=turtle.Pen()
turtle.register_shape("./pics/wn.gif")
wnb.shape("./pics/wn.gif");wng.shape("./pics/wn.gif")
wnb.penup();wng.penup()
wnb.goto(-b/2+a/2+a,-b/2+a/2);wng.goto(b/2-a/2-a,-b/2+a/2)
bbc=turtle.Pen()
bbf=turtle.Pen()
turtle.register_shape("./pics/bb.gif")
bbc.shape("./pics/bb.gif");bbf.shape("./pics/bb.gif")
bbc.penup();bbf.penup()
bbc.goto(-b/2+a/2+a*2,b/2-a/2);bbf.goto(b/2-a/2-a*2,b/2-a/2)
wbc=turtle.Pen()
wbf=turtle.Pen()
turtle.register_shape("./pics/wb.gif")
wbc.shape("./pics/wb.gif");wbf.shape("./pics/wb.gif")
wbc.penup();wbf.penup()
wbc.goto(-b/2+a/2+a*2,-b/2+a/2);wbf.goto(b/2-a/2-a*2,-b/2+a/2)
bq=turtle.Pen()
turtle.register_shape("./pics/bq.gif")
bq.shape("./pics/bq.gif")
bq.penup()
bq.goto(-b/2+a/2+a*3,b/2-a/2)
wq=turtle.Pen()
turtle.register_shape("./pics/wq.gif")
wq.shape("./pics/wq.gif")
wq.penup()
wq.goto(-b/2+a/2+a*3,-b/2+a/2)
bk=turtle.Pen()
turtle.register_shape("./pics/bk.gif")
bk.shape("./pics/bk.gif")
bk.penup()
bk.goto(-b/2+a/2+a*4,b/2-a/2)
wk=turtle.Pen()
turtle.register_shape("./pics/wk.gif")
wk.shape("./pics/wk.gif")
wk.penup()
wk.goto(-b/2+a/2+a*4,-b/2+a/2)
##TODO
qp="Pe2-e4 Pe7-e6 Ke1-e2 Ng8-f6"
##TODO
d=qp.strip().split()
white=[]
black=[]
swwh=0
for i in d:
if swwh%2==0:
white+=i.strip("QBPNKR").split("-")
else:
black+=i.strip("QBPNKR").split("-")
swwh+=1
print(white,black)
keys=[]
values=[]
for i in range(8):
for j in range(8):
g=chr(97+j)+str(i+1)
keys.append(g)
values.append((-b/2+a/2+a*j,-b/2+a/2+a*i))
pos=dict(zip(keys,values))
print(pos)
print(wpe.position())
print(wk.position())
wpss=[]
bpss=[]
wgo=[]
bgo=[]
for i in white:
wpss.append(pos[i])
for j in black:
bpss.append(pos[j])
wpsss=wpss
bpsss=bpss
print(len(wpss))
allof=sets+bsets+[wnb,wng,wra,wrh,wbc,wbf,wq,wk,bnb,bng,bra,brh,bbc,bbf,bq,bk]
for ii in range(len(allof)):
if allof[ii].position() == wpss[0]:
wgo.append(allof[ii])
del wpss[0]
del wpss[0]
for iii in range(len(allof)):
if allof[iii].position() == bpss[0]:
bgo.append(allof[iii])
del bpss[0]
del bpss[0]
times=0
def got():
global times
global wgo,wpsss
global bgo,bpsss
try:
wgo[times].pendown()
wgo[times].goto(wpsss[times])
wgo[times].penup()
bgo[times].goto(bpsss[times])
wgo[times].clear()
except:
print("谱打完了。")
times=times+1
turtle.onkey(lambda:got(),"Right")
turtle.mainloop()
代码目的:用Turtle画一个棋盘,并通过程序实现根据qp的棋谱走棋。
附:报错内容
```python
Traceback (most recent call last):
File "C:\Users\86158\Desktop\MEME\chess\board.py", line 176, in <module>
if allof[ii].position() == wpss[0]:
IndexError: list index out of range