Reputation: 57
I have a Tetris-like game, where the user controls a block of a random color, and then drops that block to the bottom, where it locks in place and then spawns a new block at the top of the screen. I want to "clear" whenever two blocks of the same color are placed next to each other, and for whatever blocks were above those cleared blocks to move down, as if they were affected by gravity.
I've tried using the collision list to no avail.
Upvotes: 2
Views: 95
Reputation: 210948
You have to find adjoining blocks with the same color and to delete them from copylist
. This has to be done, every time when a block reaches the bottom (e.g. in chooseBlock)
.
Find the indices of adjoining blocks and store them to a set()
. Note, in a set the indices are unique:
e.g.:
adjoining = set()
for i in range(len(copylist)):
for j in range(len(copylist)):
if i == j or colorList[i] != colorList[j]:
continue
if (copylist[i][0] == copylist[j][0] and abs(copylist[i][1]-copylist[j][1]) == height) or \
(copylist[i][1] == copylist[j][1] and abs(copylist[i][0]-copylist[j][0]) == width):
adjoining.add(i)
adjoining.add(j)
Create a new copylist
and colorList
, with contains only that elements, whose index is not contained in adjoining
:
copylist = [copylist[i] for i in range(len(copylist)) if i not in adjoining]
colorList = [colorList[i] for i in range(len(colorList)) if i not in adjoining]
At the end you've to find all the blocks which have to be dropped to the ground, because the block below was deleted.
Move down a block in not any()
block is below:
e.g.
for i in range(len(copylist)):
if copylist[i][1] >= 390:
continue
if not any([copylist[j] for j in range(len(copylist)) \
if i != j and copylist[i][0] == copylist[j][0] and copylist[i][1] + height == copylist[j][1]]):
copylist[i][1] += height
Do the algorithm in chooseBlock
and repeat it until adjoining blocks are found:
def chooseBlock():
global player, copylist, colorList, colorChoice, blocksLeft
#running list of blocks and their positions once placed
copylist.append(player.copy())
#running list of those blocks' colors
colorList.append(colorChoice)
#resetting player position
player.y = 50
#choosing the color of the next block randomly from a list
colorChoice = random.choice(colors)
#subtracting 1 from the count of the blocks left to place after one is placed
blocksLeft = blocksLeft - 1
while True:
adjoining = set()
for i in range(len(copylist)):
for j in range(len(copylist)):
if i == j or colorList[i] != colorList[j]:
continue
if (copylist[i][0] == copylist[j][0] and abs(copylist[i][1]-copylist[j][1]) == height) or \
(copylist[i][1] == copylist[j][1] and abs(copylist[i][0]-copylist[j][0]) == width):
adjoining.add(i)
adjoining.add(j)
if not adjoining:
break
copylist = [copylist[i] for i in range(len(copylist)) if i not in adjoining]
colorList = [colorList[i] for i in range(len(colorList)) if i not in adjoining]
testmovedown = True
while testmovedown:
testmovedown = False
for i in range(len(copylist)):
if copylist[i][1] >= 390:
continue
if not any([copylist[j] for j in range(len(copylist)) \
if i != j and copylist[i][0] == copylist[j][0] and copylist[i][1] + height == copylist[j][1]]):
copylist[i][1] += height
testmovedown = True
Upvotes: 1