如何对图像进行拖放?

问题描述

所以我正在制作一个带有可移动棋子的棋盘。我已经创建了棋盘的碎片,并渲染了这些碎片。我正在使用字典来获取碎片或碎片 IMAGES = {} 的图像。所以现在我必须为字典中的每个国际象棋图像创建一个精灵。我已经在这里完成了:

import pygame as py
import ChessEngine

py.init()
WIDTH = 512
HEIGHT = 512
DIMENSION = 8
SQ_SIZE = 512 // DIMENSION
screen = py.display.set_mode((WIDTH,HEIGHT))
MAX_FPS = 60

def loadImages():
    pieces = ['wp','wR','wN','wB','wK','wQ','bp','bR','bN','bB','bK','bQ']
    for piece in pieces:
        IMAGES[piece] = py.transform.scale(py.image.load("images/" + piece + ".png"),(SQ_SIZE,SQ_SIZE))
    # Note we can access an image by saying "IMAGES['wp']'


class DragInDrop(py.sprite.Sprite):
    def __init__(self,image,initpos):
        py.sprite.Sprite.__init__(self)
        self.pos = initpos
        self.image,self.rect = image
        self.button = (0,0)
        self.selected = 0
        self.mouSEOver = False
        self.focused = False

    def rollover(self):
        mpos = py.mouse.get_pos()
        if self.rect.collidepoint(mpos):
            self.mouSEOver = True
        else:
            self.mouSEOver = False

    def update(self):
        self.button = py.mouse.get_pressed()
        mpos = py.mouse.get_pos()
        self.selected = self.rect.collidepoint(mpos)
        if self.mouSEOver:
            if self.button == (1,0):
                pos = py.mouse.get_pos()
                self.rect.midtop = pos


def main():
    running = True
    loadImages()
    py.mouse.set_visible(True)
    gs = ChessEngine.GameState()
    allsprites = py.sprite.RenderPlain(IMAGES)
    i = DragInDrop
    while running:
        for e in py.event.get():
            if e.type == py.QUIT:
                running = False
            if e.type == py.MOUSEBUTTONDOWN:
                for i in IMAGES:
                    i.rollover()
        allsprites.update()
        allsprites.draw(screen)
        drawGameState(screen,gs)
        py.display.update()

一个导入:import ChessEngine 仅用于我执行所有国际象棋移动和国际象棋规则的地方。所以没什么重要的。

现在每当我尝试运行此代码时,它都会弹出:

RecursionError: maximum recursion depth exceeded while calling a Python object

现在我不知道这对精灵意味着什么,但我想做的就是通过字典在板上移动这些图像。我不能给每个图像一个坐标,因为每个国际象棋位置都有不同的位置。我不知道我做错了什么。如果我必须实现其他任何东西,我不知道会是什么。现在,除了使用 pygame 之外,还有其他加载这些图像的方式吗,我不知道。

感谢您的帮助,如果您需要更多信息或代码,请告诉我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)