首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python Tkinter在国际象棋游戏中移动一个带有位置的图像

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

Stack Overflow用户
提问于 2021-11-04 10:27:54
回答 1查看 207关注 0票数 0

我用Python和Tkinter做了一个国际象棋游戏,但我有一个很大的问题。

该片段是由矩阵生成的。但是,当一个盒子是空的,或者当我移动了一块时,我不希望(删除)这个盒子。我希望(删除)只有一件与协和或只是移动图像的一件(我不知道这是否可能),有人帮助吗?

代码:

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2021-11-04 16:15:56

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

创建带有标签的棋子,例如‘棋子’,然后您可以使用该标签删除旧的一组棋子:

代码语言:javascript
运行
复制
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')

但是,更好的方法是只移动选定的片段,而不是重新创建一组片段:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69837750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档