简体   繁体   中英

pygames pygame.quit() not working on Mac M1?

Although I am fairly new to python, I have some experience with programming as a whole. I was watching a Tech With Tim tutorial on a python visualizer and everything was going well until I attempted to quit the window, for some reason. pygame,quit() does not work. even if I put sys.exit() or pygame.display.quit() after/before it, if someone has experienced this before. some help would be greatly appreciated as this is a big roadblock.

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 == True:
        clock.tick(60)

        pygame.display.update()

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

     pygame.display.quit()
     pygame.quit()


if __name__ == "__main__":
     main()

Your event check is incorrect because you're comparing an Event object to pygame.QUIT , which is an int , so the comparison always fails and you'd never be able to quit the game.

You need to compare event.type == pygame.QUIT like so:

while run == True:
    clock.tick(60)

    pygame.display.update()

    for event in pygame.event.get():
        # changed from 'event' to 'event.type'
        if event.type == pygame.QUIT:
            run = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM