Python Forum
[PyGame] sound effect delay and program laggy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] sound effect delay and program laggy
#3
Its a 240 line program. But okay, here's the code. Keep in mind that its a snake program, and relies on three sound files. No idea how you can run this without the sound files, but I guess you could just create your own replacements if you're looking to run this.

import random
import pygame
pygame.init()

screenWidth=500
screenHeight=500

mainWindow=pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("PySnake")

x=screenWidth/2
y=screenHeight/2
snakeSize=50
move="down"
length=0
path=[]
fps=7
fpsClock=pygame.time.Clock()
win=True
loseGameSound=pygame.mixer.Sound('pysnake_lose_game.wav')
eatSound=pygame.mixer.Sound('pysnake_eat.wav')
bleep=pygame.mixer.Sound('pysnake_bleep.wav')

font=pygame.font.SysFont('arialblack',20)



def frandxy():
    global posx
    global posy
    posx=snakeSize*(random.randint(0,screenWidth/snakeSize-1))
    posy=snakeSize*(random.randint(0,screenWidth/snakeSize-1))
    if (posx,posy) in path or (posx,posy)==(x,y):
            frandxy()

def closeGame():
    pygame.quit()
    quit()

def playAgain():
    global x
    global y
    global length
    global move
    global path
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            bleep.play()
            closeGame()
        if key[pygame.K_y]:
            bleep.play()
            x=screenWidth/2
            y=screenHeight/2
            length=0
            move="down"
            path=[]
            game()
        if key[pygame.K_n]:
            closeGame()
        if win==False:
            mainWindow.fill((0,0,0))
        if win==True:
            mainWindow.fill((0,255,0))
        pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\
                                               screenHeight/2-snakeSize,\
                                               snakeSize*6,snakeSize*2))
        font=pygame.font.SysFont('arialblack',20)
        textTop=font.render('Play Again?',0,(0,0,0))
        textBottom=font.render(('(y)es/(n)o'),0,(0,0,0))

        mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize))
        mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2))
        pygame.display.update()

def loseGame():
    loseGameSound.play()
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_RETURN]:
            bleep.play()
            playAgain()
        
        mainWindow.fill((0,0,0))
        pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\
                                               screenHeight/2-snakeSize,\
                                               snakeSize*6,snakeSize*2))
        font=pygame.font.SysFont('arialblack',20)
        textTop=font.render('GAME OVER',0,(0,0,0))
        textBottom=font.render(('Your Length Was: '+str(length)),0,(0,0,0))

        mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize))
        mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2))
        pygame.display.update()

def winGame():
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()
        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_RETURN]:
            bleep.play()
            playAgain()
        
        mainWindow.fill((0,255,0))
        textTop=font.render('Your Snake Filled the Screen!',0,(0,0,0))
        textBottom=font.render(('Congratulations!'),0,(0,0,0))

        mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize))
        mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2))
        pygame.display.update()
        
def game():
    global x
    global y
    global move
    global length
    global path
    global fpsClock
    global win
    while True:
        
        
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_UP] and move != "down":
            move="up"
        if key[pygame.K_w] and move !="down":
            move="up"
        if key[pygame.K_DOWN] and move != "up":
            move="down"
        if key[pygame.K_s] and move != "up":
            move="down"
        if key[pygame.K_RIGHT] and move != "left":
            move="right"
        if key[pygame.K_d] and move != "left":
            move="right"
        if key[pygame.K_LEFT] and move != "right":
            move="left"
        if key[pygame.K_a] and move != "right":
            move="left"
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_e]:
            closeGame()

        path.insert(0,(x,y))
    #move snake
        if move=="down" and y<=screenHeight-snakeSize:
            y+=snakeSize
        if move=="down" and y>=screenHeight:
            y=0
        if move=="up" and y>=0:
            y-=snakeSize
        if move=="up" and y<0:
            y=screenHeight-snakeSize
        if move=="right" and x<=screenWidth-snakeSize:
            x+=snakeSize
        if move=="right" and x>=screenWidth:
            x=0
        if move=="left" and x>=0:
            x-=snakeSize
        if move=="left" and x<0:
            x=screenWidth-snakeSize

        if x==posx and y==posy:
            length+=1
            eatSound.play()
            frandxy()
        if (x,y) in path:
            win=False
            loseGame()
        if length>=(screenWidth/snakeSize)*(screenHeight/snakeSize):
            winGame()

        mainWindow.fill((0,0,0))
        #draw snake
        if length>0:
            for i in range(length):
                pygame.draw.rect(mainWindow, (0,255,0),(path[i][0],path[i][1], snakeSize,snakeSize))
        pygame.draw.rect(mainWindow, (0,255,0),(x, y, snakeSize, snakeSize))
        #draw food
        pygame.draw.rect(mainWindow, (255,0,0),(posx,posy, snakeSize, snakeSize))
        if len(path)>length:
            path=path[:(length)]
        pygame.display.update()
        fpsClock.tick(fps)

def start():

    while True:

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_e]:
            closeGame()
    
        titleFont=pygame.font.SysFont('arialblack',50)
        title=titleFont.render('PySnake',0,(0,255,0))
        enterPrompt=font.render('Press Enter to Start',0,(0,255,0))

        mainWindow.blit(title,(screenWidth/2-title.get_width()/2,screenHeight/2-snakeSize*2))
        mainWindow.blit(enterPrompt,(screenWidth/2-enterPrompt.get_width()/2,screenHeight/2))

        for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_RETURN]:
            bleep.play()
            game()
        if key[pygame.K_ESCAPE]:
            closeGame()

        pygame.display.update()

frandxy()
start()


Messages In This Thread
RE: Why do my sound effects always have a slight delay in them? - by xBlackHeartx - Sep-26-2019, 10:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Laggy Game Movement game_slayer_99 12 4,550 Oct-05-2022, 11:34 AM
Last Post: metulburr
Music [PyGame] Chopper wash effect efficiency questions. michael1789 9 4,530 Jan-19-2021, 07:12 PM
Last Post: michael1789
  Appropriately delay this PyGame code kleynah22 2 4,425 Nov-09-2017, 02:00 PM
Last Post: Windspar
  [PyGame] My program is very laggy GamePlanet 6 7,128 Aug-15-2017, 03:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020