Pygame study notes (1): rectangular and circular drawing examples

Source: Internet
Author: User
This article mainly introduces pygame study notes (1): rectangular and circular drawing examples. This article describes pygame windows, window exits, colors, circles, rectangles in pygame, and a complete example, you can refer to pygame as a python module designed for game development. 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 using pygame to draw a graph, you must first create a window, which is very simple. please refer to the following code.

The code is as follows:


Import pygame # This sentence does not need to be commented out, huh, huh
Pygame. init () # Initialize the module. This statement must be executed by any pygame program.

Screencaption = pygame. display. set_caption ('Hello World') # define the title of the window as 'Hello world'
Screen = pygame. display. set_mode ([640,480]) # defines the window size as 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:

The code is as follows:


While True:
For event in pygame. event. get ():
If event. type = pygame. QUIT:
Sys. exit ()


3. colors in pygame

In the screen. fill ([255,255,255]) statement, we can see that pygame uses an 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.

From pygame. color import THECOLORS
Then use a named color:

The code is as follows:


Pygame. draw. circle (screen, THECOLORS ["red"], [100,100)


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.

The code is as follows:


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:

The code is as follows:


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


You can also use the following definition method

The code is as follows:


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


Or

The code is as follows:


My_rect = pygame. Rect (250,150,300,200)
Pygame. draw. rect (screen, [255, 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:

The code is as follows:


Import pygame, sys
Import time
Import random

Pygame. 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], [left, top, width, height], 3)

Pygame. display. flip ()
While True:
For event in pygame. event. get ():
If event. type = pygame. QUIT:
Sys. exit ()


:

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.