Python: pygame game programming tour 6 (audio processing in the game)

Source: Internet
Author: User
Tags rewind

There is no sound in a user-friendly game. For example, when a role fails, a scream, or a match of weapons during a PK, there is background music when a hero is playing, and there is no sound involved, this section describes how to control sound in pygame. The following is an example, but the blog cannot upload multimedia programs, otherwise, you will be able to hear the most classic music he's a pirate among the Pirates of the Caribbean. The program implements the function of controlling the volume by using the up and down arrow keys.
I. instance interface:
1. The initial volume is 10.
 
2. Adjust the music sound size in real time using the up and down arrow keys:

II. Implementation Code:
 

[Python]
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
 
Import sys
Import OS
Import pygame
From pygame. locals import *
 
Def load_image (pic_name ):
'''''
Function: Image Loading Function
Input: pic_name image name
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the image directory
Path = OS. path. join (current_dir, 'image', pic_name)

# Loading Images
Return pygame. image. load (path). convert ()
 
Def load_sound (soundfile_name ):
'''''
Function: Background Music Loading Function
Input: pic_name music file name
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-22
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the audio directory
Path = OS. path. join (current_dir, 'sound', soundfile_name)

Return path
 
Def init_windows ():
'''''
Function: Window Initialization
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-21
'''
Pygame. init ()
Display_surface = pygame. display. set_mode (382,407 ))
Pygame. display. set_caption ('music processing in the game
Return display_surface
 
Def exit_windows ():
'''''
Function: Exit Processing
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-21
'''
Pygame. quit ()
Sys. exit ()
 
Def main ():
'''''
Function: voice processing
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-22
'''

Screen_surface = init_windows ()
Back_image = load_image('lession6_back.jpg ')
Back_music_file = load_sound('he_is_a_pirate.mp3 ')

Color_red = (255, 0, 0)
Color_green = (0,255, 0)
Color_blue = (0, 0,255)
 
Music_volume = 10

# Text
FontObj = pygame. font. Font ('simkai. ttf', 20)
Volume_text = U' current volume: % d' % music_volume
TextSurfaceObj = fontObj. render (volume_text, True, color_red)
TextRectObj = textSurfaceObj. get_rect ()

# Loading background music
Pygame. mixer. music. load (back_music_file)
Pygame. mixer. music. set_volume (music_volume/100.0)

# Loop playback, starting from 30th seconds
Pygame. mixer. music. play (-1, 30.0)

While True:
# Drawing
Screen_surface. B .( back_image, (0, 0 ))
Screen_surface.b133 (textSurfaceObj, textRectObj)
For event in pygame. event. get ():
If event. type = QUIT:

# Stop playing music
Pygame. mixer. music. stop ()
Exit_windows ()

If event. type = pygame. KEYDOWN:
# Using the up key to control the volume
If event. key = pygame. K_UP:
Music_volume + = 10
If (maid> 100 ):
Music_volume = 0
If event. key = pygame. K_DOWN:
Music_volume-= 10
If (music_volume <0 ):
Music_volume = 100

# Set the volume
Pygame. mixer. music. set_volume (music_volume/100.0)

# Display volume
Volume_text = U' current volume: % d' % music_volume
TextSurfaceObj = fontObj. render (volume_text, True, color_red)
TextRectObj = textSurfaceObj. get_rect ()

Pygame. display. update ()
 
If _ name _ = '_ main __':
Main ()
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

Import sys
Import OS
Import pygame
From pygame. locals import *

Def load_image (pic_name ):
'''
Function: Image Loading Function
Input: pic_name image name
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the image directory
Path = OS. path. join (current_dir, 'image', pic_name)

# Loading Images
Return pygame. image. load (path). convert ()

Def load_sound (soundfile_name ):
'''
Function: Background Music Loading Function
Input: pic_name music file name
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-22
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the audio directory www.2cto.com
Path = OS. path. join (current_dir, 'sound', soundfile_name)

Return path

Def init_windows ():
'''
Function: Window Initialization
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-21
'''
Pygame. init ()
Display_surface = pygame. display. set_mode (382,407 ))
Pygame. display. set_caption ('music processing in the game
Return display_surface

Def exit_windows ():
'''
Function: Exit Processing
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-21
'''
Pygame. quit ()
Sys. exit ()

Def main ():
'''
Function: voice processing
Input: NONE
Output: NONE
Author: dyx1024
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-22
'''

Screen_surface = init_windows ()
Back_image = load_image('lession6_back.jpg ')
Back_music_file = load_sound('he_is_a_pirate.mp3 ')

Color_red = (255, 0, 0)
Color_green = (0,255, 0)
Color_blue = (0, 0,255)

Music_volume = 10

# Text
FontObj = pygame. font. Font ('simkai. ttf', 20)
Volume_text = U' current volume: % d' % music_volume
TextSurfaceObj = fontObj. render (volume_text, True, color_red)
TextRectObj = textSurfaceObj. get_rect ()

# Loading background music
Pygame. mixer. music. load (back_music_file)
Pygame. mixer. music. set_volume (music_volume/100.0)

# Loop playback, starting from 30th seconds
Pygame. mixer. music. play (-1, 30.0)

While True:
# Drawing
Screen_surface. B .( back_image, (0, 0 ))
Screen_surface.b133 (textSurfaceObj, textRectObj)
For event in pygame. event. get ():
If event. type = QUIT:

# Stop playing music
Pygame. mixer. music. stop ()
Exit_windows ()

If event. type = pygame. KEYDOWN:
# Using the up key to control the volume
If event. key = pygame. K_UP:
Music_volume + = 10
If (maid> 100 ):
Music_volume = 0
If event. key = pygame. K_DOWN:
Music_volume-= 10
If (music_volume <0 ):
Music_volume = 100

# Set the volume
Pygame. mixer. music. set_volume (music_volume/100.0)

# Display volume
Volume_text = U' current volume: % d' % music_volume
TextSurfaceObj = fontObj. render (volume_text, True, color_red)
TextRectObj = textSurfaceObj. get_rect ()

Pygame. display. update ()

If _ name _ = '_ main __':
Main () 3. Key knowledge points:

Sound processing uses the pygame. mixer. music module, which provides a wide range of methods, as follows:


[Plain]
Pygame. mixer. music. load
Notes: loading music files
Prototype: pygame. mixer. music. load (filename): return None
 
Pygame. mixer. music. play
Views: plays music.
Prototype: pygame. mixer. music. play (loops = 0, start = 0.0): return None,
Here, loops indicates the number of loops. For example, if it is set to-1, it indicates that the loop is continuously played. For example, loops = 5,
The video is played 5 + 1 = 6 times. The start parameter indicates the second of the music file. If it is set to 0, the video is played completely from the start.
 
Pygame. mixer. music. rewind
Note: Replay
Prototype: pygame. mixer. music. rewind (): return None
 
Pygame. mixer. music. stop
Stops playback.
Prototype: pygame. mixer. music. stop (): return None
 
Pygame. mixer. music. pause
Suspend
Prototype pygame. mixer. music. pause (): return None
You can resume playback through pygame. mixer. music. unpause.
 
Pygame. mixer. music. unpause
Resume playback
Prototype: pygame. mixer. music. unpause (): return None
 
Pygame. mixer. music. fadeout
Pause the specified time and then play the video.
Prototype: pygame. mixer. music. fadeout (time): return None,
Unit: milliseconds
 
Pygame. mixer. music. set_volume
Sets the volume.
Prototype: pygame. mixer. music. set_volume (value): return None
Valid value: 0.0 ~ 1.0
 
Pygame. mixer. music. get_volume
Description: gets the volume.
Prototype: pygame. mixer. music. get_volume (): return value
 
Pygame. mixer. music. get_busy
Determines whether music is playing.
Prototype: pygame. mixer. music. get_busy (): return bool
 
Pygame. mixer. music. get_pos
Description: How long has the current playback been obtained?
Prototype: pygame. mixer. music. get_pos (): return time
 
Pygame. mixer. music. queue
Adds other music files to the playing queue. After the current music is played, other music files in the queue are automatically played.
 
Pygame. mixer. music. set_endevent
Notification of events after playback is complete
Prototype: pygame. mixer. music. set_endevent (): return None
Pygame. mixer. music. set_endevent (type): return None

Pygame. mixer. music. get_endevent
Description: gets the event after playback. If no, pygame. NOEVENT is returned.
Prototype: pygame. mixer. music. get_endevent (): return type

 

 

From Socrates Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.