繁体   English   中英

Python Tkinter在国际象棋游戏中移动一张带有位置的图像

[英]Python Tkinter move one image with position in chess game

我用 Tkinter 用 Python 制作了一个国际象棋游戏,但我遇到了一个大问题。 在此处输入图片说明

该片由矩阵生成。 但是,当一个箱子是空的或者当我移动了一块时,我不能希望(删除)这个箱子。 我想希望(删除)只有一个带有cordonnate的作品,或者只是移动一个作品的图像(我不知道是否可能),有人帮忙吗?

编码:

from tkinter import *
from PIL import Image
import math

root = Tk()
root.title("Game")


frame = Frame(root)
frame.pack()

canvas = Canvas(frame, bg="black", width=500, height=500)
canvas.pack()

background = PhotoImage(file="image/Chessboard.png")
canvas.create_image(250,250,image=background)

pion = PhotoImage(file="image/W_pion.png")

Matrixe= [["","","","","","","",""],
        ["","","","","","","",""],
        ["","","","","","","",""],
        ["","","","","","","",""],
        ["","","","","","","",""],
        ["","","","","","","",""],
        ["W_Pion","W_Pion","W_Pion","W_Pion","W_Pion","W_Pion","W_Pion","W_Pion"],
    ["","","","","","","",""]]

ligne=len(Matrixe)
col=len(Matrixe[0])
def actu():
    for l in range(ligne):
        for c in range(col):     
            if Matrixe[c][l] == "W_Pion":
                canvas.create_image(62.5 * l + 33,62.5 * c + 33,image=pion)
actu()


#mouse input
def getorigin(eventorigin):
    global Posx,Posy, i, piece
    Posx = eventorigin.x
    Posy = eventorigin.y
    
    Xcase = math.ceil((Posx / 62)) - 1
    Ycase = math.ceil((Posy / 62)) - 1
    if Matrixe[Ycase][Xcase] != "":
        global preY , preX, piece
        piece = Matrixe[Ycase][Xcase]
        i = 1
        preY = Ycase
        preX = Xcase
        print(piece)
    if canvas.bind("<Button-1>") and Matrixe[Ycase][Xcase] == "":
        Matrixe[Ycase][Xcase] = piece
        Matrixe[preY][preX] = ""
        print(Matrixe[preY][preX])
        actu()

  canvas.bind("<Button-1>", getorigin)
  root.title("Chess")
  root.iconbitmap("icon.ico")
  root.geometry("500x500")
  root.mainloop()

这是因为您在actu()创建新的棋子组之前没有删除前一组棋子。

创建带有标签的棋子,例如“棋子”,然后您可以使用该标记删除旧的棋子集:

def actu():
    canvas.delete('piece') # delete old chess pieces
    for l in range(ligne):
        for c in range(col):
            if Matrixe[c][l] == "W_Pion":
                canvas.create_image(62.5*l+33, 62.5*c+33, image=pion, tag='piece')

然而,更好的方法是移动选定的棋子而不是重新创建一组棋子:

def actu():
    for l in range(ligne):
        for c in range(col):
            if Matrixe[c][l] == "W_Pion":
                # replace cell content by the canvas item ID
                Matrixe[c][l] = canvas.create_image(62.5*l+33, 62.5*c+33, image=pion, tag=Matrixe[c][l])

actu()

piece = None

def getorigin(eventorigin):
    global preX, preY, piece

    Posx = eventorigin.x
    Posy = eventorigin.y

    Xcase = math.ceil((Posx / 62)) - 1
    Ycase = math.ceil((Posy / 62)) - 1
    if Matrixe[Ycase][Xcase] != "":
        # select the piece
        piece = Matrixe[Ycase][Xcase]
        preY = Ycase
        preX = Xcase
    elif piece:
        # a piece is selected, so move the piece
        canvas.coords(piece, Xcase*62.5+33, Ycase*62.5+33)
        Matrixe[Ycase][Xcase] = piece
        Matrixe[preY][preX] = ""
        piece = None  # deselect the piece

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM