Pygame Study Notes (1) -- installation and rectangular and circular drawing

Source: Internet
Author: User

Pygame is a python module designed to develop games. In fact, pygame is the same as time, OS, and SYS. Today, I officially started learning pygame: www.pygame.org. After the download, the installation is complete. In pygame's learning, I used the SPE editor, and it feels good.

1. pygame window

Before creating a graph in pygame, you must create a window.CodeIs it very easy.

ImportPygame # You don't need to comment on this sentence, huh, pygame. INIT ()#Module initialization,Any pygameProgramYou must execute this sentence.

Screencaption= Pygame. display. set_caption ('Hello World') # Define the title of the window as 'Hello world'
Screen= Pygame. display. set_mode ([640,480]) # Define a window size of 640*480
Screen. Fill ([255,255,255]) # Fill the window with white

2. Exit the window

Pygame has an event loop that constantly checks what users are doing. In the event loop, how to interrupt the loop (the plug-in on the right side of the window formed by pygame does not work before it is undefined), the Common Code is as follows:

WhileTrue:ForEventInPygame. event. Get ():IfEvent. type =Pygame. Quit: SYS. Exit ()

3. colors in pygame

InScreen. Fill ([255,255,255]) in this statement, we can see that pygame uses the RGB system. [0,255, 0] for pure green, [255,] for pure blue, [, 0] for pure red. If you do not use the RGB notation, pygame also provides a named color list, or you can directly use these named colors. There are more than 600 defined color sentences. You can view the specific names in the colordict. py file. When using the name list, you must first import thecolors at the beginning of the program.

 
FromPygame. ColorImportThecolors

Then use a named color:

Pygame. Draw. Circle (screen, thecolors ["Red"], [100,100], 30, 0)

4. Circle

 
Pygame. Draw. Circle () is used to draw a circle. It consists of five parameters: (1) the surface of the circle. In this example, a window is created with screen, so it is painted on the screen surface. (2) The color used for painting, for example, red [, 0]. (3) Where to draw, [top, left]. (4) diameter. (5) The line width. 0 indicates completion of filling.
 
Pygame. Draw. Circle (screen, [100,100, 0)

5. rectangle

Pygame. Draw. rect () is used to create a rectangle. Rect (left, top, width, height) is used to define the position and width and height. The Code is as follows:

 
Pygame. Draw. rect (screen, [250,150,300,200, 0], [], 0)

You can also use the following definition method

 
Rect_list = [250,150,300,200] Pygame. Draw. rect (screen ,[255, 0, 0], rect_list, 0)

Or

 
My_rect = pygame. rect (250,150,300,200) Pygame. Draw. rect (screen ,[255, 0, 0], my_rect, 0)

6. Instances

The random module is used to randomly generate sizes and positions for painting on the surface. The specific code is as follows:

 #  @ Xiaowuyi http://www.cnblogs.com/xiaowuyi  Import  Pygame, sys  Import  Time  Import  Randompygame. INIT () screencaption = Pygame. display. set_caption ( '  Hello World  '  ) Screen = Pygame. display. set_mode ([640,480 ]) Screen. Fill ([ 255,255,255 ])  For I In Range (10 ): Zhijing = Random. randint (0,100 ) Width = Random. randint (0,255 ) Height = Random. randint (0,100 ) Top = Random. randint (0,400 ) Left = Random. randint (0,500 ) Pygame. Draw. Circle (screen, [0, 0], [top, left], zhijing, 1 ) Pygame. Draw. rect (screen ,[ 255, 0, 0], [left, top, width, height], 3) Pygame. display. Flip ()  While  True:  For Event In  Pygame. event. Get ():  If Event. type = Pygame. Quit: SYS. Exit () 

For more information, see "Xiao Wuyi ".Http://www.cnblogs.com/xiaowuyi

 

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.