I want to create a really simple application that starts a song while you press the SPACE bar, but when I start the application and press the space bar, I just hear a "pop" sound, and nothing starts. No music.
Here is the code:
import pygame
from pygame.locals import *
pygame.init()
backimage = pygame.display.set_mode((395, 702), RESIZABLE)
fond = pygame.image.load("background.jpg").convert()
backimage.blit(fond, (0,0))
pygame.display.flip()
pygame.mixer.pre_init(42000,-16,1,2048)
pygame.mixer.init()
musik = pygame.mixer.Sound(b'musik.wav')
continuer = 1
while continuer == 1:
for event in pygame.event.get():
if event.type == QUIT:
continuer = 0
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
musik.play()
Ok, thank you so much guys ! It works perfectly now.
Here is the final code if someone has the same problem as me:
import pygame
from pygame.locals import *
pygame.init()
backimage = pygame.display.set_mode((395, 702), RESIZABLE)
fond = pygame.image.load("background.jpg").convert()
backimage.blit(fond, (0,0))
pygame.display.flip()
pygame.mixer.init()
pygame.mixer.music.load(b'musik.mp3')
pygame.event.clear()
while True:
event = pygame.event.wait()
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN and event.key == K_SPACE:
pygame.mixer.music.play()
elif event.type == KEYUP and event.key == K_SPACE:
pygame.mixer.music.stop()
I don't know why the code doesn't work, but I know it does work if you use a mp3 file. try this:
import pygame
from pygame.locals import *
pygame.init()
backimage = pygame.display.set_mode((395, 702), RESIZABLE)
fond = pygame.image.load("background.jpg").convert()
backimage.blit(fond, (0,0))
pygame.display.flip()
pygame.mixer.init()
pygame.mixer.music.load(b'musik.mp3')
continuer = 1
while continuer == 1:
for event in pygame.event.get():
if event.type == QUIT:
continuer = 0
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
pygame.mixer.music.play()
If you want to use your .wav file you can find .wav to .mp3 online
I think that you misunderstood the use for pygame.event. You should try with pygame.event.wait() :
pygame.event.clear()
while True:
# wait until new event happens - blocking instruction
event = pygame.event.wait()
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN and event.key = K_SPACE:
musik.play()
Related
My program plays music when I press the space bar. How to stop music when the s key is pressed?
import pygame, sys
from pygame.locals import QUIT
def main():
pygame.init()
#Intalise
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Drop Shadow!')
#load sound
beep_sound = pygame.mixer.Sound('Joy.wav')
#load music
pygame.mixer.music.load('Joy.wav')
# Game Clock
game_clock = pygame.time.Clock()
#Game Loop
running = True
while running:
#Update Section
#Update delta_time
delta_time = game_clock.tick(60) / 100
#Handle events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
#play sound
pygame.mixer.Sound.play(beep_sound)
if event.key == pygame.K_p:
#play music
pygame.mixer.music.play(-1)
#Draw Section
DISPLAYSURF.fill((128, 128, 128))
pygame.display.flip()
if __name__ == "__main__":
main()
You can use the pygame.mixer.music.stop():
Stops the music playback if it is currently playing. endevent will be triggered, if set. It won't unload the music.
Conditionally call it in your event-loop when pygame.K_s is pressed, like so:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
#play sound
pygame.mixer.Sound.play(beep_sound)
if event.key == pygame.K_p:
#play music
pygame.mixer.music.play(-1)
if event.key == pygame.K_s:
#stop music
pygame.mixer.music.stop()
I am new to pygame. I've coded on Python before but never in pygame.
I've made a code which plays a sound when certain keys are clicked and now I tried to make in image moving every time that the user click with his mouse.
import pygame, os, sys
pygame.init()
screen = pygame.display.set_mode((1300,1300))
screen.fill((250,250,250))
img = pygame.image.load(os.path.join(sys.path[0],"fonddecran.v1.jpg"))
screen.blit(img, (0, 0))
pygame.display.flip()
barrel=pygame.image.load(os.path.join(sys.path[0],"barrel-man.jpg"))
pygame.display.flip()
pygame.mixer.init()
it = true
while it:
for event in pygame.event.get():
if event.type == pygame.QUIT:
it=False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
elif event.button == 1:
screen.blit(barrel,event.pos)
pygame.display.flip()
elif event.type == pygame.KEYUP:
if event.key == ord('b'):
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
pygame.quit()
Unfortunately, the image just appears one more time every time. How can I delete it so it looks like it has moved?
To be able to "move" the image, you have to redraw the scene in each frame. Add a variable that stores the position of the image. Change the variable to the mouse position when you click the mouse:
import pygame, os, sys
pygame.init()
screen = pygame.display.set_mode((1300,1300))
img = pygame.image.load(os.path.join(sys.path[0],"fonddecran.v1.jpg"))
barrel=pygame.image.load(os.path.join(sys.path[0],"barrel-man.jpg"))
pygame.mixer.init()
barrel_pos = None
it = True
while it:
for event in pygame.event.get():
if event.type == pygame.QUIT:
it=False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
elif event.button == 1:
barrel_pos = event.pos
elif event.type == pygame.KEYUP:
if event.key == ord('b'):
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
screen.fill((250,250,250))
screen.blit(img, (0, 0))
if barrel_pos:
screen.blit(barrel, barrel_pos)
pygame.display.flip()
pygame.quit()
I'm trying to pause my game by pressing the 'p' key, but after it pauses it does not unpause when p is pressed again. Here are the relevant segments of my code, I'd like to know how to fix this and/or if there are better alternative methods of implementing a game pause.
pause = False
while True:
if not pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#game code
keys = pygame.key.get_pressed()
#if key up
elif keys[pygame.K_p]:
pause = True #appears to work correctly, screen freezes and
#prints "PAUSED" every tick.
#more game code
pygame.display.update()
fpsClock.tick(FPS)
else:
print("PAUSED")
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_p:
pause = False
pygame.display.update()
fpsClock.tick(FPS)
The code below does the trick.
I have added in if not pause: ... else: section also a handler for a keypress because without it there will be no chance to come out of the pause (the part if not pause won't never ever be run without this keypress check if once paused).
import pygame
pygame.init() # start PyGame (necessary because 'import pygame' doesn't start PyGame)
colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)
colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)
winDisplay = pygame.display.set_mode((1024, 768)) # set PyGame window size to 1024x768 pixel
pygame.display.set_caption("Minimal PyGame Test Script")
winDisplay.fill(colorWhite)
fpsClock = pygame.time.Clock()
FPS = 15
pause = False
pauseCounter = 0
while True:
if not pause:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
pause = not pause
#game code
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#more game code
pygame.display.update()
fpsClock.tick(FPS)
else:
pauseCounter += 1
print(pauseCounter, "PAUSED")
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
pause = not pause
#more game code
pygame.display.update()
fpsClock.tick(FPS)
I am using a function to wait until a controller is present, but i ran into a problem. I wanted the function to stop when I press a key. Usually these events are handled just fine, but in this part it works terrible. The event seems to be added to the event queue very late or sometimes not at all. My guess is that it is because of the uninitialisation and reinitialisation of pygame.joystick. I just don't know how to solve it. I used this program to create the error:
import pygame, sys
from pygame.locals import *
pygame.init()
SCREEN = pygame.display.set_mode((500,500))
ChangeRun = False
pygame.joystick.init()
if pygame.joystick.get_count() == 0:
while pygame.joystick.get_count() == 0:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type in [KEYUP, KEYDOWN]:
if event.key == K_ESCAPE:
ChangeRun = True
if ChangeRun == True:
break
pygame.joystick.quit()
pygame.joystick.init()
pygame.quit()
sys.exit()
I wanted to use the escape key to end the program, but only after a lot of pressing it works. The same happens with the QUIT event. When printing the events I found that most of the time no event was found.
I use windows 8.1 and python 3.4 and pygame 1.9.2
I have no joystick so I can't test it but normally I would do this similar to mouse and I wouldn't wait for controller:
import pygame
from pygame.locals import *
pygame.init()
pygame.joystick.init()
screen = pygame.display.set_mode((500,500))
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == JOYBUTTONDOWN:
print 'joy:', event.joy, 'button:', event.button
# the end
pygame.joystick.quit()
pygame.quit()
I'm using pygame to draw a line:
import pygame
from pygame.locals import*
import sys
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Lines")
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
screen.fill((0,80,0))
color = 100,255,200
width = 8
pygame.draw.line(screen, color, (100,100), (500,400), width)
pygame.display.update()
for some reason, I can't get this is work:
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
It doesnt show any error, it just doesnt work. I want to be able to press the down key and have it quit the program but it doesnt do that. I have to quit the idle. Any help will do. thanks.
This is because KEYDOWN is just the event that a key is pressed down, not that the down key has been pushed. To fix this, you have to first check if a KEYDOWN event has happened, and if it has, check what key that was pushed.
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
sys.exit()
Check out the docs on this subject to learn more.