Python screen is black for a small amount of time then closes
Question: The code in this gist
(<https://gist.github.com/tobias76/8dc2e1af90f1916a2106>) is completely broken
and nothing happens excl. a black screen and closure after seconds, I would be
more specific with the code but there is absolutely no error message. As far
as the program is concerned, it worked. As requested, here is my code (For
some reason stack overflow doesn't like all of the code being formatted);

==============================================

    
    
        import pygame
        import sys
        import random
    
        from pygame.locals import *
    
        pygame.init()
    
        FPS = 60
        FPSClock = pygame.time.Clock()
    
        screen = pygame.display.set_mode((800, 600))
        pygame.display.set_caption("Graphical Slot Machine")
    
        reelgroupone = pygame.sprite.Group()
        reelgrouptwo = pygame.sprite.Group()
        reelgroupthree = pygame.sprite.Group()
    
        reelone = (reelgroupone, 1)
        reeltwo = (reelgrouptwo, 2)
        reelthree = (reelgroupthree, 3)
    
        image = "assets/apple.png"
        image2 = "assets/bar.png"
        image3 = "assets/cherry.png"
        image4 = "assets/orange.png"
        background = "background.jpg"
    
        class Reels(pygame.sprite.Sprite):
            def __init__(self, reelgroup, reelnumbers):
                self.reelgroup = reelgroup
                self.reelnumber = reelnumbers
                self.reellist = [0, 1, 2, 3, 4, 5]
                self.reelmove = 1
                self.reelnudge = 0
                self.stoptime = 60
    
            def start(self):
                 self.reelmove = 1
            self.stoptime = 60
    
        def stop(self):
            self.reelmove = 0
    
        def update(self):
            if self.stoptime > 0:
                self.stoptime -= 1
                self.reelgroup.update()
            else:
                self.stop()
            if self.reelnudge == 1:
                self.reelmove = 0
    
        def draw(self):
            self.reelgroup.draw(screen)
    
    class Fruit(pygame.sprite.Sprite):
    
        def __init__(self, reelgroup, reel, FruitID):
            pygame.sprite.Sprite.__init__(self)
            self.reelgroup = reelgroup
            self.FruitID = FruitID
            self.machineReel = reel
    
            # Depending on Reel ID use a specific image, place the file path in the string
            if self.FruitID == 1:
                self.reelImage = "assets/apple.png"
            if self.FruitID == 2:
                self.reelImage = "assets/bar.png"
            if self.FruitID == 3:
                self.reelImage = "assets/cherry.png"
            if self.FruitID == 4:
                self.picture = "assets/orange.png"
    
            self.pic = pygame.image.load(self.reelImage).convert_alpha()
            self.where = ((self.machineReel * 155) - 30, 490)
            self.rectangle = self.pic.get_rect()
            self.rectangle.TopLeft = self.where
            # Make reels faster / slower here.
            self.reelSpeed = 8
            self.reelgroup.add(self)
    
        def fruitupdate(self):
            self.rectangle.y -= self.reelSpeed
            if self.rectangle.y < 110:
                self.kill()
    
    
    class main():
        def __init__(self):
            self.money = 10
            self.counter = 5
            self.fruitlist = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
            self.finished = 0
            self.message = ""
    
        def splash(self):
            pass
    
        def machine(self):
            while self.credits > 0:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()
                self.keys = pygame.key.get_pressed()
    
                # Change this key to whatever you want
                if self.keys[K_p] and reelone.reelmove == reeltwo.reelmove == reelthree.reelmove == 0:
                    self.money -= 1
                    self.counter = 5
                    self.finished = 0
                    reelone.start()
                    reeltwo.start()
                    reelthree.start()
                    self.message = ""
                if self.keys[K_ESCAPE]:
                    pygame.quit()
                    sys.exit()
    
                # Put your image and font blitting here.
    
                screen.blit(background,(0,0))
    
                if self.money >= 10:
                    self.counter = 1
                    if reelone.reelMove == 1 and reelone.stopTime % 10:
                        fruit = Fruit(reelgroupone, 1, random.randint(1,4))
                        del self.fruitlist[0][0]
                        self.fruitlist[0].append(fruit.ID)
                    if reeltwo.reelMove == 1 and reeltwo.stopTime % 10:
                        fruit = Fruit(reelgrouptwo, 2, random.randint(1,4))
                        del self.fruitlist[1][0]
                        self.fruitlist[1].append(fruit.ID)
                    if reelthree.reelMove == 1 and reelthree.stopTime % 10:
                        fruit = Fruit(reelgroupthree, 3, random.randint(1,4))
                        del self.fruitlist[2][0]
                        self.fruitlist[2].append(fruit.ID)
                    else:
                        self.counter += 1
                    if reelone.reelmove == 1:
                        reelone.update()
                    if reeltwo.reelmove == 1:
                        reeltwo.update()
                    if reelthree.reelmove == 1:
                        reelthree.update()
    
                    # If all the reels are moving, check if the player has won and add the credits to their
                    # account if so.
                    if reelone.reelmove == reeltwo.reelmove == reelthree.reelmove == 0 and self.finished == 0:
                        if self.fruitlist[0][2] == self.fruitlist[1][2] ==  self.fruitlist[2][2]:
                            self.message = "Winner, want to play again?"
                            if self.fruitlist[0][2] == 1:
                                self.credits += 10
                            if self.fruitlist[0][2] == 1:
                                self.credits += 100
                            if self.fruitlist[0][2] == 3:
                                self.credits += 1000
                            if self.fruitlist[0][2] == 4:
                                self.credits += 10000
                            # Now this sets the state to finished
                            self.finished = 1
                        else:
                            self.message = "Sorry, no win this time!"
                            self.end = 1
    
                reelgroupone.draw(screen)
                reelgrouptwo.draw(screen)
                reelgroupthree.draw(screen)
    
        FPSClock.tick(FPS)
        pygame.display.update()
    
    fruit = main()
    


Answer: You're not actually running the main() as a function in your code, main is a
class instead. Change main to a function, and instead, write this at the
bottom of the program:

    
    
    If __name__ == "__main__": 
        main()
    

This way, the code actually runs. Also, get rid of `fruit = main()`. It just
makes it more confusing and you're going to be running it anyways with this,
hence why it says that the process finished. You may also want to move things
previously in the main class in the main function. For example, take functions
inside of the class such as the machine() function and put it as a separate
function outside of your code, and call it in the main function with
machine().

