Hello, my task is to create a basic game using Pygame. I decided to do the basic snake game like the one that was on all of the old mobile phones. However when inputing some of the basic code, i realised that once i have paused my game i have no idea how to get it to unpause. Also i have to input an option to restart the game. I have looked on the Pygame website to see if there was any special code for restarting but i can't see anything.

How can i get my game to unpause and how can i restart my game?

# -*- coding: cp1252 -*-
import pygame , pygame .mixer , sys
from pygame.locals import *

ballimg = 'c:/Python/images/segment.png'
caption = 'Snake Game using PyGame .'
screen_size = width , height = 800 , 600
black = 0, 0, 0
speed = [0 , 1]
pygame . init () 
screen = pygame . display . set_mode ( screen_size )
pygame . display . set_caption ( caption )
ball = pygame . image . load ( ballimg ). convert ()
ballrect = ball . get_rect ()
finished = False
paused = False

while not finished :

    for event in pygame . event . get ():
        
        if event . type == KEYDOWN:
            
            if event.key == K_DOWN:
                speed = [0 , 1]
            elif event.key == K_UP:
                speed = [0 , -1]
            elif event.key == K_LEFT:
                speed = [-1 , 0]
            elif event.key == K_RIGHT:
                speed = [1 , 0]
            elif event.key == K_ESCAPE:
                finished = True
            elif event.key == K_PAUSE:
                paused = True
            

                
            

        if event . type == pygame . QUIT :
            sys . exit ()
                
    pygame.time.delay (10)


    if paused == False:

        if ballrect .left <0 or ballrect .right > width :
            speed [0] = -speed [0]
        if ballrect .top <0 or ballrect . bottom > height :
            speed [1] = -speed [1]

        ballrect2 = ballrect
        
        ballrect = ballrect . move ( speed )
        screen . fill ( black )
        screen . blit (ball , ballrect )
        screen . blit (ball , ballrect2 )
        pygame . display . flip ()   

print "Finished"

Somewhere around line 34, just add a block that listens for K_UNPAUSE.

K_UNPAUSE might be K_PAUSE, I suppose, if you want it to toggle in which case just change line 35: paused = not paused

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.