Python 涉及函数、变量未在事件发生时更新的错误

Python 涉及函数、变量未在事件发生时更新的错误,python,function,variables,python-turtle,python-chess,Python,Function,Variables,Python Turtle,Python Chess,我一直面临着a兵的国际象棋代码错误。(Def apawn+拖动()。大多数都是逻辑的,例如x1和x2在典当被释放时不更新。但每当我试图解决这个问题时,就会引入一大堆新的错误。 关于导入国际象棋,那里还没有错误,因此您可以将它们注释掉(如果您得到关于a2a2的错误,该错误是由坐标代码引起的),或者使用导入 from turtle import Screen, Turtle import chess screen = Screen() screen.title("Chess")

我一直面临着a兵的国际象棋代码错误。(Def apawn+拖动()。大多数都是逻辑的,例如x1和x2在典当被释放时不更新。但每当我试图解决这个问题时,就会引入一大堆新的错误。 关于导入国际象棋,那里还没有错误,因此您可以将它们注释掉(如果您得到关于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()
从海龟导入屏幕,海龟
进口象棋
screen=screen()
屏幕标题(“国际象棋”)
屏幕设置(600600)
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(自身):
x=self.xcor()
返回x
def cor2(自身):
y=self.ycor()
返回y
def阻力(自):
全局x2
全球y2
x2=self.xcor()
y2=self.ycor()
#检查工件是否未脱离板
如果300