0

我一直在这个棋子的棋码中遇到错误。(默认 apawn+ 拖动())。它们中的大多数是合乎逻辑的,例如 x1 和 x2 在释放 pawn 时不会更新。但是每当我尝试解决这个问题时,我都会引入一大堆新错误。关于导入国际象棋,目前还没有错误,因此您可以将它们注释掉(如果您收到有关 a2a2 的错误,则表明错误是由坐标代码引起的)或使用导入。

from turtle import Screen, Turtle
import chess

screen = Screen()
screen.title("Chess")
screen.setup(600, 600)
xnames = {1:'a', 2:'b',3:'c',4:'d',5:'e',6:'f',7:'g', 8:'h'}
x = 0
y = 0
x1 = 0 
y1 = 0
x2 = 0
y2 = 0

def cor1(self):
        x = self.xcor()
        return x
def cor2(self):
        y = self.ycor()
        return y
def drag(self):
  global x2
  global y2
  x2=self.xcor()
  y2=self.ycor() 
  #checking the piece is not off the board
  if 300<x2<-300:
    self.goto(x1,y1)
    #returning the piece to the square it started at
  if 300<y1<-300:
    self.goto(x1,y1)
  
  
                                                      
def setup1():#drawing of the board-error free for now
        base1.penup()
        base1.goto(-300, -300)
        base1.speed(0)
        base1.pendown()
        for x in range(4):
                for y in range(4):
                        base1.left(90)
                        base1.begin_fill()
                        for i in range(4):
                                base1.forward(75)
                                base1.right(90)
                        base1.end_fill()
                        base1.right(90)
                        base1.forward(150)
                base1.left(90)
                base1.forward(150)
                base1.left(90)
                base1.forward(600)
                base1.left(180)
        base1.goto(-300, -225)
        base1.forward(75)
        for x in range(4):
                for y in range(4):
                        base1.left(90)
                        base1.begin_fill()
                        for i in range(4):
                                base1.forward(75)
                                base1.right(90)
                        base1.end_fill()
                        if y != 3:
                                base1.right(90)
                                base1.forward(150)
                        else:
                                base1.right(90)
                                base1.forward(75)
                if x != 3:
                        base1.left(90)
                        base1.forward(150)
                        base1.left(90)
                        base1.forward(525)
                        base1.left(180)
                else:
                        base1.hideturtle()


def awpawn():#a file pawn
  screen.register_shape('w.gif')
  awp = Turtle()
  awp.shape('w.gif')
  awp.penup()
  awp.goto(-262.5, -187.5)
  while wtmove == True:   
    awp.ondrag(awp.goto)#pawn will follow drag
    awp.onrelease(drag(awp))#correcting pawns position to centre after release
    x1 = cor1(awp)
    y1 = cor2(awp)
    sqx1= x1//37.5#gettin previous sqr pawn was on
    sqy1= y1//37.5
    sqx= x2//37.5#getting target sqr for pawn
    sqy= y2//37.5
  #making negatie sqr postive
  if sqx<0:
    sqx+=8 
  if sqy<0:
    sqy+=7 #187.5 /37.5 is -5 then 3 not 2 
  if sqx1<0:
    sqx1+=8 
  if sqy1<0:
    sqy1+=7
  amove = xnames[int(sqx)] +str(int(sqy))#string for telling the modul what piece has been moved
  move= str(xnames[int(sqx1)] +str(int(sqy1))+xnames[int(sqx)] +str(int(sqy)))#string to check if move is legal
  #checking if the move is not to the square that the pawn is already on
  if move != str(xnames[int(sqx1)]) +str(int(sqy1))+str(xnames[int(sqx1)]) +str(int(sqy1)):
    #checking if the move is legal
    if chess.Move.from_uci(move) in board.legal_moves== True:
      wtmove == False#making it black turn to move
      awp.goto((x2//37.5)*37.5,(y2//37.5)*37.5)#making the pawn go to the centre of the square
      board.push_san(amove) #telling the module the move that has been moved
  else:
    awp.goto((x1//37.5)*37.5,(y1//37.5)*37.5)#returning the pawn to square it stared on as the move was not legal
                
                  
                


wtmove = True
base1 = Turtle()
base1.fillcolor("peru")
board = chess.Board()
setup1()
awpawn()
4

0 回答 0