[PYTHON] Implement timer function in pygame

Introduction

Using a library called pygame When creating a program like a so-called timer that plays music after a specified time has passed There was a problem that no error message was displayed and no music was played. I will write about the solution.

environment

Python 3.6.8 pygame 1.9.6

Conclusion

When music is output by pygame.mixer.music.play () Music can now be played by delaying the execution of the program by the time the music is played with time.sleep ().

Code created

The code that caused the problem

#Create a program to play music

import pygame
import time

react_time = int(input("Set how many seconds you want the music to play>>"))

print(react_time)
time.sleep(react_time)

pygame.mixer.init()
pygame.mixer.music.load("BGM.mp3")
pygame.mixer.music.play()

print("music play")

When you execute the code and enter 3 in the terminal,

Set how many seconds you want the music to play>>3
3
music play

Only the result is output, and no music is played.

Problem-solving code

By causing a delay with time.sleep () after pygame.mixer.music.play (), Music started to play.

import pygame
import time

react_time = int(input("Set how many seconds you want the music to play>>"))

print(react_time)
time.sleep(react_time)

pygame.mixer.init()
pygame.mixer.music.load("BGM.mp3")
pygame.mixer.music.play()

time.sleep(10)  # time.sleep()To secure the time until the end of processing with

print("music play")

reference

No sound in python pygame https://teratail.com/questions/173534

Recommended Posts

Implement timer function in pygame
Implement R's power.prop.test function in python
Duality in function
Implement Enigma in python
Generator function in JavaScript
Implement recommendations in Python
Implement XENO in python
Implement sum in Python
Implement Traceroute in Python 3
Implement LSTM AutoEncoder in Keras
Covector to think in function
Implement follow functionality in Django
Create a function in Python
Use callback function in Python
ntile (decile) function in python
Implement Style Transfer in Pytorch
Implement recursive closures in Go
Implement naive bayes in Python 3.3
Implement UnionFind (equivalent) in 10 lines
Implement ancient ciphers in python
Timer used in while loop
Nonlinear function modeling in Python
Draw implicit function in python
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
I tried to implement the mail sending function in Python
Immediate function in python (lie)
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement Gaussian process in Pyro
Implement stacking learning in Python [Kaggle]
Implement Table Driven Test in Java
Create user authentication function in Airflow
sort warning in the pd.concat function
Function argument type definition in python
Included notation in Python function arguments
Implementation of login function in Django
Implement a date setter in Tkinter
Write AWS Lambda function in Python
Measure function execution time in Python
Implement the Singleton pattern in Python
Function synthesis and application in Python
Quickly implement REST API in Python