0

我正在尝试为我在 python 中编码的俄罗斯方块创建一个旋转程序。旋转部分工作正常,但我遇到了块从屏幕上旋转出来或进入其他块的问题。我试图创建的是一个函数,它将在实际旋转块之前进行测试旋转以查看旋转是否有效。

函数在这里被调用:

 if rotatecheck(curblock,rowheight,columnwidth):
        rotate(curblock,rowheight,columnwidth)

其中curblock是一个包含 4 个矩形对象和 1 种颜色的列表,columnwidth并且rowheight是定义游戏空间大小的整数,rotatecheck()定义为

def rotatecheck(curc, ro, col):
    new=[]
    ct=len(blockonscreen)-1
    if ct==0:
        ct=1
    for z in range(len(curc)):
        new.append(curc[z])
    rotate(new, ro, col)
    for i in range(len(new)-1):
        if new[i].left<0 or new[i].left>=WINDOWWIDTH:
            return False
        for b in blockonscreen:
            if b !=curc:
                for x in range(len(block)-1):
                    if new[i] == block[x]:
                        return False
    return True

blockonscreen是包含 4 个矩形对象和一种颜色的列表列表,rotate()定义为

def rotate(curr,ro,col):
     if curr[4]==yellow:
         rotateo(curr,ro,col)
     elif curr[4]==cyan:
         rotatei(curr,ro,col)
     else:
         rotatea(curr,ro,col)

def rotatei(curr,ro,col):
    if curr[2].centerx>curr[0].centerx and curr[3].centerx>curr[0].centerx:
        horiz=True
        curr[0].centerx+=col
        bla=1
    elif curr[2].centerx<curr[0].centerx and curr[3].centerx<curr[0].centerx:
        horiz=True
        curr[0].centerx-=col
        bla=-1
    elif curr[2].centery>curr[0].centery and curr[3].centery>curr[0].centery:
        horiz=False
        curr[0].centery+=ro
        bla=-1
    elif curr[2].centery<curr[0].centery and curr[3].centery<curr[0].centery:
        horiz=False
        curr[0].centery-=ro
        bla=1
    for f in range (1,4):
        if horiz:
            curr[f].left=curr[0].left
            curr[f].top=curr[0].top+(blocks[0][f-1][0]*ro*bla)
        else:
            curr[f].top=curr[0].top
            curr[f].right=curr[0].right+(blocks[0][f-1][0]*col*bla)
def rotateo(curr,ro,col):
    pass
def rotatea(curr,ro,col):
    middlex=curr[0].centerx
    middley=curr[0].centery
    for r in range(1,4):
        if curr[r].centery==middley and curr[r].centerx>middlex:
            curr[r].centery+=ro
            curr[r].centerx-=col
        elif curr[r].centery==middley and curr[r].centerx<middlex:
            curr[r].centery-=ro
            curr[r].centerx+=col
        elif curr[r].centerx==middlex and curr[r].centery>middley:
            curr[r].centery-=ro
            curr[r].centerx-=col            
        elif curr[r].centerx==middlex and curr[r].centery<middley:
            curr[r].centery+=ro
            curr[r].centerx+=col            
        elif curr[r].centerx>middlex and curr[r].centery>middley:
            curr[r].centerx-=(2*col)
        elif curr[r].centery>middley and curr[r].centerx<middlex:
            curr[r].centery-=(2*ro)
        elif curr[r].centerx<middlex and curr[r].centery<middley:
            curr[r].centerx+=(2*col)
        elif curr[r].centerx>middlex and curr[r].centery<middley:
            curr[r].centery+=(2*ro)

在其当前状态下,该程序的行为就像rotatecheck()不存在一样。我反复搜索了我的代码,但我找不到任何问题。

4

1 回答 1

1

看起来您的问题是rotatecheck实际上rotates 您要测试的对象,rotate即使确实如此,也会导致它们成为 d return False

for z in range(len(curc)):
    new.append(curc[z])

centerxcentery保存在 中的对象中curc,当您执行此操作时,您将使用对相同对象的引用填充一个新列表,然后旋转这些对象。您将需要在其中创建各个对象的副本curc;定义一个copy创建具有相同值的新对象的方法,例如centerx

于 2013-12-17T17:26:32.217 回答