i am building a pong like game and cant seem to figure out how to add in a pause feature everything else works i have movement and collision detection, sound, game over and restart options but cant figure out how to pause the game any help would be greatly appreciated this is the entire code for the game i am new to python so im sure this is probably not the best way to do it.

import pygame,sys,random
from pygame.locals import *

pause=0
      

pygame.init()

screen=pygame.display.set_mode((1024,768),0,32)
pygame.mouse.set_visible(False)
background=pygame.image.load('window.jpg').convert()
ball=pygame.image.load('ball.png').convert_alpha()
stopper1=pygame.image.load('bar1.png').convert_alpha()
stopper2=pygame.image.load('bar2.png').convert_alpha()

sound=pygame.mixer.Sound('Laser.ogg')

x=random.uniform(0.0,1000.0)
y=random.uniform(0.0,728.0)
x2=1010
y2=300

speeds=600
speedx=200
speedy=200
clock=pygame.time.Clock()
                                
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            sys.exit()
            pygame.quit()
            
        if event.type==KEYUP:
            if event.key==K_ESCAPE:
                pygame.display.toggle_fullscreen()
                
        if event.type==KEYUP:
            if event.key==K_p:
                pause=True
            elif event.key==K_s:
                pause==False
                
        if pause==True:
            pygame.time.delay(3000)
        if pause==False:
            pygame.time.delay(0)
            continue                  
            
                                                                                   
    screen.blit(background,(0,0))
    screen.blit(ball,(x,y))
    screen.blit(stopper1,(0,0))
    screen.blit(stopper2,(x2,y2))
    milli=clock.tick()
    seconds=milli/1000.0
    dmx=seconds*speedx
    dmy=seconds*speedy
    dms=seconds*speeds
    
    x-=dmx
    y-=dmy

    if pygame.Rect(0,0,15,768).colliderect(x,y,50,50)==True:
        speedx=-speedx
        sound.play()
          
    if y<=0:
        speedy=-speedy
        sound.play()
        
    if y>=728:
        speedy=-speedy
        sound.play()
        
    if pygame.Rect(x,y,50,50).colliderect(x2,y2,15,50)==True:
        speedx=-speedx*1.1
        sound.play()
        
    if x>=1024:
        break
        
    if event.type==KEYDOWN:
        if event.key==K_UP and y2>15:
            y2-=dms
           
    if event.type==KEYDOWN:
        if event.key==K_DOWN and y2<704:
            y2+=dms  
                          
    pygame.display.update()
    
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            sys.exit()
            pygame.quit()
            
        if event.type==KEYUP:
            if event.key==K_ESCAPE:
                pygame.display.toggle_fullscreen()          
                            
    screen.blit(background, (0,0)) 
    font=pygame.font.SysFont('liberationserif',100)
    gameover=font.render('Game Over', True, (0,0,0), (255,255,255))
    screen.blit(gameover,(300,100))
    restart=font.render('Restart? y/n', True, (0,0,0), (255,255,255))
    screen.blit(restart, (300,400))
    
    if event.type==KEYDOWN:
        if event.key==K_n:
            sys.exit()
            pygame.quit()
            
        elif event.type==KEYDOWN:
            if event.key==K_y:
                exec file('mypong.py')
              
    pygame.display.update()

Try this:

import pygame
import sys, os

#Pause Variable
pause = False

#Main loop
while 1:
    for event in pygame.event.get():
        if event.type==KEYUP:
            if event.key==K_p:
                pause = False

    while pause == False:
        for event in pygame.event.get():
            if event.type==KEYUP:
                if event.key==K_p:
                    pause = True
            #Do your game here

Hope it helps :)

Above post has some problem but the below one is working

pause=False
#your game code
for event in pygame.event.get():
    if event.type==KEYUP:
            if event.key==K_p:
                pause = True

while pause == True:
        for event in pygame.event.get():
            if event.type==KEYUP:
                if event.key==K_p:
                    pause = False

#your remaining code
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.