I am trying to create a little game using Pygame and Python 3.4.
I have been trying to create a character sprite but, even with the large amount of online tutorials, I have not managed to make my code work properly. I know the window loads up fine by itself, but I don't know why I am getting an error.
Here is my code:
import pygame, sys, os
from pygame.locals import *
class player(pygame.sprite.Sprite):
def __init__(self,img):
pygame.sprite.Sprite.__init__(self)
self.image = img
self.rect = self.image.get_rect()
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000,750))
pygame.display.set_caption('Hello world')
img = pygame.image.load("pixel art\RPGchar.png").convert_alpha()
while True: #game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
player.draw(DISPLAYSURF)
player.update()
pygame.display.flip()
Any help would make me most grateful.
tnx
The short answer is that Pygame is only officially supported up to Python 3.3.
You might be able to tweak things so that it works with version 3.4, but if this is one of your first projects, consider removing python 3.4, then installing Python 3.3 or Python 3.2.
Here is the link for Python 3.2.5: https://www.python.org/download/releases/3.2.5
Make SURE you use the 32-bit version!
Then, you can go here: https://bitbucket.org/pygame/pygame/downloads to download your pygame package.
Related
I'm trying to play sound files (.wav) with pygame but when I start it I never hear anything.
This is the code:
import pygame
pygame.init()
pygame.mixer.init()
sounda= pygame.mixer.Sound("desert_rustle.wav")
sounda.play()
I also tried using channels but the result is the same
For me (on Windows 7, Python 2.7, PyGame 1.9) I actually have to remove the pygame.init() call to make it work or if the pygame.init() stays to create at least a screen in pygame.
My example:
import time, sys
from pygame import mixer
# pygame.init()
mixer.init()
sound = mixer.Sound(sys.argv[1])
sound.play()
time.sleep(5)
sounda.play() returns an object which is necessary for playing the sound. With it you can also find out if the sound is still playing:
channela = sounda.play()
while channela.get_busy():
pygame.time.delay(100)
I had no sound from playing mixer.Sound, but it started to work after i created the window, this is a minimal example, just change your filename, run and press UP key to play:
WAVFILE = 'tom14.wav'
import pygame
from pygame import *
import sys
mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
pygame.init()
print pygame.mixer.get_init()
screen=pygame.display.set_mode((400,400),0,32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key==K_UP:
s = pygame.mixer.Sound(WAVFILE)
ch = s.play()
while ch.get_busy():
pygame.time.delay(100)
pygame.display.update()
What you need to do is something like this:
import pygame
import time
pygame.init()
pygame.mixer.init()
sounda= pygame.mixer.Sound("desert_rustle.wav")
sounda.play()
time.sleep (20)
The reason I told the program to sleep is because I wanted a way to keep it running without typing lots of code. I had the same problem and the sound didn't play because the program closed immediately after trying to play the music.
In case you want the program to actually do something just type all the necessary code but make sure it will last long enough for the sound to fully play.
import pygame, time
pygame.mixer.init()
pygame.init()
sounda= pygame.mixer.Sound("beep.wav")
sounda.play()
pygame.init() goes after mixer.init(). It worked for me.
I had the same problem under windows 7. In my case I wasn't running the code as Administrator. Don't ask me why, but opening a command line as administrator fixed it for me.
I think what you need is pygame.mixer.music:
import pygame.mixer
from time import sleep
pygame.mixer.init()
pygame.mixer.music.load(open("\windows\media\chimes.wav","rb"))
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
sleep(1)
print "done"
You missed to wait for the sound to finish. Your application will start playing the sound but will exit immediately.
If you want to play a single wav file, you have to initialize the module and create a pygame.mixer.Sound() object from the file. Invoke play() to start playing the file. Finally, you have to wait for the file to play.
Use get_length() to get the length of the sound in seconds and wait for the sound to finish:
(The argument to pygame.time.wait() is in milliseconds)
import pygame
pygame.mixer.init()
sounda = pygame.mixer.Sound('desert_rustle.wav')
sounda.play()
pygame.time.wait(int(sounda.get_length() * 1000))
Alternatively you can use pygame.mixer.get_busy to test if a sound is being mixed. Query the status of the mixer continuously in a loop.
In the loop, you need to delay the time by either pygame.time.delay or pygame.time.Clock.tick. In addition, you need to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
import pygame
pygame.init()
pygame.mixer.init()
sounda = pygame.mixer.Sound('desert_rustle.wav')
sounda.play()
while pygame.mixer.get_busy():
pygame.time.delay(10)
pygame.event.poll()
Your code plays desert_rustle.wav quite fine on my machine (Mac OSX 10.5, Python 2.6.4, pygame 1.9.1). What OS and Python and pygame releases are you using? Can you hear the .wav OK by other means (e.g. open on a Mac's terminal or start on a Windows console followed by the filename/path to the .wav file) to guarante the file is not damaged? It's hard to debug your specific problem (which is not with the code you give) without being able to reproduce it and without having all of these crucial details.
Just try to re-save your wav file to make sure its frequency info. Or you can record a sound to make sure its frequency,bits,size and channels.(I use this method to solve this problem)
I've had something like this happen. Maybe you have the same problem? Try using an absolute path:
import pygame
pygame.init()
pygame.mixer.init()
sounda= pygame.mixer.Sound("/absolute_path/desert_rustle.wav")
sounda.play()
Where abslute_path is obviously replaced with your actual absolute path ;)
good luck.
import pygame
pygame.init()
sound = pygame.mixer.Sound("desert_rustle.wav")
pygame.mixer.Sound.play(sound)
This will work on python 3
5 years late answer but I hope I can help someone.. :-)
Firstly, you dont need the "pygame.init()"-line.
Secondly, make a loop and play the sound inside that, or else pygame.mixer will start, and stop playing again immediately.
I got this code to work fine on my Raspberry pi with Raspbian OS.
Note that I used a while-loop that continues to loop the sound forver.
import pygame.mixer
pygame.mixer.init()
sounda = pygame.mixer.Sound("desert_rustle.wav")
while True:
sounda.play()
Just try:
import pygame.mixer
from time import sleep
pygame.mixer.init()
pygame.mixer.music.load(open("\windows\media\chimes.wav","rb"))
print ""
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
sleep(1)
print "done"
This should work. You just need to add print ""and the sound will have
had time to load its self.
Many of the posts are running all of this at toplevel, which is why the sound may seem to close. The final method will return while the sound is playing, which closes the program/terminal/process (depending on how called).
What you will eventually want is a probably a class that can call either single time playback or a looping function (both will be called, and will play over each other) for background music and single sound effects.
Here is pattern, that uses a different event loop / run context other than Pygame itself, (I am using tkinter root level object and its init method, you will have to look this up for your own use case) once you have either the Pygame.init() runtime or some other, you can call these methods from your own logic, unless you are exiting the entire runtime, each file playback (either single use or looping)
this code covers the init for ONLY mixer (you need to figure our your root context and where the individual calls should be made for playback, at least 1 level inside root context to be able to rely on the main event loop preventing premature exit of sound files, YOU SHOULD NOT NEED TIME.SLEEP() AT ALL (very anti-pattern here).... ALSO whatever context calls the looping forever bg_music, it will probably be some 'level' or 'scene' or similar in your game/app context, when passing from one 'scene' to the next you will probably want to immediately replace the bg_music with the file for next 'scene', and if you need the fine-grained control stopping the sound_effect objects that are set to play once (or N times)....
from pygame import mixer
bg_music = mixer.Channel(0)
sound_effects = mixer.Channel(1)
call either of these from WITHIN your inner logic loops
effect1 = mixer.Sound('Sound_Effects/'+visid+'.ogg')
sound_effects.play(effect1, 0)
sound1 = mixer.sound('path to ogg or wav file')
bg_music.play(sound1, -1) # play object on this channel, looping forever (-1)
do this:
import pygame
pygame.mixer.init()
pygame.mixer.music.load("desert_rustle.wav")
pygame.mixer.music.play(0)
I think that your problem is that the file is a WAV file.
It worked for me with an MP3. This will probably work on python 3.6.
I'm testing out an application and the UI uses PyQt4 with Pygame embedded into it. It uses a timer to "update" itself so to speak and in the timerEvent function Pygame attempts to retrieve all detected events. Issue is, Pygame isn't detecting any events.
Here's a minimalist version of my code
#!/etc/python2.7
from PyQt4 import QtGui
from PyQt4 import QtCore
import pygame
import sys
class ImageWidget(QtGui.QWidget):
def __init__(self,surface,parent=None):
super(ImageWidget,self).__init__(parent)
w=surface.get_width()
h=surface.get_height()
self.data=surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
self.surface = surface
self.timer = QtCore.QBasicTimer()
self.timer.start(500, self)
def timerEvent(self, event):
w=self.surface.get_width()
h=self.surface.get_height()
self.data=self.surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
self.update()
for ev in pygame.event.get():
if ev.type == pygame.MOUSEBUTTONDOWN:
print "Mouse down"
def paintEvent(self,event):
qp=QtGui.QPainter()
qp.begin(self)
qp.drawImage(0,0,self.image)
qp.end()
class MainWindow(QtGui.QMainWindow):
def __init__(self,surface,parent=None):
super(MainWindow,self).__init__(parent)
self.setCentralWidget(ImageWidget(surface))
pygame.init()
s=pygame.Surface((640,480))
s.fill((64,128,192,224))
pygame.draw.circle(s,(255,255,255,255),(100,100),50)
app=QtGui.QApplication(sys.argv)
w=MainWindow(s)
w.show()
app.exec_()
How can I get Pygame events while the Pygame window is embedded in a PyQt application?
Fist of all do not mix frameworks. The frameworks may interact poorly or completely conflict with one another.
Getting it to work on your system doesn't mean it will work on another system or with a different version of any of the frameworks.
Mixing frameworks always means some kind of undefined behavior.
In your example your create an image (pygame.Surface) with the Pygame library and display it in QWidget.
You never create a Pygame window. Therefore the Pygame event handling cannot work. You need to use Qts event handling.
Anyway, if you just want to do some image processing or draw some pictures and display them in a Qt application, I suggest using OpenCV (cv2). This library is designed for powerful image manipulation and the images can be viewed nicely using a Qt user interface.
You can not.
TL; DR;
You cannot and should not combine 2 libraries that have their own event loop, for example now the Qt eventloop is blocking the pygame event loop.
For example:
pygame.display.init() works because pygame.display has an init attribute
pygame.event.init() does NOT work because pygame.event does NOT have init attribute
Or is there a way to generate the list of modules when pygame.init() is run?
"You can always initialize individual modules manually, but pygame.init() is a convenient way to get everything started." (https://www.pygame.org/docs/ref/pygame.html)
There's a way through which you can check how many modules are initialized
import pygame
(item1,item2) = pygame.init()
print('Number of modules initialized :', item1)
#There are 6 in total
I do know 4 pygame modules which have a .init() function
pygame.display.init()
pygame.mixer.init()
pygame.font.init()
pygame.fastevent.init() #This is required for event response
import pygame
file = 'some.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
This outputs, "Process finished with exit code 0", but it doesn't play anything. How can I resolve this problem?
The play function starts the music playing, but returns immediately. Then your program reaches it's end, and the pygame object is automatically destroyed which causes the music to stop.
As you commented, it does play the music if you wait for it before exiting - because then the pygame object isn't destroyed until the while loop finishes.
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
The music stops because it's an asyncronous event, which means it'll keep going with the script. then, the script stops instantly, not giving the music a chance to start.
as stated before, you could use
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
however, even better is pygame.event.wait(), as it'll wait for all asynchronous events to end.
Here is a super easy way.
import pygame
file = 'some.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
pygame.event.wait()
I've found a good solution from thepythongamebook.com:
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load('music_01.mp3')
pygame.mixer.music.play(-1)
try this one.
import pygame
def pmusic(file):
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
print("Playing...")
clock.tick(1000)
def stopmusic():
pygame.mixer.music.stop()
def getmixerargs():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
return freq, size, chan
def initMixer():
BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
FREQ, SIZE, CHAN = getmixerargs()
pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
try:
initMixer()
file = 'C:\\data\\03.mp3'
pmusic(file)
except KeyboardInterrupt: # to stop playing, press "ctrl-c"
stopmusic()
print("\nPlay Stopped by user")
except Exception:
print("unknown error")
print("Done")
PyGame has 2 different modules for playing sound and music, the pygame.mixer module and the pygame.mixer.music module. This module contains classes for loading Sound objects and controlling playback. The difference is explained in the documentation:
The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.
If you want to play a mp3 file, you need to initialize the module. Load the file with pygame.mixer.music.load. Invoke pygame.mixer.music.play() to start playback of the music stream. Finally, you have to wait for the file to play.
Use pygame.mixer.music.get_busy() to test if a sound is being mixed. Query the status of the mixer continuously in a loop.
In the loop, you need to delay the time by either pygame.time.delay or pygame.time.Clock.tick. In addition, you need to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
import pygame
pygame.init()
pygame.mixer.music.load('my_music.mp3')
pygame.mixer.music.play()
clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
clock.tick(60)
pygame.event.poll()
It seems the audio does not play because of the way you have imported it. The code below plays the sound as expected. Nothing has changed here except that rather than import pygame it uses from pygame import mixer. This may be due to the fact Pygame is a package but I'm not sure.
from pygame import mixer
file = 'some.mp3'
mixer.init()
mixer.music.load(file)
mixer.music.play()
Why is pygame working for me, without having called pygame.init() at the start of the code? I ask just out of curiosity, because it works also without pygame.init().
EDIT-example
I have the following:
import pygame
#pygame.init()
TV=pygame.display.set_mode((500,500))
runs=1
while runs:
for e in pygame.event.get():
if e.type==pygame.QUIT:
runs=0
If i close the screen (using the close button), i test this also with the pygame.init() line uncommented and in both cases i get no errors.
I run it also using pygame.quit() instaed of runs=0 line (with once pygame.init() line commented and once uncommented), i get in BOTH cases this:
for e in pygame.event.get():
pygame.error: video system not initialized
which is because the while loop still runs one more time after pygame.quit() has been called, causing the error, but this is i suppose not type of error we are talking about.
Some "modules" of pygame need to be initialized, but not all. Pygame.init() inizializes all "modules" but you also can do that for every "module" individually. Here a helpfull link: https://www.pygame.org/docs/tut/ImportInit.html