Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
game sounds module
#1
I have made, inspired by a thread here a while ago, a little "shove the numbers" game, numbers zero to fifteen, 4 by 4 grid, get the numbers in the right order. First time I tried this kind of GUI stuff.

Now I would like to add sounds. What is the best way to add sounds? I saw a module called winsound, but I use Linux. Will that work?

When I click a tile it moves to where the zero tile is. (The zero tile looks like an empty space.) I would like to hear a sshhh sound of the tile moving, also maybe some music playing in the background all the time and "Hurrah" when the puzzle is finished.

Can anyone please recommend a module and or method to produce sounds?
Reply
#2
Hi,

I would help if you could add info which GUI framework you are using - maybe the framework of your choice offers something? The most common things for games is pygame, which has classes and methods on-board for playing sounds. Plus it has already the abstraction layers build in dealing with sound systems of Linux, Win and MacOS.

Regards, noisefloor
rolandyegon likes this post
Reply
#3
Thanks for your reply!

Just using tkinter and tkinter messagebox at the moment. The game works fine, so now I want to add some bells and whistles, so to speak!

from random import shuffle # for a random start
import tkinter as tk
from tkinter import messagebox # for sending messages
Reply
#4
Hi,

this thread at SO (https://stackoverflow.com/questions/2879...-is-pushed) shows how to combine Tkinter with pygame. Pretty straight forward.

What of course always works is to play sound via a terminal-based player (on Linux e.g. mpeg123) and call it via subprocess. Just make sure to use non-blocking calls to prevent that your game freezes when playing a longer sound. However, this method has the same disadvantage like using e.g. winsound. It's not portable but depends on the platform or even your specific system and software installed. Whether this is a drawback or not depends on whether you want the whole thing to be portable or whether it is a private project which is OK to run just locally on your machine.

Regards, noisefloor
Pedroski55 likes this post
Reply
#5
Pyglet is fine for this,work on all Platforms.
Example.
import tkinter as tk
import pyglet

class App:
    def __init__(self, root):
        self.root = root
        self.root.title("Simple Sound Example")
        self.sound = pyglet.media.load("message.mp3", streaming=False)
        self.label = tk.Label(root, text="No message yet")
        self.label.pack(pady=20)
        self.button = tk.Button(root, text="Show message", command=self.show_message)
        self.button.pack(pady=10)

    def show_message(self):
        self.label.config(text="New message!")
        self.sound.play()

root = tk.Tk()
app = App(root)
root.mainloop() 
Pedroski55 and rolandyegon like this post
Reply
#6
Thanks very much!

I put this at the top of the class: sound_bite = "/home/peterr/Downloads/bell1.mp3"

and changed this: self.sound = pyglet.media.load(self.sound_bite, streaming=False)

snippsat's code works great, of course. Dingdong! Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with playing sounds jderickson 5 5,931 May-25-2023, 09:52 PM
Last Post: jderickson
  Detecting Windows System Sounds? crash2720 1 5,330 Jan-23-2018, 07:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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