📜 ⬆️ ⬇️

Pygame player

Hello world ,% Username%. I noticed that the last time on Habré a lot of posts about python. Yes, the language is gaining popularity. Hurray comrades! So I decided to join this language. Soon I got tired of the hello world and wanted to write something necessary. either didn’t look nice or were too heavy. A little too lazy, I got down to business.

Already at the very beginning of writing the script, I had a choice. Use pyqt and a library to work with sound, or use pygame. I chose the second, I was very sorry about that. The process has begun. I will not post the full code here, it is waiting for you on the link
However, due to the terrible habit of not commenting on the code, I will highlight here the main points of working with pygame.

The concept is that there is a main loop in the program, in which information is processed and it is conditionally changed. Pictures on the screen.
')
while running:
...

pygame.display.init()
pygame.font.init()
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=4096)


Initialization of used modules.
In general, in theory, it was possible to replace all this with pygame.init (), but it looks more solid, and the internal paranoiac will be a little less worried about speed. It is also necessary to clarify the last line.
frequency = 44100 - Audio sample rate, default 22050 (Sound awful)
size = -16 - Terrible magic voodoo, I will be grateful if someone explains why this parameter is needed.
Channels = 2 - Specify the number of channels to play.
buffer = 4096 - Buffer size

pygame.mixer.music.set_volume (self.logic.volume)
Specify the playback volume. It is important to remember that this method indicates the volume of the entire program, and not the individual files.

self.screen = pygame.display.set_mode((600,230))

This method creates the main window and the main Surface. As you may have guessed, it takes as an argument the resolution of the future window.

pygame.display.set_caption(u"Ktulhu fhtang")

Specifying the title of the main window.

self.ui = pygame.font.Font("Arial.ttf",32)

Download new font from file. It is not necessary to download from the file, but it seems to me a bit simpler than parsing with sysFont.
As arguments takes the file name and font size.

self.ui.render(u"Play",True,(0,0,0))

This method draws the text received by the first argument. The second argument is text smoothing. The third argument is the color value in the format (r, g, b). Immediately it should be noted that pygame can render unicode, if you put the magic letter u ""

self.screen.fill((255,255,255))

Fills the screen with the specified color, in general, not only the screen, but any Surface, primitives including.

self.screen.blit(self.play,(0,0))

An extremely important blit method, it takes 2 arguments. 1 - Any Surface object, 2 - Coordinates in which you need to draw a layer. What does this method do? It Attaches the received surface to the surface to which it is applied. In this case, self.play joins the main surface.

event = pygame.event.poll()

We save in a variable all events collected for one run of the cycle.

pygame.display.flip()

The method of updating the image on the screen. Without it, you will not see anything except a black screen.

pygame.time.wait(25)

Due to the fact that python is not too fast a language, a high number of fps loads even powerful computers, so I run every cycle to wait 25 milliseconds. FPS did not measure, but the processor is not loaded, and there is enough rendering speed.

Why did I regret choosing pygame? The problem is that when you try to play mp3 files with tags version 3 (and there are already a lot of them), pygame hangs and it is extremely unpleasant. The only solution is to change the library, I do not know when this bug will be fixed.

rghost.ru/38700690 - That Frankinstein described in the article above.

I know the code does not claim to be the most logical and well-designed, but still please do not kick

Source: https://habr.com/ru/post/146067/


All Articles