Adding a number to a variable in a while true loop once in python

Viewed 164

I am calling a fucntion for a pygame so i need to have it in a loop and i want to add 50 to a variable called sy=100. The function reads a directory looking for any python files and if it finds them i want to display the first file at 100 which is the start point of sy. then i want to add 50 to the variable and then it write the next file at the next point without moving the first one so far it just writes the text all at the same point and moves it down the screen so how would i get it so each file is seperated by 50 pixels and the text doesn't move.

Heres my Function

def games():
    f = 0
    sy = 100
    file = []
    ftext = pygame.font.SysFont("Arial", 20)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        screen.fill(DarkSpace)
        ToolBarButton("Home", 0, 0, 150, 50, SpaceGrey, CornflowerBlue, 10, cmain)
        ToolBarButton(username, 153,0,150,50, SpaceGrey, CornflowerBlue, 10, accountDetails)
        ToolBarButton("Programs", 305,0,150,50, SpaceGrey, CornflowerBlue, 10, programs)
        ToolBarButton("Games", 458,0,150,50, SpaceGrey, CornflowerBlue, 10, games)
        ToolBarButton("Help", 610,0,150,50, SpaceGrey, CornflowerBlue, 10, hel)
        DropDown(NeonGreen, CornflowerBlue, 764, 16, 30, 30, DropMenu)
        Btext(screen, "Loading Games!", CornflowerBlue, ftext, 600,600,600,600)
        fileDir = os.listdir("Games\\")
        for fileN in fileDir:
            verif = fileN.endswith('.py') or fileN.endswith('.pyw')
            if not verif:
                fileDir.remove(fileN)
            else:
                text(screen, fileN, CornflowerBlue, ftext, 400,sy)


        pygame.display.update()
1 Answers

So i tried figuring it out and i did! So i add the filtered files to a list and then do another for loop to break them up and then i write them to the screen. I also took the part where i read the directory and filter it for certian files out of the loop to make it eaiser new function

def games():
    file = []
    ftext = pygame.font.SysFont("Arial", 20)
    fileDir = os.listdir("Games\\")
    for fileN in fileDir:
        verif = fileN.endswith('.py') or fileN.endswith('.pyw')
        if not verif:
            fileDir.remove(fileN)
        else:
            file.append(fileN)
    while True:
        sy = 100
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        screen.fill(DarkSpace)
        ToolBarButton("Home", 0, 0, 150, 50, SpaceGrey, CornflowerBlue, 10, cmain)
        ToolBarButton(username, 153,0,150,50, SpaceGrey, CornflowerBlue, 10, accountDetails)
        ToolBarButton("Programs", 305,0,150,50, SpaceGrey, CornflowerBlue, 10, programs)
        ToolBarButton("Games", 458,0,150,50, SpaceGrey, CornflowerBlue, 10, games)
        ToolBarButton("Help", 610,0,150,50, SpaceGrey, CornflowerBlue, 10, hel)
        DropDown(NeonGreen, CornflowerBlue, 764, 16, 30, 30, DropMenu)
        for data in file:
            sy += 50
            text(screen, data, NeonGreen, ftext, 400,sy)
        pygame.display.update()

If anyone has a btter way or a different solution please let me know!

Related