Python Forum

Full Version: python seems to be skipping lines of code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! i wrote tetris in python using pygame the other day and am having a strange problem. There is a section of code that does not seem to run. here is the relevant function
def update():
    #time.time()
    global level, currTimeSD, currTimeBI, currTimeFD, currTimeSAC, timeShiftDown, timeFastDrop, timeBetweenInput, timeShiftAfterClear, kLeft, kRight, kDown, xCursor, yCursor, currMinoRot, currMino, swapMino, currMinoShape, swapMinoShape, alreadySwapped, minoActive, shiftAfterClear

    if time.time() < currTimeSD:
        currTimeSD = 0
    if time.time() < currTimeBI:
        currTimeBI = 0
    if time.time() < currTimeFD:
        currTimeFD = 0
    if time.time() < currTimeSAC:
        currTimeSAC = 0

    if shiftAfterClear == True:
        if time.time() - currTimeSAC >= timeShiftAfterClear:
            print("in shift after clear")
            currTimeSAC = time.time()
            shiftAfterClear = False
            shiftDownBoard()
            currMinoShape = 0
            currMino = generateRandomMino()
            minoActive = True
        return

    if time.time() - currTimeBI >= timeBetweenInput and anyKeyTrue():
        currTimeBI = time.time()
        if kLeft == True and xCursor != 0:
            if checkSafeShiftX(-1) == True:
                xCursor -= 1
        if kRight == True and xCursor != 9:
            if checkSafeShiftX(1) == True:
                xCursor += 1
        
        if kQ == True:
            if currMinoRot == 0:
                safe, newX = checkSafeRot(3)
                if safe:
                    xCursor = newX
                    currMinoRot = 3
            else:
                safe, newX = checkSafeRot(currMinoRot - 1)
                if safe:
                    xCursor = newX
                    currMinoRot -= 1
        if kW == True:
            if currMinoRot == 3:
                safe, newX = checkSafeRot(0)
                if safe:
                    xCursor = newX
                    currMinoRot = 0
            else:
                safe, newX = checkSafeRot(currMinoRot + 1)
                if safe:
                    xCursor = newX
                    currMinoRot += 1

        if kE == True and alreadySwapped == False:
            alreadySwapped = True
            if swapMinoShape == 0:
                swapMinoShape = currMinoShape
                swapMino = currMino
                currMino = generateRandomMino()
            else:
                swpms = swapMinoShape
                swpm = swapMino
                swapMinoShape = currMinoShape
                swapMino = currMino
                currMinoShape = swpms
                currMino = swpm
                xCursor, yCursor = generateStartCoord()
        
        if kUp == True:
            dropDownAndAdd()
            alreadySwapped = False
            checkCleared = clearBoard()
            if checkCleared == True:
                minoActive = False
                shiftAfterClear = True
                currTimeSAC = time.time()
            else:
                currMinoShape = 0
                currMino = generateRandomMino()
        if kEsc == True:
            print("Goodbye!")
            exit()

    if (time.time() - currTimeSD >= timeShiftDown) or (time.time() - currTimeFD >= timeFastDrop and kDown == True):
        currTimeFD = time.time()
        currTimeSD = time.time()
        if checkSafeShiftY() == True:
            yCursor += 1
        else:
            print("hi there", flush = True)
            addToBoard()
            for a in range(10):
                if board[a][0] == 1:
                    print("Sorry, you lose!")
                    exit()
            alreadySwapped = False
            checkCleared = clearBoard()
            print("whatup", flush = True)
            if checkCleared == True:
                print("genius", flush=True)
                print("whatup1", flush = True)
                tmpLevel = level
                level = 5 #int(linesCleared / 10)
                if tmpLevel != level:
                    if timeShiftDown - 0.08 < 0.03:
                        timeShiftDown = 0.03 
                    else:
                        timeShiftDown -= 0.08
                minoActive = False
                shiftAfterClear = True
                currTimeSAC = time.time()
            else:
                currMinoShape = 0
                currMino = generateRandomMino()
    return
down near the bottom of the code this block:

                print("genius", flush=True)
                print("whatup1", flush = True)
                tmpLevel = level
                level = 5 #int(linesCleared / 10)
                if tmpLevel != level:
                    if timeShiftDown - 0.08 < 0.03:
                        timeShiftDown = 0.03 
                    else:
                        timeShiftDown -= 0.08
does not run. when i move it up to the
if shiftAfterClear == True:

block near the top of the function everything runs fine. Any idea what is happening? thanks!
tmpLevel = level
level = 5 #int(linesCleared / 10)
This is not your problem, but the second line of code here overwrites the previous
remove line 105

before if timeShiftDown - 0.08 < 0.03:
add: print(f"in loop, tmpLevel: {tmpLevel}"

show results