How would I add a background image and sound to this pygame program?

179 views Asked by At

Hello I would need help I would need to place a background image and some sound. possibly also sound effects after clicking on the button, I have taken over the menu, because I didn't know how to do it, I wanted to do it via the buttons, but somehow it didn't work. And to advise the game, I have somehow done it in another file, how to get to it using the play button. (I'm a complete beginner in python) Thanks for answer

import pygame
import pygame_menu

pygame.init()
background = pygame.image.load('qHyYvd.jpeg')

surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Trade Ships")
icon = pygame.image.load('2022-03-15_15.28.55.png')
pygame.display.set_icon(icon)

def set_difficulty(value, difficulty):
    pass

def start_the_game():
    pass

menu = pygame_menu.Menu('Welcome', 400, 300,
                        theme=pygame_menu.themes.THEME_BLUE)

menu.add.text_input('Name :', default='Save1')
menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty)
menu.add.button('Play', start_the_game)
menu.add.button('Quit', pygame_menu.events.EXIT)

menu.mainloop(surface)
1

There are 1 answers

0
SamuelLouf On

Full Code

import pygame
import pygame_menu

pygame.init()
pygame.mixer.init()

background = pygame_menu.BaseImage(
    image_path="qHyYvd.jpg"
)

surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Trade Ships")
icon = pygame.image.load('2022-03-15_15.28.55.png')
pygame.display.set_icon(icon)

def set_difficulty(value, difficulty):
    pass

def start_the_game():
    pygame.mixer.Sound("click.wav").play(0, 0, 0)

def main_background() -> None:
    """
    Background color/image of the main menu. In this function, the user can plot images, play sounds, etc.
    """
    background.draw(surface)

menu = pygame_menu.Menu('Welcome', 400, 300,
                        theme=pygame_menu.themes.THEME_BLUE)

menu.add.text_input('Name :', default='Save1')
menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty)
menu.add.button('Play', start_the_game)
menu.add.button('Quit', pygame_menu.events.EXIT)

menu.mainloop(surface, main_background)

Explanations

For the background

First, you need to load the image into a variable but using the pygame_menu.BaseImage() function.

background = pygame_menu.BaseImage(
    image_path="qHyYvd.jpg"
)

Then, create a function that draws the background into the page.

def main_background() -> None:
    """
    Background color/image of the main menu. In this function, the user can plot images, play sounds, etc.
    """
    background.draw(surface)

Finally, add the main_background() function in the main loop.

menu.mainloop(surface, main_background)

Now, your image is used as a background.

For the button click sound

First, initialise pygame.mixer :

pygame.mixer.init()

Then in your button's function load the sound and play it :

pygame.mixer.Sound("click.wav").play(0, 0, 0)