Utilising the pygame.mixer.music.get_endevent() - python-3.x

I want a tkinter label to show nothing when a sound effect has finished.
I've been researching the www on how to create/initialise/catch the end of music event with no luck.
def play_btn():
if mixer.music.get_busy():
mixer.music.fadeout(1000)
snd_fyl.set(snd_list.get(ACTIVE))
mixer.music.load(snd_dir+"/"+snd_list.get(ACTIVE)+"mp3")
mixer.music.play()
def stop_btn():
mixer.music.stop()
clear_label()
def clear_label():
snd_fyl.set("")
snd_lbl1 = LabelFrame(MainWindow, text="Sound effect playing", labelanchor=N)
snd_playing_lbl = Label(snd_lbl1, width=40, textvariable=snd_fyl)
Obviously play_btn function plays a sound effect from a list.
The stop_btn function prematurely halts the sound effect and clears the label.
The clear_label function has been created in readiness for the end_of_song event

You have to use set_endevent() to set value which it will send to event queue when music has finished.
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
And then you can test it in event loop
if event.type == MUSIC_END:
print('music end event')
It will print text when music has finished - but not when you stop it or pause it.
BTW: on Linux I see this text few milliseconds before it ends playing music.
Full working example - but without tkinter
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
pygame.mixer.music.load('sound.wav')
pygame.mixer.music.play()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MUSIC_END:
print('music end event')
if event.type == pygame.MOUSEBUTTONDOWN:
# play again
pygame.mixer.music.play()
pygame.quit()
EDIT: Example with tkinter
import pygame
import tkinter as tk
def check_event():
for event in pygame.event.get():
if event.type == MUSIC_END:
print('music end event')
label['text'] = ''
root.after(100, check_event)
def play():
label['text'] = 'playing'
pygame.mixer.music.play()
# --- main ---
pygame.init()
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
pygame.mixer.music.load('sound.wav')
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(root, text='Play', command=play)
button.pack()
check_event()
root.mainloop()
pygame.quit()

Related

Pygame gamepad won't run anymore with an Xbox 360 controller and an Ubuntu machine [duplicate]

This code goes into infinite loop. I cant use A button on xbox 360 controller
import pygame
from pygame import joystick
pygame.init()
joystick = pygame.joystick.Joystick(0)
pygame.joystick.init()
print("start")
while True:
if joystick.get_button(0) == 1 :
print("stoped")
break
I cant use A button on xbox 360 controller
Personnaly, I can, so this seems to be possible. You are just missing that pretty much every user input needs to be updated by pygame through pygame.event.get().
From the pygame documentation:
Once the device is initialized the pygame event queue will start receiving events about its input.
So, apparently you need to get the events in the while loop like such to make the joystick work:
import pygame
from pygame.locals import *
pygame.init()
joystick = pygame.joystick.Joystick(0)
while True:
for event in pygame.event.get(): # get the events (update the joystick)
if event.type == QUIT: # allow to click on the X button to close the window
pygame.quit()
exit()
if joystick.get_button(0):
print("stopped")
break
Also,
In the line if joystick.get_button(0) == 1, you don't need to type == 1 because the statement is already True.
You are initializing pygame.joystick twice: through the line pygame.init() and pygame.joystick.init().
You don't need to type from pygame import joystick because you already already have it in the line import pygame.
You can take this as reference and use it in your own way.
import pygame
import sys
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()
WIDTH,HEIGHT = 500,500
WHITE = (255,255,255)
BLUE = (0,0,255)
BLUISH = (75,75,255)
YELLOW =(255,255,0)
screen = pygame.display.set_mode((WIDTH,HEIGHT))
smile = pygame.image.load("smile.jpg")
smile = pygame.transform.scale(smile,(WIDTH,HEIGHT))
idle = pygame.image.load("idle.jpg")
idle = pygame.transform.scale(idle,(WIDTH,HEIGHT))
joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYBUTTONDOWN:
if event.button == 0: #press A button to smile
screen.fill(WHITE)
screen.blit(smile,(0,0))
pygame.display.update()
clock.tick(10)
elif event.type == pygame.JOYBUTTONUP:
if event.button == 0:
screen.fill(WHITE)
screen.blit(idle,(0,0))
pygame.display.update()
clock.tick(10)

What can I do to make my PyGame window respond?

I am trying to make a window open every time I run my code, but every time I run the code, it shows the window as unresponsive.
My code is as follows:
import os
import time
import sys
pygame.font.init()
WIDTH, HEIGHT = 500, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")), (WIDTH, HEIGHT))
def main():
fps = 60
clock = pygame.time.Clock()
def redraw_window():
WIN.blit(BG, (0,0))
pygame.display.update()
while True:
clock.tick(fps)
redraw_window()
for event in pygame.event.get():
if event.type == sys.exit:
sys.exit()
input()
main()
I've already tried a few thing with event.get and event.pump, I'm probably missing something very basic. Could someone tell me what else I could try?
Also, I'm new at pygame, so if anyone sees another mistake, please tell me about. I would greatly appreciate it.
The window is unresponsive because you don't handle the events by calling pygame.event.get().
While pygame.event.get() is in your code, it's never executed because
a) it's in a function you never call
b) your code blocks on the input() call
You're code should look like this:
import os
pygame.init()
WIDTH, HEIGHT = 500, 400
def main():
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")).convert_alpha(), (WIDTH, HEIGHT))
fps = 60
clock = pygame.time.Clock()
while True:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
WIN.blit(BG, (0,0))
pygame.display.flip()
main()

How do I make graphics of this button better in pygame?

import pygame, sys, time
from pygame.locals import*
###################################
screen = "START"
pygame.init()
window_width = 1000
window_height = 650
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("LOGO QUIZ")
#############################################
def button(normal, activated, x_pos, y_pos, length, height, func=None):
button = pygame.image.load(normal)
mousex, mousey = pygame.mouse.get_pos()
if (mousex>x_pos)and(mousex<(length+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
button = pygame.image.load(activated)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN )and (event.button == 1):
if (mousex>x_pos)and(mousex<(mousex+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
func()
window.blit(button,(x_pos, y_pos))
## button = pygame.image.load(activated)
window.blit(button,(x_pos, y_pos))
def START():
global screen
while screen != "QUIT":
for event in pygame.event.get():
if event.type == QUIT:
screen = "QUIT"
bg = pygame.image.load("START.png")
window.blit(bg,(0,0))
button("play_u.png", "play_a.png", 350, 200, 325, 75, game)
pygame.display.update()
def game():
global screen
while screen != "QUIT":
for event in pygame.event.get():
if event.type == QUIT:
screen = "QUIT"
window.fill((20, 20, 20))
pygame.display.update()
pygame.quit()
sys.exit()
START()
pygame.display.update()
The above code is for the start menu of my game, I made a START function to show the start screen and then the game function to run the main game later, in the button function I want to make it as when the mouse is hovered over the picture, it should change the picture(activated pic) but when I do this it takes lots of time it change , how do i shorten the time?
Your code does not work because you call pygame.event.get() multiple times per frame; once in START, and once in button.
Think about what happends when there's a MOUSEBUTTONDOWN event in the queue, then pygame.event.get() is called in START: The event gets removed from the queue, and when pygame.event.get() is called in button, the MOUSEBUTTONDOWN event is already gone and if (event.type == pygame.MOUSEBUTTONDOWN ) will never be True.
So instead of calling for event in pygame.event.get(): in START, you could store the result of pygame.event.get() in a variable and iterate over that in START and button. This way, you won't lose events.
Here's an example:
...
def button(events, normal, activated, x_pos, y_pos, length, height, func=None):
button = pygame.image.load(normal)
mousex, mousey = pygame.mouse.get_pos()
if (mousex>x_pos)and(mousex<(length+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
button = pygame.image.load(activated)
# iterate over the list if events instead of calling pygame.event.get
for event in events:
if (event.type == pygame.MOUSEBUTTONDOWN )and (event.button == 1):
if (mousex>x_pos)and(mousex<(mousex+x_pos))and(mousey>y_pos)and(mousey<(y_pos+height)):
func()
window.blit(button,(x_pos, y_pos))
...
def START():
global screen
while screen != "QUIT":
# store the list of events in a variable
events = pygame.event.get()
# iterate over the new list
for event in events:
if event.type == QUIT:
screen = "QUIT"
bg = pygame.image.load("START.png")
window.blit(bg,(0,0))
# pass the list of events to the button function
button(events, "play_u.png", "play_a.png", 350, 200, 325, 75, game)
pygame.display.update()

Can't import any pygame functions (E1101)

When I run the following code, a white screen appears and then quickly disappears. I installed pygame on the VSCode terminal with pip3 (I am on mac) and now that I finally got it to import pygame, I get multiple errors for every function called such as "clock" or "pygame.KEYDOWN". They say "Module 'pygame' has no 'KEYDOWN' member" [E1101]. I also get an error with the "init" member.
I've seen other posts that tell me to copy and paste something into the json settings, but when I tried that I got even more errors.
#Game
#By Robert Smith
#Initialize python
import pygame
pygame.init()
#Set the screen
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
screen.blit(background, (0 , 0))
#Make the window exitable
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop == False #close the window
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
mainloop = False
#Set the framerate
milliseconds = clock.tick(60)#DO NOT GO FASTER THAN THIS FRAMERATE
Try this:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop == False #close the window
elif event.type == keys[pygame.K_DOWN]:
if event.key == keys[pygame.K_ESCAPE]:
mainloop = False
And this:
milliseconds = pygame.time.Clock.tick(60)

Quit function is not working in pygame

I want the system to exit when I click the "x" on the window of pygame. I want to be able to call the quit_game function whenever I need to. So if the user does quit, it actually quits. I have tried sys exit and pygame.quit(), but I haven't been able to successfully implement those. Right now I just have the quit function built into the intro screen. I am using python 3.4.3. Here is the code.
import math
import random
import time
import pygame
import sys
import glob
pygame.init()
move=0
FPS=60
blue=(0,0,255)
white=(255,255,255)
black=(0,0,0)
green=(0,155,0)
display_width=800
display_height=600
gamedisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Stacker')
clock=pygame.time.Clock()
speed=2
smallfont=pygame.font.SysFont("Arial",int(display_width/32))
mediumfont=pygame.font.SysFont("Arial",int(display_width/16))
largefont=pygame.font.SysFont("Arial",int(display_width/10))
gamedisplay.fill(green)
block=display_height/12
pygame.display.update()
def quit_game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
def intro_screen():
welcome_message = mediumfont.render(str("Welcome to Stacker!!!"), True,black)
gamedisplay.blit(welcome_message,(display_width/4,display_height/40))
how_to_play_1=smallfont.render(str("Your goal is to get to the major prize bar"),True,black)
gamedisplay.blit(how_to_play_1,(display_width/3.619909502,display_height/2))
how_to_play=smallfont.render(str("Press the space bar to stop the shape"),True,black)
gamedisplay.blit(how_to_play,(display_width/3.48583878,display_height/(12/7)))
quit_game()
pygame.display.update()
def middle_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/15),display_height-block,block,block))
pygame.display.update()
def left_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/13),display_height-block,block,block))
pygame.display.update()
def right_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/17),display_height-block,block,block))
pygame.display.update()
def major_screen():
major_message = mediumfont.render(str("Major Prize Here!"), True,black)
gamedisplay.blit(major_message,(display_width/(10/3),display_height/40))
pygame.display.update()
intro_screen()
pygame.time.delay(8000)
gamedisplay.fill(green)
major_screen()
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60))
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60))
middle_block()
left_block()
right_block()
pygame.display.update()
Your code has no main game-loop. You may thinks this:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
Is your game-loop. While that is almost correct, it is missing a key part. A while loop. Your telling Python to loop through every single event that Pygame is tracking. After it finishes looping through the events once, it stops. You want Python to continually loop through the Pygame events, until the window is closed. To do this create a variable to control your loop. Such as running = True or not_quit = True. Next wrap a while-loop around your for-loop using the control variable as your condition. Lastly, if the user closes the window, set your control variable to False.
running = True # our variable to control the loop.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # break the loop
pygame.quit()
sys.exit()
quit()
This should allow you to close your window. I should note however that none of the text/shapes your blitting/drawing to the screen will be visible unless inside your game-loop. So you may need to rethink your code a bit.

Resources