ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:pygame:step010

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
en:pygame:step010 [2014/01/09 11:07]
127.0.0.1 external edit
en:pygame:step010 [2020/05/15 22:44] (current)
Horst JENS
Line 91: Line 91:
 View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​010_sound_only_no_graphic.py View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​010_sound_only_no_graphic.py
  
-//click reload in your browser if you see no code here:// +<code python>​ 
-<​html>​ +#!/usr/bin/env python 
- <script src="​http://​gist-it.appspot.com/github/horstjens/ThePythonGameBook/​blob/​master/pygame/010_sound_only_no_graphic.py"> +# -*- coding: utf-8 -*- 
- </script></​html>+ 
 +"""​ 
 +Name:    010_sound_only_no_graphic.py 
 +Purpose: demonstrate use of pygame for playing sound & music 
 +URL:     http://ThePythonGameBook.com  
 +Author: ​ Horst.Jens@spielend-programmieren.at 
 +Licence: gpl, see http://www.gnu.org/licenses/gpl.html 
 + 
 +works with pyhton3.4 and python2.7 
 +"""​ 
 + 
 +#the next line is only needed for python2.x and not necessary for python3.x 
 +from __future__ import print_function,​ division 
 + 
 +import ​pygame 
 +import os 
 +import sys 
 + 
 +# if using python2, the get_input command needs to act like raw_input:​ 
 +if sys.version_info[:​2] <= (2, 7): 
 +    get_input = raw_input 
 +else: 
 +    get_input = input # python3 
 +         
 + 
 +pygame.mixer.pre_init(44100,​ -16, 2, 2048) # setup mixer to avoid sound lag 
 +pygame.init() ​                             #initialize pygame 
 + 
 +# look for sound & music files in subfolder '​data'​ 
 +pygame.mixer.music.load(os.path.join('​data',​ '​an-turr.ogg'​))#​load music 
 +jump = pygame.mixer.Sound(os.path.join('​data','​jump.wav'​)) ​ #load sound 
 +fail = pygame.mixer.Sound(os.path.join('​data','​fail.wav'​)) ​ #load sound 
 + 
 +# play music non-stop 
 +pygame.mixer.music.play(-1) ​                           
 + 
 +# game loop 
 +gameloop = True 
 + 
 +while gameloop: 
 +    # indicate if music is playing 
 +    if pygame.mixer.music.get_busy():​ 
 +        print("​ ... music is playing"​) 
 +    else:  
 +        print("​ ... music is not playing"​) 
 +    # print menu  
 +    print("​please press key:"​) 
 +    print("​[a] to play '​jump.wav'​ sound"​) 
 +    print("​[b] to play '​fail.wav'​ sound"​) 
 +    print("​[m] to toggle music on/off"​) 
 +    print("​[q] to quit"​) 
 +    answer = get_input("​press key [a] or [b] or [m] or [q], followed by [ENTER]"​) 
 +    answer = answer.lower() # force lower case 
 +    if "a" in answer: 
 +        jump.play() 
 +        print("​playing jump.wav once"​) 
 +    elif "​b"​ in answer: 
 +        fail.play() 
 +        print("​playing fail.wav once"​) 
 +    elif "​m"​ in answer: 
 +        if pygame.mixer.music.get_busy():​ 
 +            pygame.mixer.music.stop() 
 +        else: 
 +            pygame.mixer.music.play() 
 +    elif "​q"​ in answer: 
 +        #break from gameloop 
 +        gameloop = False 
 +    else: 
 +        print("​please press either [a], [b], [m] or [q] and [ENTER]"​) 
 + 
 + 
 +print("​bye-bye"​) 
 +pygame.quit() # clean exit  
 + 
 +</code>
  
 === with graphic === === with graphic ===
Line 100: Line 174:
 View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​010_sound_and_music.py View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​010_sound_and_music.py
  
-//click reload in your browser if you see no code here:// +<code python>​ 
-<​html>​ +#!/usr/bin/env python 
- <script src="​http://​gist-it.appspot.com/github/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​010_sound_and_music.py">​ +# -*- coding: utf-8 -*- 
- </script><​/html>+"""​ 
 +010_sound_and_music.py 
 +plays music and sound effects 
 +url: http://thepythongamebook.com/en:part2:pygame:step010 
 +author: horst.jens@spielend-programmieren.at 
 +licence: gpl, see http://www.gnu.org/​licenses/​gpl.html
  
 +This program plays music and 
 +plays a sound effect whenever the a of b  key is pressed and released
 +All files must be in a '​data'​ subfolder.
 +The '​data'​ subfolder must be in the same folder as the program.
  
-^ [[:en:pygame:step009| ← previous]] ^ [[en:​pygame:​start| ↑ Overview]] ^ [[:​en:​pygame:​step011| → next ]] ^+works with python3.4 and python2.7 
 +"""​ 
 +import ​pygame 
 +import os
  
-====== ​Comment ​this Page ====== + 
-~~DISQUS~~+pygame.mixer.pre_init(44100,​ -16, 2, 2048) # setup mixer to avoid sound lag 
 +pygame.init() ​                     #initialize pygame 
 + 
 +try: 
 +    pygame.mixer.music.load(os.path.join('​data',​ '​an-turr.ogg'​))#​load music 
 +    jump pygame.mixer.Sound(os.path.join('​data','​jump.wav'​)) ​ #load sound 
 +    fail pygame.mixer.Sound(os.path.join('​data','​fail.wav'​)) ​ #load sound 
 +except: 
 +    raise(UserWarning,​ "could not load or play soundfiles in '​data'​ folder :​-("​) 
 + 
 +pygame.mixer.music.play(-1) ​                          # play music non-stop 
 + 
 +screen=pygame.display.set_mode((640,​480)) # set screensize of pygame window 
 +background ​pygame.Surface(screen.get_size()) ​ #create empty pygame surface 
 +background.fill((255,​255,​255)) ​    #fill the background white color (red,​green,​blue) 
 +background ​background.convert() ​ #convert Surface object to make blitting faster 
 +screen.blit(background,​ (0,​0)) ​    #draw the background on screen 
 +clock = pygame.time.Clock() ​       #create a pygame clock object 
 +mainloop = True                     
 +FPS = 30 # desired framerate in frames per second. try out other values ! 
 +while mainloop: 
 +    milliseconds ​clock.tick(FPS) # do not go faster than this framerate 
 +    for event in pygame.event.get():​ 
 +        if event.type ​== pygame.QUIT:​ 
 +            mainloop ​False # pygame window closed by user 
 +        elif event.type ​== pygame.KEYDOWN:​ 
 +            if event.key ​== pygame.K_ESCAPE:​ 
 +                mainloop = False # user pressed ESC 
 +            if event.key == pygame.K_a:​ 
 +                fail.play() ​                 # play sound effect 
 +            if event.key == pygame.K_b:​ 
 +                jump.play() ​                 # play sound effect 
 +    # print the framerate into the pygame window title 
 +    pygame.display.set_caption("​FPS:​ {:.2f} Press [a] or [b] to play sound effects"​.format(clock.get_fps())) 
 +    pygame.display.flip() ​         # flip the screen 
 + 
 +</​code>​ 
 + 
 + 
 +^ [[:​en:​pygame:​step009| ← previous]] ^ [[en:​pygame:​start| ↑ Overview]] ^ [[:​en:​pygame:​step011| → next ]] ^
  
  
  
/var/www/horst/thepythongamebook.com/data/pages/en/pygame/step010.txt · Last modified: 2020/05/15 22:44 by Horst JENS