Search code examples
pythonpygamewindowpylance

Why will my code not open a window in pygame?


It's supposed to just open the window so far, but during testing, it wouldn't open the window, although no errors are displayed.

I've already tried using "pygame.disply.flip()" instead of update but that changed nothing. I get this in the terminal too: pygame 2.1.2 (SDL 2.0.18, Python 3.10.1) Hello from the pygame community. https://www.pygame.org/contribute.html and that's it.

import pygame
import random

pygame.init()

class DrawInformation:
    BLACK = 0, 0, 0
    WHITE = 255, 255, 255
    GREEN = 0, 255, 0 
    RED = 255, 0, 0
    GREY = 128, 128, 128
    BACKGROUND_COLOR = WHITE

    SIDE_PAD = 100
    TOP_PAD = 150

    def __init__(self, width, height, lst):
        self.width = width
        self.height = height
        
        self.window = pygame.display.set_mode((width, height))
        pygame.display.set_caption("Sorting Algorithm Visualization")
        self.set_list(lst)

    def set_list(self, lst):
        self.list = lst
        self.min_val = min(lst)
        self.max_val = max(lst)

        self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
        self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
        self.start_x = self.SIDE_PAD // 2

def generate_starting_list(n, min_val, max_val):
    lst = []

    for _ in range(n):
        val = random.randint(min_val, max_val)
        lst.append(val)
    
    return lst

def main():
    run = True
    clock = pygame.time.Clock()

    n = 50
    min_val = 0
    max_val = 100


    lst = generate_starting_list(n, min_val, max_val)
    draw_info = DrawInformation(800, 600, lst)

    while run:
        clock.tick(60)
        
        pygame.display.update()

        for event in pygame.event.get():
            if event == pygame.QUIT:
                run = False
    
    pygame.quit()

    if __name__ == "__main__":
        main()

This is what I have so far ... it's also saying that "draw_info" is not accessed by pylance and idk why??


Solution

  • Line

    if __name__ == "__main__":
        main()
    

    is intended so is inside the def main() so it is not running when the program starts.

    here's the corrected code.

    import random
    
    pygame.init()
    
    class DrawInformation:
        BLACK = 0, 0, 0
        WHITE = 255, 255, 255
        GREEN = 0, 255, 0 
        RED = 255, 0, 0
        GREY = 128, 128, 128
        BACKGROUND_COLOR = WHITE
    
        SIDE_PAD = 100
        TOP_PAD = 150
    
        def __init__(self, width, height, lst):
            self.width = width
            self.height = height
            
            self.window = pygame.display.set_mode((width, height))
            pygame.display.set_caption("Sorting Algorithm Visualization")
            self.set_list(lst)
    
        def set_list(self, lst):
            self.list = lst
            self.min_val = min(lst)
            self.max_val = max(lst)
    
            self.block_width = round((self.width - self.SIDE_PAD) / len(lst))
            self.block_height = round((self.height - self.TOP_PAD) / (self.max_val - self.min_val))
            self.start_x = self.SIDE_PAD // 2
    
    def generate_starting_list(n, min_val, max_val):
        lst = []
    
        for _ in range(n):
            val = random.randint(min_val, max_val)
            lst.append(val)
        
        return lst
    
    def main():
        run = True
        clock = pygame.time.Clock()
    
        n = 50
        min_val = 0
        max_val = 100
    
    
        lst = generate_starting_list(n, min_val, max_val)
        draw_info = DrawInformation(800, 600, lst)
    
        while run:
            clock.tick(60)
            
            pygame.display.update()
    
            for event in pygame.event.get():
                if event == pygame.QUIT:
                    run = False
        
        pygame.quit()
    
    if __name__ == "__main__":
        main()