Pausing Pygame

Sometimes when developing games, what you see on the screen happens too fast for you to make sense of it. You can try print statements, or you can take the time to learn an external debugger, which would probably be overkill for what you want it to do right now. One solution i've found when writing games for pygame is to just not let the main while loop finish until you're ready. To that end, I did this:

    keepGoing = True
    while keepGoing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        pygame.display.update()
        NextFrame = False
        while not NextFrame:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    keepGoing = False
                if event.type == pygame.KEYDOWN:
                    NextFrame = True

This code allows no further input when it is running. Since you're waiting on this to finish, this means that you have literally no input. Also, it isn't possible to go backward. A keypress on any key will let it move onto the next frame. To use, just add the last while loop to the end of your main loop, as shown

Storing a timedelta in a sqlite3 database

I found myself needing to store a datetime.timedelta object in a sqlite3 database. However, despite having adaptors for datetime.datetime objects, timedelta misses out. Through googling/irc, i found that it would be best to store the timedelta as a string. An alternative is to store the start and end times of the timedelta as datetime.datetime objects (which can be stored easily), however I chose not to.

The Code

def convertTime(t):
    """Converts a timedelta to a string. ignores days and annoying decimal places
    compatible with strToTD
    """
    st = str(t)
    st = st.split(".")
    st[-1] = st[-1][:2]
    s = ":".join(st)
    return s
    
def strToTD(s):
    """converts a string that was a timedelta (having been converted using
    convertTime) back into a timedelta
    """
    s = [int(x) for x in s.split(":")]
    ms = s[3]
    sec = s[2] + ms/100.0
    m = s[1]
    h = s[0]
    td = datetime.timedelta(hours = h, minutes = m, seconds = sec)#, microseconds = us)
    return td

While this is only good for up house:minute:seconds:micro/milli-seconds, it is an alternative to storing the start and end times

Topics