Play the Raspberry Pi "II"--using Python for animation and multimedia

Source: Internet
Author: User
Tags message queue rewind

Environment: Raspberry Pi, System Raspbian, System comes with two versions of Python and Pygame.

1. Artboards

The procedure is as follows:

1! /home/pi/game_1.py2 ImportPygame3 4width = 6405Height = 4806RADIUS = 1007Fill = 18 9Pygame.init ()#Initialize PygameTen  Onewindow =Pygame.display.set_mode ((window, height)) AWindow.fill (Pygame. Color (255, 255, 255))#fill the artboard with white -  whileTrue: -Pygame.draw.circle (window, Pygame. Color (255, 0, 0), the(WIDTH/2, HEIGHT/2), radius, fill)#Red Draw Circle -Pygame.display.update ()#Update Image

Input command: Python game_1.py

Run the program with the following results:

Keyboard Input: Ctrl + C

Terminates the program.

However, due to the Raspberry Pi graphical interface of the card burst, return to the command window and then CTRL + C will sometimes get stuck, rather than directly in the Pygame window operation to the crisp, in fact, Pygame window is a GUI programming, similar to the Tkinter module made of GUI, Then close the window requires an event input response, add code after the original game_1.py file:

1  while True: 2     if inch  for inch Pygame.event.get ()]:3          break

I found that the Raspberry Pi connection network is really slow, plug in the power to start the network for several minutes to SSH connection to the Raspberry Pi IP, as for the access to the external network, sometimes there is no, sometimes due to some kind of failure, such as a long time not to move the Raspberry Pi or restart WiFi will cause network connection failure, putty become inactive, This time only to re-plug the power to normal, or the direct SSH will show time out, can only say that no HDMI cable is troublesome, a restart the network or restart the computer will be wrong.

2. Surface

Surface can be imagined as a rectangular image, which can be made up of multiple image frames to implement a scene in a game or animation, and the next thing to do is merge the two pictures.

Tips: Because of the need for pictures here, so after the purchase of the HDMI converter, the use of the network is still very convenient, first convenient to map, downloaded Scrot, downloaded directly in the command line input:Scrot can intercept the current screen picture, can also be entered:Scrot -H command to view the help document, such as select a range to enter:scrot-s can intercept the desired image range, the saved picture is placed in the current path, you can use the pi inside the picture viewer to view the image.

The procedure is as follows:

1 ImportPygame2 3 Pygame.init ()4 #set the black background surface size5Screen = Pygame.display.set_mode ((450,450))  6 #load the first picture and convert it to a pattern that matches the current pattern7Background = Pygame.image.load ("banboo.jpg"). Convert_alpha ()8Theremin = Pygame.image.load ("panda.jpg"). Convert_alpha ()9 #combine the first picture with your surface and set the position of the picture merge by parameterTen Screen.blit (background, (0,0)) OneScreen.blit (Theremin, (135, 150)) A  whileTrue: -Pygame.display.update ()

The results are shown below:

Analysis: As can be seen, the giant panda is not fully displayed, because the giant panda "panda.jpg" the resolution of the picture is larger than the bamboo "banboo.jpg" This picture, so it is not possible to display the full picture of the panda, so you can try to adjust the giant panda pixels to adapt to the bamboo pixels.

3. Drawing on surface

If you want to draw a piece of text, you need to load the font, you need to use the Font module to load the fonts file and render the text. The list of fonts on the PI can be obtained first through the pygame.font.get_fonts () function:

1 Import Pygame 2 3 Pygame.init () 4  for inch pygame.font.get_fonts (): 5     Print FontName

Output results such as:

The following will use the code to load the Freemono font to render the text:

1 ImportPygame2 3 Pygame.init ()4Screen = Pygame.display.set_mode ((725, 92))5 #set the font type to "Freemono", size 72nd number6Font = Pygame.font.SysFont ("Freemono", 1, Bold =)7Textsurface = Font.render ("I Love my pi!", 1,8Pygame. Color (255, 255, 255))9Screen.blit (Textsurface, (10, 10))Ten  whileTrue: OnePygame.display.update ()

The output results are as follows:

4. Handling Events and inputs

In Pygame, a user-triggered event is captured and placed in the message queue as an object of the event, while the Pygame.event module provides an unhandled event object from the message queue and enables further processing of the event. The next step is to extend the Pygame_1 program to draw different circles by capturing mouse events, the closer the edge of the window is, the greater the radius of the circle.

The program looks like this :

The result diagram is beautiful:

5. Play Sound

Tips: Here, the Raspberry Pi screen appears at the top right of a color block when the power supply is not enough, it is recommended to intercept the USB port, with the power of the multi-interface USB, because of severe power supply, otherwise play music plug the headset when a variety of failures, and then the system crashes, the Raspberry Pi fever hair hot.

5.1 Open the tool interface by entering the following command on the command line:

sudo raspi-config

5.2 Select Advanced-option return, select audio and enter

5.3 Due to the default audio output of the test HDMI output, if your HDMI connected to the display is no sound playback port, it is recommended to switch to analog signal output, that is, headphone output, select forced audio can

OK, now you can Baidu an online music, in the headphone jack plug in headphones can hear music.

6. Play Video

In order not to let my Raspberry Pi early, I decided to get the power supply USB, and sure enough that the nasty small square disappeared, but not stable, electricity, or will be with the power of the red lights flashing a flash, the small box will flash a flash, may be the power supply is still not enough, or because I plugged in too much, Maybe the wireless mouse and keyboard will be better. ;

The Pygame.movie module can play the video, provided the video must be in MPEG1 format, which is similar to the ". mpg" End of the video. Of course, you can convert video from other formats to MPG format by installing FFmpeg.

The code is as follows:

1 ImportPygame2  fromTimeImportSleep3 4 Pygame.init ()5 6Screen = Pygame.display.set_mode ((320, 240))7Movie = Pygame.movie.Movie ("Foo.mpg")8 Movie.play ()9  whileTrue:Ten     if  not(movie.get_play): One         Print("Rewind") A Movie.rewind () # Rewind - movie.play () #重新播放 -     ifPygame. QUITinch[Event.type forEventinchPygame.event.get ()]: the          Break

Warm tip: There are many complete instance programs in the Pygame.examples module, you can find the source code in the/usr/share/pyshared/pygame/exalmpes directory.

Play the Raspberry Pi "II"--using Python for animation and multimedia

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.