Make game with Python & pygame (3)

Source: Internet
Author: User

Next we will introduce some basic things of pygame.

Drawing Images with pygame. image. Load () and bits ()

Many games do not just use simple drawing functions, but also require a variety of images (also known as Genie). pygame can be loaded to the surface object in PNG format, JPG, GIF, and BMP. You can search for different formats on the Internet.

Next, I will introduce the things in the last part of the code in the previous article.

The pygame. image. Load () function returns a surface object used to display images. This surface object is separated from the display window object, so we must copy the Image Display object to the display window object through the BITs () method.

The method is as follows:

Displaysurf. bsurf (variable Mg, (catx, Caty ))

The first parameter is the image object to be loaded, and the second parameter is the coordinate value of X and Y in the upper left corner of the image to be displayed.

Font

All games require text display. pygame provides some simple functions for fonts and text creation. The following is a program using the pygame font.

import pygame, sysfrom pygame.locals import *pygame.init()DISPLAYSURF = pygame.display.set_mode((400,300))pygame.display.set_caption("Hello World")WHITE = (255, 255, 255)GREEN = (0, 255, 0)BLUE = (0, 0 ,128)fontObj = pygame.font.Font('freesansbold.ttf', 32)textSurfaceObj = fontObj.render('Hello World!', True, GREEN, BLUE)textRectObj = textSurfaceObj.get_rect()textRectObj.center = (200, 150)while True:    DISPLAYSURF.fill(WHITE)    DISPLAYSURF.blit(textSurfaceObj, textRectObj)    for event in pygame.event.get():        if event.type == QUIT:            pygame.quit()            sys.exit()    pygame.display.update()    

Effect after execution

There are six steps to display the font on the screen:

  1. Create a pygame. Font. Font object
  2. Create a surface object to store the font display. You need to call the render () method of the font object.
  3. Create a rectangle object by calling the get_rect () method of the Surface object. This rectangle object will have the width and height of the font. However, the top and left attribute values are 0.
  4. Set the position of a rectangle object by changing its attribute value. Here we set the center of the rectangle object to 200,150
  5. Copy the Display object storing the font to the display window object (from the object returned by pygame. display. set_mode)
  6. Call pygame. display. Update () to display the window objects on the screen.
The constructor parameters of pygame. Font. Font () are a font file and an integer representing the font size. We passed "freesansbold. TTF "(The font is from pygame) and the integer 32. the parameter of the render () method is a string to be displayed. A bool value determines whether anti-aliasing, text color, and text background color are required. If you want a transparent background color, you only need to call this method without passing the last background color parameter. Anti-aliasing,This is a concept. (Omitted) Playing soundPlaying sound is even easier than displaying images. First, you need to create a pygame. mixer. Sound object by calling the pygame. mixer. Sound () constructor. It requires a string parameter, that is, the audio file name. Pygame can load WAV, MP3, or Oog audio files. The play () method of the sound object must be called to play the sound. Call the stop () method if you want to immediately stop the sound. The stop () method has no parameters. The following is a simple code for loading sound:
soundObj = pygame.mixer.Sound('beeps.wav')soundObj.play()import timetime.sleep(1)soundObj.stop()

Sound objects can produce good sound effects when a player does something, such as being hurt and picking up a coin. It is best for a game to have background music, no matter when the game starts. Pygame can only load one music file as the background music at a time. To load a background music file, call the pygame. mixer. Music. Load () function to pass a string about the music file name. The music file format can be WAV, MPs, or Midi. to start playing background music, call pygame. mixer. music. play (-1, 0.0) function. The-1 parameter enables the background music to be played permanently until the end of the music file. If it is set to 0 or an integer greater than 0, then the music will only cyclically pass the integer representing so many times, rather than always loop. 0.0 indicates playing music from the beginning of the music file. When the value is passed, the music starts to be played. To stop background music, you need to call the pygame. mixer. Music. Stop () function. This function has no parameter. The following code describes how to process music files:

pygame.mixer.music.load('backgroundmusic.mp3')pygame.mixer.music.play(-1,0.0)#....some more of your code goes here..pygame.mixer.music.stop()
The above is about some basic things of pygame. Then, the author continues the introduction of pygame with each chapter of a game, and the comments and explanations are all very detailed. It is really a good book.

Related Article

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.