2019-07-28 02:56:05 (edited by Quin 2019-07-28 02:56:43)

Hi all
So i am working on a game in python, and i would like it to have a menu, with music and click and enter sounds. The only problem is, i don't know how to go about this. any help would be apprishiated.

2019-07-28 04:29:06 (edited by magurp244 2019-07-28 04:33:49)

I tend to use classes for encapsulated menus, easier to drop them in and out. Example in pygame:

import pygame
from pygame import mixer
import sys

class main(object):
    def __init__(self):
    #initialize pygame
        pygame.init()
    #initialize sound mixer
        mixer.init()
    #create display
        self.window = pygame.display.set_mode([640,480])
    #current menu state
        self.state = 'title'
    #list of menu/game classes
        self.menus = {'title':titlescreen(), 'game':game()}

    #primary update loop
        while True:
        #check for key press events
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    key = event
                else:
                    key = None

        #update and pass key presses and menu state to active menu
            self.state = self.menus[self.state].update(key,self.state)

        #update window
            pygame.display.update()


#titlescreen menu class
class titlescreen(object):
    def __init__(self):
    #load sound
        self.click = mixer.Sound('sound.wav')
    #load sound
        self.enter = mixer.Sound('enter.wav')
    #load music
        self.music = mixer.Sound('music.wav')
    #menu option
        self.option = 0

    #select channel's for better playback control
        self.channel0 = pygame.mixer.Channel(0)
        self.channel1 = pygame.mixer.Channel(1)
    #play background music
        self.channel0.queue(self.music)

    def update(self,event,state):
    #if titlescreen is active and not playing, play
        if self.channel0.get_busy == False:
            self.channel0.play(self.music)
    #queue the music to keep it playing
        if self.channel0.get_queue() == None:
            self.channel0.queue(self.music)

    #if a key has been pressed
        if event != None:
        #move up in the menu and play tone
            if event.key == pygame.K_UP:
                if self.option > 0:
                    self.option -= 1
                    self.channel1.play(self.click)

        #move down in the menu and play tone
            elif event.key == pygame.K_DOWN:
                if self.option < 1:
                    self.option += 1
                    self.channel1.play(self.click)

        #select a menu option
            elif event.key == pygame.K_RETURN:
            #if option is 0 return game state and play tone
                if self.option == 0:
                    self.channel1.play(self.enter)
                    return 'game'
            #quit game
                elif self.option == 1:
                    self.channel1.play(self.enter)
                    pygame.quit()
                    sys.exit(0)

    #return current state if no changes
        return state


#main game class
class game(object):
    def __init__(self):
        pass

#DEMO: return to titlescreen
    def update(self,key,state):
        return 'title'


a = main()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer