Ive created a snake game with the help of a tutorial and i want to create a pause function for my program however, every other tutorial and website i looked at hasn't really helped, can someone help me figure out how to pause my game?

the code is as follows, sorry btw forgot to post code

    import pygame, sys, random
    from pygame.math import Vector2

    cell_size = 40
    cell_number = 20

    class SNAKE:
        def __init__(self):
            self.body = {Vector2(5,10),Vector2(4,10),Vector2(3,10)}
            self.direction = Vector2(1,0)
            self.new_block = False


        def draw_snake(self):
            for block in self.body:
                x_pos = int(block.x * cell_size)
                y_pos = int(block.y * cell_size)
                block_rect = pygame.Rect(x_pos,y_pos,cell_size,cell_size)
                pygame.draw.rect(screen,(183,111,122),block_rect)

        def move_snake(self):
            if self.new_block == True:
                body_copy = self.body{:}
                body_copy.insert(0,body_copy{0} + self.direction)
                self.body = body_copy{:}
                self.new_block = False
            else:
                body_copy = self.body{:-1}
                body_copy.insert(0,body_copy{0} + self.direction)
                self.body = body_copy{:}

        def add_block(self):
            self.new_block = True

    class FRUIT:
        def __init__(self):
            self.randomize()

        def draw_fruit(self):
            fruit_rect = pygame.Rect(int(self.pos.x*cell_size),int(self.pos.y*cell_size),cell_size, cell_size)
            pygame.draw.rect(screen,(126,166,114),fruit_rect)

        def randomize(self):
            self.x = random.randint(0,cell_number - 1)
            self.y = random.randint(0,cell_number - 1)
            self.pos = Vector2(self.x,self.y)

    class MAIN:
        def __init__(self):
            self.snake = SNAKE()
            self.fruit = FRUIT()

        def update(self):
            self.snake.move_snake()
            self.check_collision()
            self.check_fail()

        def draw_elements(self):
            self.fruit.draw_fruit()
            self.snake.draw_snake()
            self.draw_grass()
            self.draw_score()

        def check_collision(self):
            if self.fruit.pos == self.snake.body{0}:
                self.fruit.randomize()
                self.snake.add_block()

            for block in self.snake.body{1:}:
                if block == self.fruit.pos:
                    self.fruit.randomize()

        def check_fail(self):
            if not 0 <= self.snake.body{0}.x < cell_number or not 0 <= self.snake.body{0}.y < cell_number:
                self.game_over()

            for block in self.snake.body{1:}:
                if block == self.snake.body{0}:
                    self.game_over()

        def draw_grass(self):
            grass_color =(167,209,61)

            for row in range(cell_number):
                if row % 2 ==0:
                    for col in range(cell_number):
                        if col % 2 == 0:
                            grass_rect = pygame.Rect(col * cell_size,row * cell_size,cell_size,cell_size)
                            pygame.draw.rect(screen,grass_color,grass_rect)
                else:
                    for col in range(cell_number):
                        if col % 2 != 0:
                            grass_rect = pygame.Rect(col * cell_size,row * cell_size,cell_size,cell_size)
                            pygame.draw.rect(screen,grass_color,grass_rect)

        def draw_score(self):
            score_text = str(len(self.snake.body) - 3)
            score_surface = game_font.render(score_text,True,(56,74,12))
            score_x = int(cell_size * cell_number - 60)
            score_y = int(cell_size * cell_number - 40)
            score_rect = score_surface.get_rect(center = (score_x,score_y))

            screen.blit(score_surface,score_rect)

        def game_over(self):
            pygame.quit()
            sys.exit()

    pygame.mixer.pre_init(44100,-16,2,512)
    pygame.init()
    screen = pygame.display.set_mode((cell_size * cell_number,cell_size * cell_number))
    clock = pygame.time.Clock()

    SCREEN_UPDATE = pygame.USEREVENT
    pygame.time.set_timer(SCREEN_UPDATE,150)
    game_font = pygame.font.Font(None, 25)
    paused = False

    main_game = MAIN()

    while True:
        if paused == True:
            continue
        for event in pygame.event.get():
            screen.fill((175,215,70))
            main_game.draw_elements()
            pygame.display.update()
            clock.tick(60)
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_p:
                    paused = True
                elif event.key == pygame.K_c:
                    paused = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if main_game.snake.direction.x != 1:
                        main_game.snake.direction = Vector2(-1,0)
                if event.key == pygame.K_RIGHT:
                    if main_game.snake.direction.x != -1:
                        main_game.snake.direction = Vector2(1,0)
                if event.key == pygame.K_DOWN:
                    if main_game.snake.direction.y != -1:
                        main_game.snake.direction = Vector2(0,1)
                if event.key == pygame.K_UP:
                    if main_game.snake.direction.y != 1:
                        main_game.snake.direction = Vector2(0,-1)

            if event.type == SCREEN_UPDATE:
                main_game.update()

            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

I'm not familiar with the pygame module but I do have a question and an unrelated suggestion. I see that you are using dictionaries where it appears you should be using lists. Unless there is something I am missing I suggest you replace all of your curly brackets with square brackets or you will get nothing but syntax errors.

As for the pause, I suggest you replace pause/continue keys with just a pause toggle on 'p'. Your problem appears to be that when you hit pause you stop processing events. Try the following to fix.

while True:
    for event in pygame.event.get():
        screen.fill((175,215,70))
        main_game.draw_elements()
        pygame.display.update()
        clock.tick(60)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_p:
                paused = not paused

        if paused:
            continue

Also, when you have multiple exclusive contions you should be using elif as in

if condition-1:
    do stuff
elif condition-2:
    do stuff:
elif condition-3:
    do stuff
commented: ahh ok many thanks, just tried this and it works, as for the curly brackets im new to this website so i changed all square brackets to the others +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.