Attempting to make tetris game- Collision code is not working properly

54 Views Asked by At

I am working on a Tetris game from scratch. The code for colliding with other objects seems like it would work, but it just isn't. Please note that the collision code is not finished. Not looking for advice on how to improve collisions, just need help on debugging it and finding out why it is erroring. All code that is relevant to collisions is listed below.

Collision Code

def getCols():
    global wait, pause
    # gets col with floor
    if 2 in piece[0]:
        if piece[2] + 50 == 500:
            wait = 1.5
            return True
    else:
        if piece[2] + 25 == 500:
            wait = 1.5
            return True
            

    # gets col with other pieces
    i = 0

    for coords in pieceIndCoords:
        # if iterating over current piece, skip
        if i == 0:
            pass
        else:
            for coord in coords:
                player = pieceIndCoords[0]
                for p in player:
                    if coord[0] == p[0] and coord[1] <= p[1] + 25:
                        return True
        i += 1
    return False

If collision function returns True

        if spawn:
            spawnPiece()
            spawn = False
        else:
            moveDown()

        if getCols():
            spawn = True

Spawns a new Tetris piece

def spawnPiece():
    global piece, pieces, piecesOnBoard
    piece = [getRandomPiece()]
    piece.append(getSpawnCoords(piece)[0])
    piece.append(getSpawnCoords(piece)[1])
    piecesOnBoard.insert(0, piece)

    return piece

Draws all pieces onto board This function is where I think the problem is occurring but I can't seem to figure out why. Inside this function is also where all coordinates of individual pieces are stored (i.e. the coordinates of the units in the tetris blocks)
It may also be of note that this function is stored in a separate file.

def drawPieces():
    global indCoords
    pieces = gamedata.getPieces()
    indCoordsWhole = []
    indCoords = []

    for piece in pieces:
        pieceObject = piece[0]
        indCoords = []
        x = piece[1]
        y = piece[2]

        for c in pieceObject:

            if c == 0:
                x += 25
            elif c == 1:
                pygame.draw.rect(screen, gamedata.getPieceColor(piece), pygame.Rect((x, y), (25, 25)))
                indCoords.append([x, y])
                x += 25
            elif c == 2:
                y += 25
                x = piece[1]
            elif c == 3:
                break
        indCoordsWhole.append(indCoords)
    gamedata.updateIndCoords(indCoordsWhole)

Blocks collide with the ground properly, but when the blocks spawn they immediately are detecting a collision with something and the block gets stuck on top of the screen. The blocks then keep spawning continuously and piling up on top of the screen.
I am okay with needing to rewrite much of the code if need be.

0

There are 0 best solutions below