I'm trying to create a rotation program for Tetris that I am coding in python. The rotation part works correctly but I have the problem of blocks rotating off the screen or into other blocks. What I have tried to create is a function that will make a test rotation to see if the rotation is valid before actually rotating the block.
The functions are called here:
if rotatecheck(curblock,rowheight,columnwidth):
rotate(curblock,rowheight,columnwidth)
where curblock
is a list with 4 rect objects and 1 color, columnwidth
and rowheight
are integers defining the size of the playspace and rotatecheck()
is defined as
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
is a list of lists containing 4 rect objects and a color, and rotate()
is defined as
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)
In its current state the program acts as if rotatecheck()
doesn't exist. I have scoured my code repeatedly but I can't find anything wrong with it.
It looks like your problem here is that
rotatecheck
actuallyrotate
s the objects you are trying to test, causing them to berotate
d even if it doesreturn False
.centerx
andcentery
are held in the objects incurc
, when you do this you fill a new list with references to the same objects, which are then rotated. You will need to create copies of the individual objects withincurc
; define acopy
method which creates a new object with the same values of e.g.centerx
.